OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_SOCKET_TCP_SOCKET_WIN_H_ | 5 #ifndef NET_SOCKET_TCP_SOCKET_WIN_H_ |
6 #define NET_SOCKET_TCP_SOCKET_WIN_H_ | 6 #define NET_SOCKET_TCP_SOCKET_WIN_H_ |
7 | 7 |
8 #include <winsock2.h> | 8 #include <winsock2.h> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
15 #include "base/win/object_watcher.h" | 15 #include "base/win/object_watcher.h" |
16 #include "net/base/address_family.h" | 16 #include "net/base/address_family.h" |
17 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
| 18 #include "net/base/ip_endpoint.h" |
18 #include "net/base/net_export.h" | 19 #include "net/base/net_export.h" |
19 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
20 | 21 |
21 namespace net { | 22 namespace net { |
22 | 23 |
23 class IPEndPoint; | 24 class AddressList; |
| 25 class IOBuffer; |
24 | 26 |
25 // TODO(yzshen): This class is incomplete. TCP client operations (Connect/Read/ | |
26 // Write/etc.) will be added. And TCPClientSocket will be changed to be a | |
27 // wrapper around TCPSocket. | |
28 class NET_EXPORT TCPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe), | 27 class NET_EXPORT TCPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe), |
29 public base::win::ObjectWatcher::Delegate { | 28 public base::win::ObjectWatcher::Delegate { |
30 public: | 29 public: |
31 TCPSocketWin(NetLog* net_log, const NetLog::Source& source); | 30 TCPSocketWin(NetLog* net_log, const NetLog::Source& source); |
32 virtual ~TCPSocketWin(); | 31 virtual ~TCPSocketWin(); |
33 | 32 |
34 int Create(AddressFamily family); | 33 int Open(AddressFamily family); |
35 // Takes ownership of |socket|. | 34 // Takes ownership of |socket|. |
36 int Adopt(SOCKET socket); | 35 int AdoptConnectedSocket(SOCKET socket, const IPEndPoint& peer_address); |
37 // Returns a socket descriptor. The ownership is transferred to the caller. | 36 |
38 SOCKET Release(); | |
39 int Bind(const IPEndPoint& address); | 37 int Bind(const IPEndPoint& address); |
40 int GetLocalAddress(IPEndPoint* address) const; | 38 |
41 int Listen(int backlog); | 39 int Listen(int backlog); |
42 int Accept(scoped_ptr<TCPSocketWin>* socket, | 40 int Accept(scoped_ptr<TCPSocketWin>* socket, |
43 IPEndPoint* address, | 41 IPEndPoint* address, |
44 const CompletionCallback& callback); | 42 const CompletionCallback& callback); |
| 43 |
| 44 int Connect(const IPEndPoint& address, const CompletionCallback& callback); |
| 45 bool IsConnected() const; |
| 46 bool IsConnectedAndIdle() const; |
| 47 |
| 48 // Multiple outstanding requests are not supported. |
| 49 // Full duplex mode (reading and writing at the same time) is supported. |
| 50 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 51 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 52 |
| 53 int GetLocalAddress(IPEndPoint* address) const; |
| 54 int GetPeerAddress(IPEndPoint* address) const; |
| 55 |
| 56 // Sets various socket options. |
| 57 // The commonly used options for server listening sockets: |
| 58 // - SetExclusiveAddrUse(). |
45 int SetDefaultOptionsForServer(); | 59 int SetDefaultOptionsForServer(); |
| 60 // The commonly used options for client sockets and accepted sockets: |
| 61 // - Increase the socket buffer sizes for WinXP; |
| 62 // - SetNoDelay(true); |
| 63 // - SetKeepAlive(true, 45). |
| 64 int SetDefaultOptionsForClient(); |
46 int SetExclusiveAddrUse(); | 65 int SetExclusiveAddrUse(); |
| 66 bool SetReceiveBufferSize(int32 size); |
| 67 bool SetSendBufferSize(int32 size); |
| 68 bool SetKeepAlive(bool enable, int delay); |
| 69 bool SetNoDelay(bool no_delay); |
| 70 |
47 void Close(); | 71 void Close(); |
48 | 72 |
| 73 bool UsingTCPFastOpen() const; |
| 74 bool IsValid() const { return socket_ != INVALID_SOCKET; } |
| 75 |
| 76 // Marks the start/end of a series of connect attempts for logging purpose. |
| 77 // |
| 78 // TCPClientSocket may attempt to connect to multiple addresses until it |
| 79 // succeeds in establishing a connection. The corresponding log will have |
| 80 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a |
| 81 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of |
| 82 // NetLog::TYPE_TCP_CONNECT. |
| 83 // |
| 84 // TODO(yzshen): Change logging format and let TCPClientSocket log the |
| 85 // start/end of a series of connect attempts itself. |
| 86 void StartLoggingMultipleConnectAttempts(const AddressList& addresses); |
| 87 void EndLoggingMultipleConnectAttempts(int net_error); |
| 88 |
49 const BoundNetLog& net_log() const { return net_log_; } | 89 const BoundNetLog& net_log() const { return net_log_; } |
50 | 90 |
| 91 private: |
| 92 class Core; |
| 93 |
51 // base::ObjectWatcher::Delegate implementation. | 94 // base::ObjectWatcher::Delegate implementation. |
52 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; | 95 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
53 | 96 |
54 private: | |
55 int AcceptInternal(scoped_ptr<TCPSocketWin>* socket, | 97 int AcceptInternal(scoped_ptr<TCPSocketWin>* socket, |
56 IPEndPoint* address); | 98 IPEndPoint* address); |
57 | 99 |
| 100 int DoConnect(); |
| 101 void DoConnectComplete(int result); |
| 102 |
| 103 void LogConnectBegin(const AddressList& addresses); |
| 104 void LogConnectEnd(int net_error); |
| 105 |
| 106 int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 107 void DidCompleteConnect(); |
| 108 void DidCompleteWrite(); |
| 109 void DidSignalRead(); |
| 110 |
58 SOCKET socket_; | 111 SOCKET socket_; |
59 HANDLE socket_event_; | |
60 | 112 |
| 113 HANDLE accept_event_; |
61 base::win::ObjectWatcher accept_watcher_; | 114 base::win::ObjectWatcher accept_watcher_; |
62 | 115 |
63 scoped_ptr<TCPSocketWin>* accept_socket_; | 116 scoped_ptr<TCPSocketWin>* accept_socket_; |
64 IPEndPoint* accept_address_; | 117 IPEndPoint* accept_address_; |
65 CompletionCallback accept_callback_; | 118 CompletionCallback accept_callback_; |
66 | 119 |
| 120 // The various states that the socket could be in. |
| 121 bool waiting_connect_; |
| 122 bool waiting_read_; |
| 123 bool waiting_write_; |
| 124 |
| 125 // The core of the socket that can live longer than the socket itself. We pass |
| 126 // resources to the Windows async IO functions and we have to make sure that |
| 127 // they are not destroyed while the OS still references them. |
| 128 scoped_refptr<Core> core_; |
| 129 |
| 130 // External callback; called when connect or read is complete. |
| 131 CompletionCallback read_callback_; |
| 132 |
| 133 // External callback; called when write is complete. |
| 134 CompletionCallback write_callback_; |
| 135 |
| 136 IPEndPoint peer_address_; |
| 137 // The OS error that a connect attempt last completed with. |
| 138 int connect_os_error_; |
| 139 |
| 140 bool logging_multiple_connect_attempts_; |
| 141 |
67 BoundNetLog net_log_; | 142 BoundNetLog net_log_; |
68 | 143 |
69 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin); | 144 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin); |
70 }; | 145 }; |
71 | 146 |
72 } // namespace net | 147 } // namespace net |
73 | 148 |
74 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_ | 149 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_ |
| 150 |
OLD | NEW |