Chromium Code Reviews| 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 Create(AddressFamily family); |
|
wtc
2013/09/06 21:55:42
"Open" is a better name. "Create" is usually a met
yzshen1
2013/09/09 23:14:48
I slightly prefer Create(). The document of OS API
wtc
2013/09/12 01:27:16
The socket() function is more similar to a static
yzshen1
2013/09/12 20:07:50
Done. I have changed the name to Open.
On 2013/09
| |
| 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 int GetLocalAddress(IPEndPoint* address) const; |
| 39 | |
| 41 int Listen(int backlog); | 40 int Listen(int backlog); |
| 42 int Accept(scoped_ptr<TCPSocketWin>* socket, | 41 int Accept(scoped_ptr<TCPSocketWin>* socket, |
| 43 IPEndPoint* address, | 42 IPEndPoint* address, |
| 44 const CompletionCallback& callback); | 43 const CompletionCallback& callback); |
| 44 | |
| 45 int Connect(const IPEndPoint& address, const CompletionCallback& callback); | |
| 46 bool IsConnected() const; | |
| 47 bool IsConnectedAndIdle() const; | |
| 48 int GetPeerAddress(IPEndPoint* address) const; | |
|
wtc
2013/09/06 21:55:42
I suggest declaring GetPeerAddress next to the rel
yzshen1
2013/09/09 23:14:48
Done.
| |
| 49 | |
| 50 // Multiple outstanding requests are not supported. | |
| 51 // Full duplex mode (reading and writing at the same time) is supported. | |
| 52 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
| 53 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
| 54 | |
| 45 int SetDefaultOptionsForServer(); | 55 int SetDefaultOptionsForServer(); |
| 56 int SetDefaultOptionsForClient(); | |
| 46 int SetExclusiveAddrUse(); | 57 int SetExclusiveAddrUse(); |
| 58 bool SetReceiveBufferSize(int32 size); | |
| 59 bool SetSendBufferSize(int32 size); | |
| 60 bool SetKeepAlive(bool enable, int delay); | |
| 61 bool SetNoDelay(bool no_delay); | |
| 62 | |
| 47 void Close(); | 63 void Close(); |
| 48 | 64 |
| 65 bool UsingTCPFastOpen() const; | |
| 66 bool IsValid() const { return socket_ != INVALID_SOCKET; } | |
|
wtc
2013/09/06 21:55:42
You can consider renaming this method "IsOpen".
yzshen1
2013/09/09 23:14:48
Please see my comment about Create(). Thanks!
On
| |
| 67 | |
| 68 // Marks the start/end of a series of connect attempts for logging purpose. | |
| 69 // | |
| 70 // TCPClientSocket may attempt to connect to multiple addresses until it | |
|
wtc
2013/09/06 21:55:42
TCPClientSocket => TCPSocket
or did you really me
yzshen1
2013/09/09 23:14:48
I did really mean TCPClientSocket. I agree that it
| |
| 71 // succeeds in establishing a connection. The corresponding log will be | |
|
wtc
2013/09/06 21:55:42
Nit: will be => will have ?
yzshen1
2013/09/09 23:14:48
Done.
| |
| 72 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a | |
| 73 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of | |
| 74 // NetLog::TYPE_TCP_CONNECT. | |
| 75 void StartLoggingMultipleConnectAttempts(const AddressList& addresses); | |
| 76 void EndLoggingMultipleConnectAttempts(int net_error); | |
| 77 | |
| 49 const BoundNetLog& net_log() const { return net_log_; } | 78 const BoundNetLog& net_log() const { return net_log_; } |
| 50 | 79 |
| 80 private: | |
| 81 class Core; | |
| 82 | |
| 51 // base::ObjectWatcher::Delegate implementation. | 83 // base::ObjectWatcher::Delegate implementation. |
| 52 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; | 84 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
| 53 | 85 |
| 54 private: | |
| 55 int AcceptInternal(scoped_ptr<TCPSocketWin>* socket, | 86 int AcceptInternal(scoped_ptr<TCPSocketWin>* socket, |
| 56 IPEndPoint* address); | 87 IPEndPoint* address); |
| 57 | 88 |
| 89 int DoConnect(); | |
| 90 void DoConnectComplete(int result); | |
| 91 | |
| 92 void LogConnectStart(const AddressList& addresses); | |
| 93 void LogConnectCompletion(int net_error); | |
| 94 | |
| 95 int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
| 96 void DoReadCallback(int rv); | |
| 97 void DoWriteCallback(int rv); | |
| 98 void DidCompleteConnect(); | |
| 99 void DidCompleteWrite(); | |
| 100 void DidSignalRead(); | |
| 101 | |
| 58 SOCKET socket_; | 102 SOCKET socket_; |
| 59 HANDLE socket_event_; | |
| 60 | 103 |
| 104 HANDLE accept_event_; | |
| 61 base::win::ObjectWatcher accept_watcher_; | 105 base::win::ObjectWatcher accept_watcher_; |
| 62 | 106 |
| 63 scoped_ptr<TCPSocketWin>* accept_socket_; | 107 scoped_ptr<TCPSocketWin>* accept_socket_; |
| 64 IPEndPoint* accept_address_; | 108 IPEndPoint* accept_address_; |
| 65 CompletionCallback accept_callback_; | 109 CompletionCallback accept_callback_; |
| 66 | 110 |
| 111 // The various states that the socket could be in. | |
| 112 bool waiting_connect_; | |
| 113 bool waiting_read_; | |
| 114 bool waiting_write_; | |
| 115 | |
| 116 // The core of the socket that can live longer than the socket itself. We pass | |
| 117 // resources to the Windows async IO functions and we have to make sure that | |
| 118 // they are not destroyed while the OS still references them. | |
| 119 scoped_refptr<Core> core_; | |
| 120 | |
| 121 // External callback; called when connect or read is complete. | |
| 122 CompletionCallback read_callback_; | |
| 123 | |
| 124 // External callback; called when write is complete. | |
| 125 CompletionCallback write_callback_; | |
| 126 | |
| 127 IPEndPoint peer_address_; | |
| 128 // The OS error that a connect attempt last completed with. | |
| 129 int connect_os_error_; | |
| 130 | |
| 131 bool logging_multiple_connect_attempts_; | |
| 132 | |
| 67 BoundNetLog net_log_; | 133 BoundNetLog net_log_; |
| 68 | 134 |
| 69 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin); | 135 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin); |
| 70 }; | 136 }; |
| 71 | 137 |
| 72 } // namespace net | 138 } // namespace net |
| 73 | 139 |
| 74 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_ | 140 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_ |
| 141 | |
| OLD | NEW |