ozone: drm: Crash immediately if no usable GPU is found
[chromium-blink-merge.git] / net / udp / udp_socket_libevent.h
blob7ce730e0f06046a37ed9bf64e7224862287d6513
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_
6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/threading/non_thread_safe.h"
12 #include "net/base/address_family.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/ip_endpoint.h"
16 #include "net/base/net_export.h"
17 #include "net/base/net_util.h"
18 #include "net/base/rand_callback.h"
19 #include "net/log/net_log.h"
20 #include "net/socket/socket_descriptor.h"
21 #include "net/udp/datagram_socket.h"
23 namespace net {
25 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe {
26 public:
27 UDPSocketLibevent(DatagramSocket::BindType bind_type,
28 const RandIntCallback& rand_int_cb,
29 net::NetLog* net_log,
30 const net::NetLog::Source& source);
31 virtual ~UDPSocketLibevent();
33 // Opens the socket.
34 // Returns a net error code.
35 int Open(AddressFamily address_family);
37 // Connects the socket to connect with a certain |address|.
38 // Should be called after Open().
39 // Returns a net error code.
40 int Connect(const IPEndPoint& address);
42 // Binds the address/port for this socket to |address|. This is generally
43 // only used on a server. Should be called after Open().
44 // Returns a net error code.
45 int Bind(const IPEndPoint& address);
47 // Closes the socket.
48 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
49 void Close();
51 // Copies the remote udp address into |address| and returns a net error code.
52 int GetPeerAddress(IPEndPoint* address) const;
54 // Copies the local udp address into |address| and returns a net error code.
55 // (similar to getsockname)
56 int GetLocalAddress(IPEndPoint* address) const;
58 // IO:
59 // Multiple outstanding read requests are not supported.
60 // Full duplex mode (reading and writing at the same time) is supported
62 // Reads from the socket.
63 // Only usable from the client-side of a UDP socket, after the socket
64 // has been connected.
65 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
67 // Writes to the socket.
68 // Only usable from the client-side of a UDP socket, after the socket
69 // has been connected.
70 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
72 // Reads from a socket and receive sender address information.
73 // |buf| is the buffer to read data into.
74 // |buf_len| is the maximum amount of data to read.
75 // |address| is a buffer provided by the caller for receiving the sender
76 // address information about the received data. This buffer must be kept
77 // alive by the caller until the callback is placed.
78 // |address_length| is a ptr to the length of the |address| buffer. This
79 // is an input parameter containing the maximum size |address| can hold
80 // and also an output parameter for the size of |address| upon completion.
81 // |callback| is the callback on completion of the RecvFrom.
82 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
83 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|,
84 // and |address_length| alive until the callback is called.
85 int RecvFrom(IOBuffer* buf,
86 int buf_len,
87 IPEndPoint* address,
88 const CompletionCallback& callback);
90 // Sends to a socket with a particular destination.
91 // |buf| is the buffer to send
92 // |buf_len| is the number of bytes to send
93 // |address| is the recipient address.
94 // |address_length| is the size of the recipient address
95 // |callback| is the user callback function to call on complete.
96 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
97 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
98 // alive until the callback is called.
99 int SendTo(IOBuffer* buf,
100 int buf_len,
101 const IPEndPoint& address,
102 const CompletionCallback& callback);
104 // Sets the receive buffer size (in bytes) for the socket.
105 // Returns a net error code.
106 int SetReceiveBufferSize(int32 size);
108 // Sets the send buffer size (in bytes) for the socket.
109 // Returns a net error code.
110 int SetSendBufferSize(int32 size);
112 // Returns true if the socket is already connected or bound.
113 bool is_connected() const { return is_connected_; }
115 const BoundNetLog& NetLog() const { return net_log_; }
117 // Sets corresponding flags in |socket_options_| to allow the socket
118 // to share the local address to which the socket will be bound with
119 // other processes. Should be called between Open() and Bind().
120 // Returns a net error code.
121 int AllowAddressReuse();
123 // Sets corresponding flags in |socket_options_| to allow or disallow sending
124 // and receiving packets to and from broadcast addresses.
125 // Returns a net error code.
126 int SetBroadcast(bool broadcast);
128 // Joins the multicast group.
129 // |group_address| is the group address to join, could be either
130 // an IPv4 or IPv6 address.
131 // Returns a net error code.
132 int JoinGroup(const IPAddressNumber& group_address) const;
134 // Leaves the multicast group.
135 // |group_address| is the group address to leave, could be either
136 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
137 // it will be ignored.
138 // It's optional to leave the multicast group before destroying
139 // the socket. It will be done by the OS.
140 // Returns a net error code.
141 int LeaveGroup(const IPAddressNumber& group_address) const;
143 // Sets interface to use for multicast. If |interface_index| set to 0,
144 // default interface is used.
145 // Should be called before Bind().
146 // Returns a net error code.
147 int SetMulticastInterface(uint32 interface_index);
149 // Sets the time-to-live option for UDP packets sent to the multicast
150 // group address. The default value of this option is 1.
151 // Cannot be negative or more than 255.
152 // Should be called before Bind().
153 // Returns a net error code.
154 int SetMulticastTimeToLive(int time_to_live);
156 // Sets the loopback flag for UDP socket. If this flag is true, the host
157 // will receive packets sent to the joined group from itself.
158 // The default value of this option is true.
159 // Should be called before Bind().
160 // Returns a net error code.
162 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
163 // different between Windows and Unix-like systems. The inconsistency only
164 // happens when there are more than one applications on the same host
165 // joined to the same multicast group while having different settings on
166 // multicast loopback mode. On Windows, the applications with loopback off
167 // will not RECEIVE the loopback packets; while on Unix-like systems, the
168 // applications with loopback off will not SEND the loopback packets to
169 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
170 int SetMulticastLoopbackMode(bool loopback);
172 // Sets the differentiated services flags on outgoing packets. May not
173 // do anything on some platforms.
174 // Returns a net error code.
175 int SetDiffServCodePoint(DiffServCodePoint dscp);
177 // Resets the thread to be used for thread-safety checks.
178 void DetachFromThread();
180 private:
181 enum SocketOptions {
182 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
185 class ReadWatcher : public base::MessageLoopForIO::Watcher {
186 public:
187 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
189 // MessageLoopForIO::Watcher methods
191 void OnFileCanReadWithoutBlocking(int /* fd */) override;
193 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
195 private:
196 UDPSocketLibevent* const socket_;
198 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
201 class WriteWatcher : public base::MessageLoopForIO::Watcher {
202 public:
203 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
205 // MessageLoopForIO::Watcher methods
207 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
209 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
211 private:
212 UDPSocketLibevent* const socket_;
214 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
217 void DoReadCallback(int rv);
218 void DoWriteCallback(int rv);
219 void DidCompleteRead();
220 void DidCompleteWrite();
222 // Handles stats and logging. |result| is the number of bytes transferred, on
223 // success, or the net error code on failure. On success, LogRead takes in a
224 // sockaddr and its length, which are mandatory, while LogWrite takes in an
225 // optional IPEndPoint.
226 void LogRead(int result, const char* bytes, socklen_t addr_len,
227 const sockaddr* addr) const;
228 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
230 // Same as SendTo(), except that address is passed by pointer
231 // instead of by reference. It is called from Write() with |address|
232 // set to NULL.
233 int SendToOrWrite(IOBuffer* buf,
234 int buf_len,
235 const IPEndPoint* address,
236 const CompletionCallback& callback);
238 int InternalConnect(const IPEndPoint& address);
239 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
240 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
242 // Applies |socket_options_| to |socket_|. Should be called before
243 // Bind().
244 int SetMulticastOptions();
245 int DoBind(const IPEndPoint& address);
246 // Binds to a random port on |address|.
247 int RandomBind(const IPAddressNumber& address);
249 int socket_;
251 int addr_family_;
252 bool is_connected_;
254 // Bitwise-or'd combination of SocketOptions. Specifies the set of
255 // options that should be applied to |socket_| before Bind().
256 int socket_options_;
258 // Multicast interface.
259 uint32 multicast_interface_;
261 // Multicast socket options cached for SetMulticastOption.
262 // Cannot be used after Bind().
263 int multicast_time_to_live_;
265 // How to do source port binding, used only when UDPSocket is part of
266 // UDPClientSocket, since UDPServerSocket provides Bind.
267 DatagramSocket::BindType bind_type_;
269 // PRNG function for generating port numbers.
270 RandIntCallback rand_int_cb_;
272 // These are mutable since they're just cached copies to make
273 // GetPeerAddress/GetLocalAddress smarter.
274 mutable scoped_ptr<IPEndPoint> local_address_;
275 mutable scoped_ptr<IPEndPoint> remote_address_;
277 // The socket's libevent wrappers
278 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
279 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
281 // The corresponding watchers for reads and writes.
282 ReadWatcher read_watcher_;
283 WriteWatcher write_watcher_;
285 // The buffer used by InternalRead() to retry Read requests
286 scoped_refptr<IOBuffer> read_buf_;
287 int read_buf_len_;
288 IPEndPoint* recv_from_address_;
290 // The buffer used by InternalWrite() to retry Write requests
291 scoped_refptr<IOBuffer> write_buf_;
292 int write_buf_len_;
293 scoped_ptr<IPEndPoint> send_to_address_;
295 // External callback; called when read is complete.
296 CompletionCallback read_callback_;
298 // External callback; called when write is complete.
299 CompletionCallback write_callback_;
301 BoundNetLog net_log_;
303 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent);
306 } // namespace net
308 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_