| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 5 #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| 6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/threading/non_thread_safe.h" | 11 #include "base/threading/non_thread_safe.h" |
| 12 #include "net/base/address_family.h" |
| 12 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
| 13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 14 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
| 16 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
| 17 #include "net/base/rand_callback.h" | 18 #include "net/base/rand_callback.h" |
| 18 #include "net/socket/socket_descriptor.h" | 19 #include "net/socket/socket_descriptor.h" |
| 19 #include "net/udp/datagram_socket.h" | 20 #include "net/udp/datagram_socket.h" |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 | 23 |
| 23 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe { | 24 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe { |
| 24 public: | 25 public: |
| 25 UDPSocketLibevent(DatagramSocket::BindType bind_type, | 26 UDPSocketLibevent(DatagramSocket::BindType bind_type, |
| 26 const RandIntCallback& rand_int_cb, | 27 const RandIntCallback& rand_int_cb, |
| 27 net::NetLog* net_log, | 28 net::NetLog* net_log, |
| 28 const net::NetLog::Source& source); | 29 const net::NetLog::Source& source); |
| 29 virtual ~UDPSocketLibevent(); | 30 virtual ~UDPSocketLibevent(); |
| 30 | 31 |
| 31 // Connect the socket to connect with a certain |address|. | 32 // Opens the socket. |
| 33 // Returns a net error code. |
| 34 int Open(AddressFamily address_family); |
| 35 |
| 36 // Connects the socket to connect with a certain |address|. |
| 37 // Should be called after Open(). |
| 32 // Returns a net error code. | 38 // Returns a net error code. |
| 33 int Connect(const IPEndPoint& address); | 39 int Connect(const IPEndPoint& address); |
| 34 | 40 |
| 35 // Bind the address/port for this socket to |address|. This is generally | 41 // Binds the address/port for this socket to |address|. This is generally |
| 36 // only used on a server. | 42 // only used on a server. Should be called after Open(). |
| 37 // Returns a net error code. | 43 // Returns a net error code. |
| 38 int Bind(const IPEndPoint& address); | 44 int Bind(const IPEndPoint& address); |
| 39 | 45 |
| 40 // Close the socket. | 46 // Closes the socket. |
| 47 // TODO(rvargas, hidehiko): Disallow re-Open() after Close(). |
| 41 void Close(); | 48 void Close(); |
| 42 | 49 |
| 43 // Copy the remote udp address into |address| and return a network error code. | 50 // Copies the remote udp address into |address| and returns a net error code. |
| 44 int GetPeerAddress(IPEndPoint* address) const; | 51 int GetPeerAddress(IPEndPoint* address) const; |
| 45 | 52 |
| 46 // Copy the local udp address into |address| and return a network error code. | 53 // Copies the local udp address into |address| and returns a net error code. |
| 47 // (similar to getsockname) | 54 // (similar to getsockname) |
| 48 int GetLocalAddress(IPEndPoint* address) const; | 55 int GetLocalAddress(IPEndPoint* address) const; |
| 49 | 56 |
| 50 // IO: | 57 // IO: |
| 51 // Multiple outstanding read requests are not supported. | 58 // Multiple outstanding read requests are not supported. |
| 52 // Full duplex mode (reading and writing at the same time) is supported | 59 // Full duplex mode (reading and writing at the same time) is supported |
| 53 | 60 |
| 54 // Read from the socket. | 61 // Reads from the socket. |
| 55 // Only usable from the client-side of a UDP socket, after the socket | 62 // Only usable from the client-side of a UDP socket, after the socket |
| 56 // has been connected. | 63 // has been connected. |
| 57 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 64 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 58 | 65 |
| 59 // Write to the socket. | 66 // Writes to the socket. |
| 60 // Only usable from the client-side of a UDP socket, after the socket | 67 // Only usable from the client-side of a UDP socket, after the socket |
| 61 // has been connected. | 68 // has been connected. |
| 62 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 69 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 63 | 70 |
| 64 // Read from a socket and receive sender address information. | 71 // Reads from a socket and receive sender address information. |
| 65 // |buf| is the buffer to read data into. | 72 // |buf| is the buffer to read data into. |
| 66 // |buf_len| is the maximum amount of data to read. | 73 // |buf_len| is the maximum amount of data to read. |
| 67 // |address| is a buffer provided by the caller for receiving the sender | 74 // |address| is a buffer provided by the caller for receiving the sender |
| 68 // address information about the received data. This buffer must be kept | 75 // address information about the received data. This buffer must be kept |
| 69 // alive by the caller until the callback is placed. | 76 // alive by the caller until the callback is placed. |
| 70 // |address_length| is a ptr to the length of the |address| buffer. This | 77 // |address_length| is a ptr to the length of the |address| buffer. This |
| 71 // is an input parameter containing the maximum size |address| can hold | 78 // is an input parameter containing the maximum size |address| can hold |
| 72 // and also an output parameter for the size of |address| upon completion. | 79 // and also an output parameter for the size of |address| upon completion. |
| 73 // |callback| is the callback on completion of the RecvFrom. | 80 // |callback| is the callback on completion of the RecvFrom. |
| 74 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | 81 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
| 75 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, | 82 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, |
| 76 // and |address_length| alive until the callback is called. | 83 // and |address_length| alive until the callback is called. |
| 77 int RecvFrom(IOBuffer* buf, | 84 int RecvFrom(IOBuffer* buf, |
| 78 int buf_len, | 85 int buf_len, |
| 79 IPEndPoint* address, | 86 IPEndPoint* address, |
| 80 const CompletionCallback& callback); | 87 const CompletionCallback& callback); |
| 81 | 88 |
| 82 // Send to a socket with a particular destination. | 89 // Sends to a socket with a particular destination. |
| 83 // |buf| is the buffer to send | 90 // |buf| is the buffer to send |
| 84 // |buf_len| is the number of bytes to send | 91 // |buf_len| is the number of bytes to send |
| 85 // |address| is the recipient address. | 92 // |address| is the recipient address. |
| 86 // |address_length| is the size of the recipient address | 93 // |address_length| is the size of the recipient address |
| 87 // |callback| is the user callback function to call on complete. | 94 // |callback| is the user callback function to call on complete. |
| 88 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | 95 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
| 89 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| | 96 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| |
| 90 // alive until the callback is called. | 97 // alive until the callback is called. |
| 91 int SendTo(IOBuffer* buf, | 98 int SendTo(IOBuffer* buf, |
| 92 int buf_len, | 99 int buf_len, |
| 93 const IPEndPoint& address, | 100 const IPEndPoint& address, |
| 94 const CompletionCallback& callback); | 101 const CompletionCallback& callback); |
| 95 | 102 |
| 96 // Set the receive buffer size (in bytes) for the socket. | 103 // Sets the receive buffer size (in bytes) for the socket. |
| 104 // Returns a net error code. |
| 97 int SetReceiveBufferSize(int32 size); | 105 int SetReceiveBufferSize(int32 size); |
| 98 | 106 |
| 99 // Set the send buffer size (in bytes) for the socket. | 107 // Sets the send buffer size (in bytes) for the socket. |
| 108 // Returns a net error code. |
| 100 int SetSendBufferSize(int32 size); | 109 int SetSendBufferSize(int32 size); |
| 101 | 110 |
| 102 // Returns true if the socket is already connected or bound. | 111 // Returns true if the socket is already connected or bound. |
| 103 bool is_connected() const { return socket_ != kInvalidSocket; } | 112 bool is_connected() const { return is_connected_; } |
| 104 | 113 |
| 105 const BoundNetLog& NetLog() const { return net_log_; } | 114 const BoundNetLog& NetLog() const { return net_log_; } |
| 106 | 115 |
| 107 // Sets corresponding flags in |socket_options_| to allow the socket | 116 // Sets corresponding flags in |socket_options_| to allow the socket |
| 108 // to share the local address to which the socket will be bound with | 117 // to share the local address to which the socket will be bound with |
| 109 // other processes. Should be called before Bind(). | 118 // other processes. Should be called between Open() and Bind(). |
| 110 void AllowAddressReuse(); | 119 // Returns a net error code. |
| 120 int AllowAddressReuse(); |
| 111 | 121 |
| 112 // Sets corresponding flags in |socket_options_| to allow sending | 122 // Sets corresponding flags in |socket_options_| to allow or disallow sending |
| 113 // and receiving packets to and from broadcast addresses. Should be | 123 // and receiving packets to and from broadcast addresses. |
| 114 // called before Bind(). | 124 // Returns a net error code. |
| 115 void AllowBroadcast(); | 125 int SetBroadcast(bool broadcast); |
| 116 | 126 |
| 117 // Join the multicast group. | 127 // Joins the multicast group. |
| 118 // |group_address| is the group address to join, could be either | 128 // |group_address| is the group address to join, could be either |
| 119 // an IPv4 or IPv6 address. | 129 // an IPv4 or IPv6 address. |
| 120 // Return a network error code. | 130 // Returns a net error code. |
| 121 int JoinGroup(const IPAddressNumber& group_address) const; | 131 int JoinGroup(const IPAddressNumber& group_address) const; |
| 122 | 132 |
| 123 // Leave the multicast group. | 133 // Leaves the multicast group. |
| 124 // |group_address| is the group address to leave, could be either | 134 // |group_address| is the group address to leave, could be either |
| 125 // an IPv4 or IPv6 address. If the socket hasn't joined the group, | 135 // an IPv4 or IPv6 address. If the socket hasn't joined the group, |
| 126 // it will be ignored. | 136 // it will be ignored. |
| 127 // It's optional to leave the multicast group before destroying | 137 // It's optional to leave the multicast group before destroying |
| 128 // the socket. It will be done by the OS. | 138 // the socket. It will be done by the OS. |
| 129 // Return a network error code. | 139 // Returns a net error code. |
| 130 int LeaveGroup(const IPAddressNumber& group_address) const; | 140 int LeaveGroup(const IPAddressNumber& group_address) const; |
| 131 | 141 |
| 132 // Set interface to use for multicast. If |interface_index| set to 0, default | 142 // Sets interface to use for multicast. If |interface_index| set to 0, |
| 133 // interface is used. | 143 // default interface is used. |
| 134 // Should be called before Bind(). | 144 // Should be called before Bind(). |
| 135 // Returns a network error code. | 145 // Returns a net error code. |
| 136 int SetMulticastInterface(uint32 interface_index); | 146 int SetMulticastInterface(uint32 interface_index); |
| 137 | 147 |
| 138 // Set the time-to-live option for UDP packets sent to the multicast | 148 // Sets the time-to-live option for UDP packets sent to the multicast |
| 139 // group address. The default value of this option is 1. | 149 // group address. The default value of this option is 1. |
| 140 // Cannot be negative or more than 255. | 150 // Cannot be negative or more than 255. |
| 141 // Should be called before Bind(). | 151 // Should be called before Bind(). |
| 142 // Return a network error code. | 152 // Returns a net error code. |
| 143 int SetMulticastTimeToLive(int time_to_live); | 153 int SetMulticastTimeToLive(int time_to_live); |
| 144 | 154 |
| 145 // Set the loopback flag for UDP socket. If this flag is true, the host | 155 // Sets the loopback flag for UDP socket. If this flag is true, the host |
| 146 // will receive packets sent to the joined group from itself. | 156 // will receive packets sent to the joined group from itself. |
| 147 // The default value of this option is true. | 157 // The default value of this option is true. |
| 148 // Should be called before Bind(). | 158 // Should be called before Bind(). |
| 149 // Return a network error code. | 159 // Returns a net error code. |
| 150 // | 160 // |
| 151 // Note: the behavior of |SetMulticastLoopbackMode| is slightly | 161 // Note: the behavior of |SetMulticastLoopbackMode| is slightly |
| 152 // different between Windows and Unix-like systems. The inconsistency only | 162 // different between Windows and Unix-like systems. The inconsistency only |
| 153 // happens when there are more than one applications on the same host | 163 // happens when there are more than one applications on the same host |
| 154 // joined to the same multicast group while having different settings on | 164 // joined to the same multicast group while having different settings on |
| 155 // multicast loopback mode. On Windows, the applications with loopback off | 165 // multicast loopback mode. On Windows, the applications with loopback off |
| 156 // will not RECEIVE the loopback packets; while on Unix-like systems, the | 166 // will not RECEIVE the loopback packets; while on Unix-like systems, the |
| 157 // applications with loopback off will not SEND the loopback packets to | 167 // applications with loopback off will not SEND the loopback packets to |
| 158 // other applications on the same host. See MSDN: http://goo.gl/6vqbj | 168 // other applications on the same host. See MSDN: http://goo.gl/6vqbj |
| 159 int SetMulticastLoopbackMode(bool loopback); | 169 int SetMulticastLoopbackMode(bool loopback); |
| 160 | 170 |
| 161 // Set the differentiated services flags on outgoing packets. May not | 171 // Sets the differentiated services flags on outgoing packets. May not |
| 162 // do anything on some platforms. | 172 // do anything on some platforms. |
| 163 // Return a network error code. | 173 // Returns a net error code. |
| 164 int SetDiffServCodePoint(DiffServCodePoint dscp); | 174 int SetDiffServCodePoint(DiffServCodePoint dscp); |
| 165 | 175 |
| 166 // Resets the thread to be used for thread-safety checks. | 176 // Resets the thread to be used for thread-safety checks. |
| 167 void DetachFromThread(); | 177 void DetachFromThread(); |
| 168 | 178 |
| 169 private: | 179 private: |
| 170 enum SocketOptions { | 180 enum SocketOptions { |
| 171 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, | 181 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 |
| 172 SOCKET_OPTION_BROADCAST = 1 << 1, | |
| 173 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2 | |
| 174 }; | 182 }; |
| 175 | 183 |
| 176 class ReadWatcher : public base::MessageLoopForIO::Watcher { | 184 class ReadWatcher : public base::MessageLoopForIO::Watcher { |
| 177 public: | 185 public: |
| 178 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 186 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
| 179 | 187 |
| 180 // MessageLoopForIO::Watcher methods | 188 // MessageLoopForIO::Watcher methods |
| 181 | 189 |
| 182 void OnFileCanReadWithoutBlocking(int /* fd */) override; | 190 void OnFileCanReadWithoutBlocking(int /* fd */) override; |
| 183 | 191 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 211 void DidCompleteWrite(); | 219 void DidCompleteWrite(); |
| 212 | 220 |
| 213 // Handles stats and logging. |result| is the number of bytes transferred, on | 221 // Handles stats and logging. |result| is the number of bytes transferred, on |
| 214 // success, or the net error code on failure. On success, LogRead takes in a | 222 // success, or the net error code on failure. On success, LogRead takes in a |
| 215 // sockaddr and its length, which are mandatory, while LogWrite takes in an | 223 // sockaddr and its length, which are mandatory, while LogWrite takes in an |
| 216 // optional IPEndPoint. | 224 // optional IPEndPoint. |
| 217 void LogRead(int result, const char* bytes, socklen_t addr_len, | 225 void LogRead(int result, const char* bytes, socklen_t addr_len, |
| 218 const sockaddr* addr) const; | 226 const sockaddr* addr) const; |
| 219 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; | 227 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; |
| 220 | 228 |
| 221 // Returns the OS error code (or 0 on success). | |
| 222 int CreateSocket(int addr_family); | |
| 223 | |
| 224 // Same as SendTo(), except that address is passed by pointer | 229 // Same as SendTo(), except that address is passed by pointer |
| 225 // instead of by reference. It is called from Write() with |address| | 230 // instead of by reference. It is called from Write() with |address| |
| 226 // set to NULL. | 231 // set to NULL. |
| 227 int SendToOrWrite(IOBuffer* buf, | 232 int SendToOrWrite(IOBuffer* buf, |
| 228 int buf_len, | 233 int buf_len, |
| 229 const IPEndPoint* address, | 234 const IPEndPoint* address, |
| 230 const CompletionCallback& callback); | 235 const CompletionCallback& callback); |
| 231 | 236 |
| 232 int InternalConnect(const IPEndPoint& address); | 237 int InternalConnect(const IPEndPoint& address); |
| 233 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | 238 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
| 234 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | 239 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
| 235 | 240 |
| 236 // Applies |socket_options_| to |socket_|. Should be called before | 241 // Applies |socket_options_| to |socket_|. Should be called before |
| 237 // Bind(). | 242 // Bind(). |
| 238 int SetSocketOptions(); | 243 int SetMulticastOptions(); |
| 239 int DoBind(const IPEndPoint& address); | 244 int DoBind(const IPEndPoint& address); |
| 240 // Binds to a random port on |address|. | 245 // Binds to a random port on |address|. |
| 241 int RandomBind(const IPAddressNumber& address); | 246 int RandomBind(const IPAddressNumber& address); |
| 242 | 247 |
| 243 int socket_; | 248 int socket_; |
| 244 int addr_family_; | 249 int addr_family_; |
| 250 bool is_connected_; |
| 245 | 251 |
| 246 // Bitwise-or'd combination of SocketOptions. Specifies the set of | 252 // Bitwise-or'd combination of SocketOptions. Specifies the set of |
| 247 // options that should be applied to |socket_| before Bind(). | 253 // options that should be applied to |socket_| before Bind(). |
| 248 int socket_options_; | 254 int socket_options_; |
| 249 | 255 |
| 250 // Multicast interface. | 256 // Multicast interface. |
| 251 uint32 multicast_interface_; | 257 uint32 multicast_interface_; |
| 252 | 258 |
| 253 // Multicast socket options cached for SetSocketOption. | 259 // Multicast socket options cached for SetMulticastOption. |
| 254 // Cannot be used after Bind(). | 260 // Cannot be used after Bind(). |
| 255 int multicast_time_to_live_; | 261 int multicast_time_to_live_; |
| 256 | 262 |
| 257 // How to do source port binding, used only when UDPSocket is part of | 263 // How to do source port binding, used only when UDPSocket is part of |
| 258 // UDPClientSocket, since UDPServerSocket provides Bind. | 264 // UDPClientSocket, since UDPServerSocket provides Bind. |
| 259 DatagramSocket::BindType bind_type_; | 265 DatagramSocket::BindType bind_type_; |
| 260 | 266 |
| 261 // PRNG function for generating port numbers. | 267 // PRNG function for generating port numbers. |
| 262 RandIntCallback rand_int_cb_; | 268 RandIntCallback rand_int_cb_; |
| 263 | 269 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 291 CompletionCallback write_callback_; | 297 CompletionCallback write_callback_; |
| 292 | 298 |
| 293 BoundNetLog net_log_; | 299 BoundNetLog net_log_; |
| 294 | 300 |
| 295 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | 301 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
| 296 }; | 302 }; |
| 297 | 303 |
| 298 } // namespace net | 304 } // namespace net |
| 299 | 305 |
| 300 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 306 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| OLD | NEW |