OLD | NEW |
1 // Copyright 2013 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_SOCKET_TCP_SOCKET_WIN_H_ | 5 #ifndef NET_SOCKET_TCP_SOCKET_NACL_H_ |
6 #define NET_SOCKET_TCP_SOCKET_WIN_H_ | 6 #define NET_SOCKET_TCP_SOCKET_NACL_H_ |
7 | |
8 #include <winsock2.h> | |
9 | 7 |
10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" |
11 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
14 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
15 #include "base/win/object_watcher.h" | |
16 #include "net/base/address_family.h" | 14 #include "net/base/address_family.h" |
17 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
18 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
19 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
| 18 #include "net/socket/socket_descriptor.h" |
20 | 19 |
21 namespace net { | 20 namespace net { |
22 | 21 |
23 class AddressList; | 22 class AddressList; |
24 class IOBuffer; | 23 class IOBuffer; |
25 class IPEndPoint; | 24 class IPEndPoint; |
26 | 25 |
27 class NET_EXPORT TCPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe), | 26 class NET_EXPORT TCPSocketNacl : public base::NonThreadSafe { |
28 public base::win::ObjectWatcher::Delegate { | |
29 public: | 27 public: |
30 TCPSocketWin(NetLog* net_log, const NetLog::Source& source); | 28 TCPSocketNacl(NetLog* net_log, const NetLog::Source& source); |
31 virtual ~TCPSocketWin(); | 29 virtual ~TCPSocketNacl(); |
32 | 30 |
33 int Open(AddressFamily family); | 31 int Open(AddressFamily family); |
34 // Takes ownership of |socket|. | 32 // Takes ownership of |socket|. |
35 int AdoptConnectedSocket(SOCKET socket, const IPEndPoint& peer_address); | 33 int AdoptConnectedSocket(int socket, const IPEndPoint& peer_address); |
36 | 34 |
37 int Bind(const IPEndPoint& address); | 35 int Bind(const IPEndPoint& address); |
38 | 36 |
39 int Listen(int backlog); | 37 int Listen(int backlog); |
40 int Accept(scoped_ptr<TCPSocketWin>* socket, | 38 int Accept(scoped_ptr<TCPSocketNacl>* socket, |
41 IPEndPoint* address, | 39 IPEndPoint* address, |
42 const CompletionCallback& callback); | 40 const CompletionCallback& callback); |
43 | 41 |
44 int Connect(const IPEndPoint& address, const CompletionCallback& callback); | 42 int Connect(const IPEndPoint& address, const CompletionCallback& callback); |
45 bool IsConnected() const; | 43 bool IsConnected() const; |
46 bool IsConnectedAndIdle() const; | 44 bool IsConnectedAndIdle() const; |
47 | 45 |
48 // Multiple outstanding requests are not supported. | 46 // Multiple outstanding requests are not supported. |
49 // Full duplex mode (reading and writing at the same time) is supported. | 47 // Full duplex mode (reading and writing at the same time) is supported. |
50 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 48 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
51 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 49 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
52 | 50 |
53 int GetLocalAddress(IPEndPoint* address) const; | 51 int GetLocalAddress(IPEndPoint* address) const; |
54 int GetPeerAddress(IPEndPoint* address) const; | 52 int GetPeerAddress(IPEndPoint* address) const; |
55 | 53 |
56 // Sets various socket options. | 54 // Sets various socket options. |
57 // The commonly used options for server listening sockets: | 55 // The commonly used options for server listening sockets: |
58 // - SetExclusiveAddrUse(). | 56 // - SetAddressReuse(true). |
59 int SetDefaultOptionsForServer(); | 57 int SetDefaultOptionsForServer(); |
60 // The commonly used options for client sockets and accepted sockets: | 58 // The commonly used options for client sockets and accepted sockets: |
61 // - Increase the socket buffer sizes for WinXP; | |
62 // - SetNoDelay(true); | 59 // - SetNoDelay(true); |
63 // - SetKeepAlive(true, 45). | 60 // - SetKeepAlive(true, 45). |
64 void SetDefaultOptionsForClient(); | 61 void SetDefaultOptionsForClient(); |
65 int SetExclusiveAddrUse(); | 62 int SetAddressReuse(bool allow); |
66 bool SetReceiveBufferSize(int32 size); | 63 bool SetReceiveBufferSize(int32 size); |
67 bool SetSendBufferSize(int32 size); | 64 bool SetSendBufferSize(int32 size); |
68 bool SetKeepAlive(bool enable, int delay); | 65 bool SetKeepAlive(bool enable, int delay); |
69 bool SetNoDelay(bool no_delay); | 66 bool SetNoDelay(bool no_delay); |
70 | 67 |
71 void Close(); | 68 void Close(); |
72 | 69 |
73 bool UsingTCPFastOpen() const; | 70 bool UsingTCPFastOpen() const; |
74 bool IsValid() const { return socket_ != INVALID_SOCKET; } | 71 bool IsValid() const { return false; } |
75 | 72 |
76 // Marks the start/end of a series of connect attempts for logging purpose. | 73 // Marks the start/end of a series of connect attempts for logging purpose. |
77 // | 74 // |
78 // TCPClientSocket may attempt to connect to multiple addresses until it | 75 // TCPClientSocket may attempt to connect to multiple addresses until it |
79 // succeeds in establishing a connection. The corresponding log will have | 76 // succeeds in establishing a connection. The corresponding log will have |
80 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a | 77 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a |
81 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of | 78 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of |
82 // NetLog::TYPE_TCP_CONNECT. | 79 // NetLog::TYPE_TCP_CONNECT. |
83 // | 80 // |
84 // TODO(yzshen): Change logging format and let TCPClientSocket log the | 81 // TODO(yzshen): Change logging format and let TCPClientSocket log the |
85 // start/end of a series of connect attempts itself. | 82 // start/end of a series of connect attempts itself. |
86 void StartLoggingMultipleConnectAttempts(const AddressList& addresses); | 83 void StartLoggingMultipleConnectAttempts(const AddressList& addresses); |
87 void EndLoggingMultipleConnectAttempts(int net_error); | 84 void EndLoggingMultipleConnectAttempts(int net_error); |
88 | 85 |
89 const BoundNetLog& net_log() const { return net_log_; } | 86 const BoundNetLog& net_log() const { return net_log_; } |
90 | 87 |
91 private: | 88 private: |
92 class Core; | |
93 | |
94 // base::ObjectWatcher::Delegate implementation. | |
95 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; | |
96 | |
97 int AcceptInternal(scoped_ptr<TCPSocketWin>* socket, | |
98 IPEndPoint* address); | |
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 | |
111 SOCKET socket_; | |
112 | |
113 HANDLE accept_event_; | |
114 base::win::ObjectWatcher accept_watcher_; | |
115 | |
116 scoped_ptr<TCPSocketWin>* accept_socket_; | |
117 IPEndPoint* accept_address_; | |
118 CompletionCallback accept_callback_; | |
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 scoped_ptr<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 | |
142 BoundNetLog net_log_; | 89 BoundNetLog net_log_; |
143 | 90 |
144 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin); | 91 DISALLOW_COPY_AND_ASSIGN(TCPSocketNacl); |
145 }; | 92 }; |
146 | 93 |
147 } // namespace net | 94 } // namespace net |
148 | 95 |
149 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_ | 96 #endif // NET_SOCKET_TCP_SOCKET_NACL_H_ |
150 | |
OLD | NEW |