| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_WIN_H_ | 5 #ifndef NET_UDP_UDP_SOCKET_NACL_H_ |
| 6 #define NET_UDP_UDP_SOCKET_WIN_H_ | 6 #define NET_UDP_UDP_SOCKET_NACL_H_ |
| 7 | |
| 8 #include <winsock2.h> | |
| 9 | 7 |
| 10 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 12 #include "base/threading/non_thread_safe.h" | 11 #include "base/threading/non_thread_safe.h" |
| 13 #include "base/win/object_watcher.h" | |
| 14 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
| 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 16 #include "net/base/net_log.h" |
| 16 #include "net/base/rand_callback.h" | 17 #include "net/base/rand_callback.h" |
| 17 #include "net/base/ip_endpoint.h" | 18 #include "net/socket/socket_descriptor.h" |
| 18 #include "net/base/io_buffer.h" | |
| 19 #include "net/base/net_log.h" | |
| 20 #include "net/udp/datagram_socket.h" | 19 #include "net/udp/datagram_socket.h" |
| 21 | 20 |
| 22 namespace net { | 21 namespace net { |
| 23 | 22 |
| 24 class NET_EXPORT UDPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 23 class NET_EXPORT UDPSocketNacl : public base::NonThreadSafe { |
| 25 public: | 24 public: |
| 26 UDPSocketWin(DatagramSocket::BindType bind_type, | 25 UDPSocketNacl(DatagramSocket::BindType bind_type, |
| 27 const RandIntCallback& rand_int_cb, | 26 const RandIntCallback& rand_int_cb, |
| 28 net::NetLog* net_log, | 27 net::NetLog* net_log, |
| 29 const net::NetLog::Source& source); | 28 const net::NetLog::Source& source); |
| 30 virtual ~UDPSocketWin(); | 29 virtual ~UDPSocketNacl(); |
| 31 | 30 |
| 32 // Connect the socket to connect with a certain |address|. | 31 // Connect the socket to connect with a certain |address|. |
| 33 // Returns a net error code. | 32 // Returns a net error code. |
| 34 int Connect(const IPEndPoint& address); | 33 int Connect(const IPEndPoint& address); |
| 35 | 34 |
| 36 // Bind the address/port for this socket to |address|. This is generally | 35 // Bind the address/port for this socket to |address|. This is generally |
| 37 // only used on a server. | 36 // only used on a server. |
| 38 // Returns a net error code. | 37 // Returns a net error code. |
| 39 int Bind(const IPEndPoint& address); | 38 int Bind(const IPEndPoint& address); |
| 40 | 39 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 const IPEndPoint& address, | 93 const IPEndPoint& address, |
| 95 const CompletionCallback& callback); | 94 const CompletionCallback& callback); |
| 96 | 95 |
| 97 // Set the receive buffer size (in bytes) for the socket. | 96 // Set the receive buffer size (in bytes) for the socket. |
| 98 bool SetReceiveBufferSize(int32 size); | 97 bool SetReceiveBufferSize(int32 size); |
| 99 | 98 |
| 100 // Set the send buffer size (in bytes) for the socket. | 99 // Set the send buffer size (in bytes) for the socket. |
| 101 bool SetSendBufferSize(int32 size); | 100 bool SetSendBufferSize(int32 size); |
| 102 | 101 |
| 103 // Returns true if the socket is already connected or bound. | 102 // Returns true if the socket is already connected or bound. |
| 104 bool is_connected() const { return socket_ != INVALID_SOCKET; } | 103 bool is_connected() const { return false; } |
| 105 | 104 |
| 106 const BoundNetLog& NetLog() const { return net_log_; } | 105 const BoundNetLog& NetLog() const { return net_log_; } |
| 107 | 106 |
| 108 // Sets corresponding flags in |socket_options_| to allow the socket | 107 // Sets corresponding flags in |socket_options_| to allow the socket |
| 109 // to share the local address to which the socket will be bound with | 108 // to share the local address to which the socket will be bound with |
| 110 // other processes. Should be called before Bind(). | 109 // other processes. Should be called before Bind(). |
| 111 void AllowAddressReuse(); | 110 void AllowAddressReuse(); |
| 112 | 111 |
| 113 // Sets corresponding flags in |socket_options_| to allow sending | 112 // Sets corresponding flags in |socket_options_| to allow sending |
| 114 // and receiving packets to and from broadcast addresses. Should be | 113 // and receiving packets to and from broadcast addresses. Should be |
| (...skipping 18 matching lines...) Expand all Loading... |
| 133 // Set interface to use for multicast. If |interface_index| set to 0, default | 132 // Set interface to use for multicast. If |interface_index| set to 0, default |
| 134 // interface is used. | 133 // interface is used. |
| 135 // Should be called before Bind(). | 134 // Should be called before Bind(). |
| 136 // Returns a network error code. | 135 // Returns a network error code. |
| 137 int SetMulticastInterface(uint32 interface_index); | 136 int SetMulticastInterface(uint32 interface_index); |
| 138 | 137 |
| 139 // Set the time-to-live option for UDP packets sent to the multicast | 138 // Set the time-to-live option for UDP packets sent to the multicast |
| 140 // group address. The default value of this option is 1. | 139 // group address. The default value of this option is 1. |
| 141 // Cannot be negative or more than 255. | 140 // Cannot be negative or more than 255. |
| 142 // Should be called before Bind(). | 141 // Should be called before Bind(). |
| 142 // Return a network error code. |
| 143 int SetMulticastTimeToLive(int time_to_live); | 143 int SetMulticastTimeToLive(int time_to_live); |
| 144 | 144 |
| 145 // Set the loopback flag for UDP socket. If this flag is true, the host | 145 // Set the loopback flag for UDP socket. If this flag is true, the host |
| 146 // will receive packets sent to the joined group from itself. | 146 // will receive packets sent to the joined group from itself. |
| 147 // The default value of this option is true. | 147 // The default value of this option is true. |
| 148 // Should be called before Bind(). | 148 // Should be called before Bind(). |
| 149 // Return a network error code. |
| 149 // | 150 // |
| 150 // Note: the behavior of |SetMulticastLoopbackMode| is slightly | 151 // Note: the behavior of |SetMulticastLoopbackMode| is slightly |
| 151 // different between Windows and Unix-like systems. The inconsistency only | 152 // different between Windows and Unix-like systems. The inconsistency only |
| 152 // happens when there are more than one applications on the same host | 153 // happens when there are more than one applications on the same host |
| 153 // joined to the same multicast group while having different settings on | 154 // joined to the same multicast group while having different settings on |
| 154 // multicast loopback mode. On Windows, the applications with loopback off | 155 // multicast loopback mode. On Windows, the applications with loopback off |
| 155 // will not RECEIVE the loopback packets; while on Unix-like systems, the | 156 // will not RECEIVE the loopback packets; while on Unix-like systems, the |
| 156 // applications with loopback off will not SEND the loopback packets to | 157 // applications with loopback off will not SEND the loopback packets to |
| 157 // other applications on the same host. See MSDN: http://goo.gl/6vqbj | 158 // other applications on the same host. See MSDN: http://goo.gl/6vqbj |
| 158 int SetMulticastLoopbackMode(bool loopback); | 159 int SetMulticastLoopbackMode(bool loopback); |
| 159 | 160 |
| 160 // Set the differentiated services flags on outgoing packets. May not | 161 // Set the differentiated services flags on outgoing packets. May not |
| 161 // do anything on some platforms. | 162 // do anything on some platforms. |
| 163 // Return a network error code. |
| 162 int SetDiffServCodePoint(DiffServCodePoint dscp); | 164 int SetDiffServCodePoint(DiffServCodePoint dscp); |
| 163 | 165 |
| 164 // Resets the thread to be used for thread-safety checks. | 166 // Resets the thread to be used for thread-safety checks. |
| 165 void DetachFromThread(); | 167 void DetachFromThread(); |
| 166 | 168 |
| 167 private: | 169 private: |
| 168 enum SocketOptions { | |
| 169 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, | |
| 170 SOCKET_OPTION_BROADCAST = 1 << 1, | |
| 171 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2 | |
| 172 }; | |
| 173 | |
| 174 class Core; | |
| 175 | |
| 176 void DoReadCallback(int rv); | |
| 177 void DoWriteCallback(int rv); | |
| 178 void DidCompleteRead(); | |
| 179 void DidCompleteWrite(); | |
| 180 | |
| 181 // Handles stats and logging. |result| is the number of bytes transferred, on | |
| 182 // success, or the net error code on failure. LogRead retrieves the address | |
| 183 // from |recv_addr_storage_|, while LogWrite takes it as an optional argument. | |
| 184 void LogRead(int result, const char* bytes) const; | |
| 185 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; | |
| 186 | |
| 187 // Returns the OS error code (or 0 on success). | |
| 188 int CreateSocket(int addr_family); | |
| 189 | |
| 190 // Same as SendTo(), except that address is passed by pointer | |
| 191 // instead of by reference. It is called from Write() with |address| | |
| 192 // set to NULL. | |
| 193 int SendToOrWrite(IOBuffer* buf, | |
| 194 int buf_len, | |
| 195 const IPEndPoint* address, | |
| 196 const CompletionCallback& callback); | |
| 197 | |
| 198 int InternalConnect(const IPEndPoint& address); | |
| 199 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | |
| 200 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | |
| 201 | |
| 202 // Applies |socket_options_| to |socket_|. Should be called before | |
| 203 // Bind(). | |
| 204 int SetSocketOptions(); | |
| 205 int DoBind(const IPEndPoint& address); | |
| 206 // Binds to a random port on |address|. | |
| 207 int RandomBind(const IPAddressNumber& address); | |
| 208 | |
| 209 // Attempts to convert the data in |recv_addr_storage_| and |recv_addr_len_| | |
| 210 // to an IPEndPoint and writes it to |address|. Returns true on success. | |
| 211 bool ReceiveAddressToIPEndpoint(IPEndPoint* address) const; | |
| 212 | |
| 213 SOCKET socket_; | |
| 214 int addr_family_; | |
| 215 | |
| 216 // Bitwise-or'd combination of SocketOptions. Specifies the set of | |
| 217 // options that should be applied to |socket_| before Bind(). | |
| 218 int socket_options_; | |
| 219 | |
| 220 // Multicast interface. | |
| 221 uint32 multicast_interface_; | |
| 222 | |
| 223 // Multicast socket options cached for SetSocketOption. | |
| 224 // Cannot be used after Bind(). | |
| 225 int multicast_time_to_live_; | |
| 226 | |
| 227 // How to do source port binding, used only when UDPSocket is part of | |
| 228 // UDPClientSocket, since UDPServerSocket provides Bind. | |
| 229 DatagramSocket::BindType bind_type_; | |
| 230 | |
| 231 // PRNG function for generating port numbers. | |
| 232 RandIntCallback rand_int_cb_; | |
| 233 | |
| 234 // These are mutable since they're just cached copies to make | |
| 235 // GetPeerAddress/GetLocalAddress smarter. | |
| 236 mutable scoped_ptr<IPEndPoint> local_address_; | |
| 237 mutable scoped_ptr<IPEndPoint> remote_address_; | |
| 238 | |
| 239 // The core of the socket that can live longer than the socket itself. We pass | |
| 240 // resources to the Windows async IO functions and we have to make sure that | |
| 241 // they are not destroyed while the OS still references them. | |
| 242 scoped_refptr<Core> core_; | |
| 243 | |
| 244 IPEndPoint* recv_from_address_; | |
| 245 | |
| 246 // Cached copy of the current address we're sending to, if any. Used for | |
| 247 // logging. | |
| 248 scoped_ptr<IPEndPoint> send_to_address_; | |
| 249 | |
| 250 // External callback; called when read is complete. | |
| 251 CompletionCallback read_callback_; | |
| 252 | |
| 253 // External callback; called when write is complete. | |
| 254 CompletionCallback write_callback_; | |
| 255 | |
| 256 BoundNetLog net_log_; | 170 BoundNetLog net_log_; |
| 257 | 171 |
| 258 DISALLOW_COPY_AND_ASSIGN(UDPSocketWin); | 172 DISALLOW_COPY_AND_ASSIGN(UDPSocketNacl); |
| 259 }; | 173 }; |
| 260 | 174 |
| 261 } // namespace net | 175 } // namespace net |
| 262 | 176 |
| 263 #endif // NET_UDP_UDP_SOCKET_WIN_H_ | 177 #endif // NET_UDP_UDP_SOCKET_NACL_H_ |
| OLD | NEW |