| 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_WIN_H_ | 5 #ifndef NET_UDP_UDP_SOCKET_WIN_H_ |
| 6 #define NET_UDP_UDP_SOCKET_WIN_H_ | 6 #define NET_UDP_UDP_SOCKET_WIN_H_ |
| 7 | 7 |
| 8 #include <qos2.h> | 8 #include <qos2.h> |
| 9 #include <winsock2.h> | 9 #include <winsock2.h> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "base/win/object_watcher.h" | 14 #include "base/win/object_watcher.h" |
| 15 #include "base/win/scoped_handle.h" |
| 15 #include "net/base/address_family.h" | 16 #include "net/base/address_family.h" |
| 16 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
| 17 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
| 18 #include "net/base/rand_callback.h" | 19 #include "net/base/rand_callback.h" |
| 19 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
| 21 #include "net/base/net_log.h" | 22 #include "net/base/net_log.h" |
| 22 #include "net/udp/datagram_socket.h" | 23 #include "net/udp/datagram_socket.h" |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 | 26 |
| 26 class NET_EXPORT UDPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 27 class NET_EXPORT UDPSocketWin |
| 28 : NON_EXPORTED_BASE(public base::NonThreadSafe), |
| 29 NON_EXPORTED_BASE(public base::win::ObjectWatcher::Delegate) { |
| 27 public: | 30 public: |
| 28 UDPSocketWin(DatagramSocket::BindType bind_type, | 31 UDPSocketWin(DatagramSocket::BindType bind_type, |
| 29 const RandIntCallback& rand_int_cb, | 32 const RandIntCallback& rand_int_cb, |
| 30 net::NetLog* net_log, | 33 net::NetLog* net_log, |
| 31 const net::NetLog::Source& source); | 34 const net::NetLog::Source& source); |
| 32 virtual ~UDPSocketWin(); | 35 virtual ~UDPSocketWin(); |
| 33 | 36 |
| 34 // Opens the socket. | 37 // Opens the socket. |
| 35 // Returns a net error code. | 38 // Returns a net error code. |
| 36 int Open(AddressFamily address_family); | 39 int Open(AddressFamily address_family); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // other applications on the same host. See MSDN: http://goo.gl/6vqbj | 170 // other applications on the same host. See MSDN: http://goo.gl/6vqbj |
| 168 int SetMulticastLoopbackMode(bool loopback); | 171 int SetMulticastLoopbackMode(bool loopback); |
| 169 | 172 |
| 170 // Sets the differentiated services flags on outgoing packets. May not | 173 // Sets the differentiated services flags on outgoing packets. May not |
| 171 // do anything on some platforms. | 174 // do anything on some platforms. |
| 172 int SetDiffServCodePoint(DiffServCodePoint dscp); | 175 int SetDiffServCodePoint(DiffServCodePoint dscp); |
| 173 | 176 |
| 174 // Resets the thread to be used for thread-safety checks. | 177 // Resets the thread to be used for thread-safety checks. |
| 175 void DetachFromThread(); | 178 void DetachFromThread(); |
| 176 | 179 |
| 180 // This class by default uses overlapped IO. Call this method before Open() |
| 181 // to switch to non-blocking IO. |
| 182 void UseNonBlockingIO(); |
| 183 |
| 177 private: | 184 private: |
| 178 enum SocketOptions { | 185 enum SocketOptions { |
| 179 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 | 186 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 |
| 180 }; | 187 }; |
| 181 | 188 |
| 182 class Core; | 189 class Core; |
| 183 | 190 |
| 184 void DoReadCallback(int rv); | 191 void DoReadCallback(int rv); |
| 185 void DoWriteCallback(int rv); | 192 void DoWriteCallback(int rv); |
| 193 |
| 186 void DidCompleteRead(); | 194 void DidCompleteRead(); |
| 187 void DidCompleteWrite(); | 195 void DidCompleteWrite(); |
| 188 | 196 |
| 197 // base::ObjectWatcher::Delegate implementation. |
| 198 virtual void OnObjectSignaled(HANDLE object); |
| 199 void OnReadSignaled(); |
| 200 void OnWriteSignaled(); |
| 201 |
| 202 void WatchForReadWrite(); |
| 203 |
| 189 // Handles stats and logging. |result| is the number of bytes transferred, on | 204 // Handles stats and logging. |result| is the number of bytes transferred, on |
| 190 // success, or the net error code on failure. LogRead retrieves the address | 205 // success, or the net error code on failure. |
| 191 // from |recv_addr_storage_|, while LogWrite takes it as an optional argument. | 206 void LogRead(int result, const char* bytes, const IPEndPoint* address) const; |
| 192 void LogRead(int result, const char* bytes) const; | |
| 193 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; | 207 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; |
| 194 | 208 |
| 195 // Same as SendTo(), except that address is passed by pointer | 209 // Same as SendTo(), except that address is passed by pointer |
| 196 // instead of by reference. It is called from Write() with |address| | 210 // instead of by reference. It is called from Write() with |address| |
| 197 // set to NULL. | 211 // set to NULL. |
| 198 int SendToOrWrite(IOBuffer* buf, | 212 int SendToOrWrite(IOBuffer* buf, |
| 199 int buf_len, | 213 int buf_len, |
| 200 const IPEndPoint* address, | 214 const IPEndPoint* address, |
| 201 const CompletionCallback& callback); | 215 const CompletionCallback& callback); |
| 202 | 216 |
| 203 int InternalConnect(const IPEndPoint& address); | 217 int InternalConnect(const IPEndPoint& address); |
| 204 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | 218 |
| 205 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | 219 // Version for using overlapped IO. |
| 220 int InternalRecvFromOverlapped(IOBuffer* buf, |
| 221 int buf_len, |
| 222 IPEndPoint* address); |
| 223 int InternalSendToOverlapped(IOBuffer* buf, |
| 224 int buf_len, |
| 225 const IPEndPoint* address); |
| 226 |
| 227 // Version for using non-blocking IO. |
| 228 int InternalRecvFromNonBlocking(IOBuffer* buf, |
| 229 int buf_len, |
| 230 IPEndPoint* address); |
| 231 int InternalSendToNonBlocking(IOBuffer* buf, |
| 232 int buf_len, |
| 233 const IPEndPoint* address); |
| 206 | 234 |
| 207 // Applies |socket_options_| to |socket_|. Should be called before | 235 // Applies |socket_options_| to |socket_|. Should be called before |
| 208 // Bind(). | 236 // Bind(). |
| 209 int SetMulticastOptions(); | 237 int SetMulticastOptions(); |
| 210 int DoBind(const IPEndPoint& address); | 238 int DoBind(const IPEndPoint& address); |
| 211 // Binds to a random port on |address|. | 239 // Binds to a random port on |address|. |
| 212 int RandomBind(const IPAddressNumber& address); | 240 int RandomBind(const IPAddressNumber& address); |
| 213 | 241 |
| 214 // Attempts to convert the data in |recv_addr_storage_| and |recv_addr_len_| | |
| 215 // to an IPEndPoint and writes it to |address|. Returns true on success. | |
| 216 bool ReceiveAddressToIPEndpoint(IPEndPoint* address) const; | |
| 217 | |
| 218 SOCKET socket_; | 242 SOCKET socket_; |
| 219 int addr_family_; | 243 int addr_family_; |
| 220 bool is_connected_; | 244 bool is_connected_; |
| 221 | 245 |
| 222 // Bitwise-or'd combination of SocketOptions. Specifies the set of | 246 // Bitwise-or'd combination of SocketOptions. Specifies the set of |
| 223 // options that should be applied to |socket_| before Bind(). | 247 // options that should be applied to |socket_| before Bind(). |
| 224 int socket_options_; | 248 int socket_options_; |
| 225 | 249 |
| 226 // Multicast interface. | 250 // Multicast interface. |
| 227 uint32 multicast_interface_; | 251 uint32 multicast_interface_; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 240 // These are mutable since they're just cached copies to make | 264 // These are mutable since they're just cached copies to make |
| 241 // GetPeerAddress/GetLocalAddress smarter. | 265 // GetPeerAddress/GetLocalAddress smarter. |
| 242 mutable scoped_ptr<IPEndPoint> local_address_; | 266 mutable scoped_ptr<IPEndPoint> local_address_; |
| 243 mutable scoped_ptr<IPEndPoint> remote_address_; | 267 mutable scoped_ptr<IPEndPoint> remote_address_; |
| 244 | 268 |
| 245 // The core of the socket that can live longer than the socket itself. We pass | 269 // The core of the socket that can live longer than the socket itself. We pass |
| 246 // resources to the Windows async IO functions and we have to make sure that | 270 // resources to the Windows async IO functions and we have to make sure that |
| 247 // they are not destroyed while the OS still references them. | 271 // they are not destroyed while the OS still references them. |
| 248 scoped_refptr<Core> core_; | 272 scoped_refptr<Core> core_; |
| 249 | 273 |
| 274 // True if non-blocking IO is used. |
| 275 bool use_non_blocking_io_; |
| 276 |
| 277 // Watches |read_write_event_|. |
| 278 base::win::ObjectWatcher read_write_watcher_; |
| 279 |
| 280 // Events for read and write. |
| 281 base::win::ScopedHandle read_write_event_; |
| 282 |
| 283 // The buffers used in Read() and Write(). |
| 284 scoped_refptr<IOBuffer> read_iobuffer_; |
| 285 scoped_refptr<IOBuffer> write_iobuffer_; |
| 286 |
| 287 int read_iobuffer_len_; |
| 288 int write_iobuffer_len_; |
| 289 |
| 250 IPEndPoint* recv_from_address_; | 290 IPEndPoint* recv_from_address_; |
| 251 | 291 |
| 252 // Cached copy of the current address we're sending to, if any. Used for | 292 // Cached copy of the current address we're sending to, if any. Used for |
| 253 // logging. | 293 // logging. |
| 254 scoped_ptr<IPEndPoint> send_to_address_; | 294 scoped_ptr<IPEndPoint> send_to_address_; |
| 255 | 295 |
| 256 // External callback; called when read is complete. | 296 // External callback; called when read is complete. |
| 257 CompletionCallback read_callback_; | 297 CompletionCallback read_callback_; |
| 258 | 298 |
| 259 // External callback; called when write is complete. | 299 // External callback; called when write is complete. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 SetFlowFn set_flow_func_; | 361 SetFlowFn set_flow_func_; |
| 322 | 362 |
| 323 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake); | 363 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake); |
| 324 DISALLOW_COPY_AND_ASSIGN(QwaveAPI); | 364 DISALLOW_COPY_AND_ASSIGN(QwaveAPI); |
| 325 }; | 365 }; |
| 326 | 366 |
| 327 | 367 |
| 328 } // namespace net | 368 } // namespace net |
| 329 | 369 |
| 330 #endif // NET_UDP_UDP_SOCKET_WIN_H_ | 370 #endif // NET_UDP_UDP_SOCKET_WIN_H_ |
| OLD | NEW |