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_LIBEVENT_H_ | 5 #ifndef NET_SOCKET_TCP_SOCKET_LIBEVENT_H_ |
6 #define NET_SOCKET_TCP_SOCKET_LIBEVENT_H_ | 6 #define NET_SOCKET_TCP_SOCKET_LIBEVENT_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/threading/non_thread_safe.h" | |
15 #include "net/base/address_family.h" | 12 #include "net/base/address_family.h" |
16 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
17 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
18 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
19 #include "net/socket/socket_descriptor.h" | |
20 | 16 |
21 namespace net { | 17 namespace net { |
22 | 18 |
23 class AddressList; | 19 class AddressList; |
24 class IOBuffer; | 20 class IOBuffer; |
25 class IPEndPoint; | 21 class IPEndPoint; |
| 22 class SocketLibevent; |
26 | 23 |
27 class NET_EXPORT TCPSocketLibevent : public base::NonThreadSafe { | 24 class NET_EXPORT TCPSocketLibevent { |
28 public: | 25 public: |
29 TCPSocketLibevent(NetLog* net_log, const NetLog::Source& source); | 26 TCPSocketLibevent(NetLog* net_log, const NetLog::Source& source); |
30 virtual ~TCPSocketLibevent(); | 27 virtual ~TCPSocketLibevent(); |
31 | 28 |
32 int Open(AddressFamily family); | 29 int Open(AddressFamily family); |
33 // Takes ownership of |socket|. | 30 // Takes ownership of |socket_fd|. |
34 int AdoptConnectedSocket(int socket, const IPEndPoint& peer_address); | 31 int AdoptConnectedSocket(int socket_fd, const IPEndPoint& peer_address); |
35 | 32 |
36 int Bind(const IPEndPoint& address); | 33 int Bind(const IPEndPoint& address); |
37 | 34 |
38 int Listen(int backlog); | 35 int Listen(int backlog); |
39 int Accept(scoped_ptr<TCPSocketLibevent>* socket, | 36 int Accept(scoped_ptr<TCPSocketLibevent>* socket, |
40 IPEndPoint* address, | 37 IPEndPoint* address, |
41 const CompletionCallback& callback); | 38 const CompletionCallback& callback); |
42 | 39 |
43 int Connect(const IPEndPoint& address, const CompletionCallback& callback); | 40 int Connect(const IPEndPoint& address, const CompletionCallback& callback); |
44 bool IsConnected() const; | 41 bool IsConnected() const; |
(...skipping 17 matching lines...) Expand all Loading... |
62 void SetDefaultOptionsForClient(); | 59 void SetDefaultOptionsForClient(); |
63 int SetAddressReuse(bool allow); | 60 int SetAddressReuse(bool allow); |
64 int SetReceiveBufferSize(int32 size); | 61 int SetReceiveBufferSize(int32 size); |
65 int SetSendBufferSize(int32 size); | 62 int SetSendBufferSize(int32 size); |
66 bool SetKeepAlive(bool enable, int delay); | 63 bool SetKeepAlive(bool enable, int delay); |
67 bool SetNoDelay(bool no_delay); | 64 bool SetNoDelay(bool no_delay); |
68 | 65 |
69 void Close(); | 66 void Close(); |
70 | 67 |
71 bool UsingTCPFastOpen() const; | 68 bool UsingTCPFastOpen() const; |
72 bool IsValid() const { return socket_ != kInvalidSocket; } | 69 bool IsValid() const { return socket_ != NULL; } |
73 | 70 |
74 // Marks the start/end of a series of connect attempts for logging purpose. | 71 // Marks the start/end of a series of connect attempts for logging purpose. |
75 // | 72 // |
76 // TCPClientSocket may attempt to connect to multiple addresses until it | 73 // TCPClientSocket may attempt to connect to multiple addresses until it |
77 // succeeds in establishing a connection. The corresponding log will have | 74 // succeeds in establishing a connection. The corresponding log will have |
78 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a | 75 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a |
79 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of | 76 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of |
80 // NetLog::TYPE_TCP_CONNECT. | 77 // NetLog::TYPE_TCP_CONNECT. |
81 // | 78 // |
82 // TODO(yzshen): Change logging format and let TCPClientSocket log the | 79 // TODO(yzshen): Change logging format and let TCPClientSocket log the |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 // is the expected case results for FAST_OPEN_SLOW_CONNECT_RETURN. | 126 // is the expected case results for FAST_OPEN_SLOW_CONNECT_RETURN. |
130 FAST_OPEN_NO_SYN_DATA_NACK, | 127 FAST_OPEN_NO_SYN_DATA_NACK, |
131 | 128 |
132 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN) | 129 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN) |
133 // and our later probe for ack/nack state failed. | 130 // and our later probe for ack/nack state failed. |
134 FAST_OPEN_NO_SYN_DATA_FAILED, | 131 FAST_OPEN_NO_SYN_DATA_FAILED, |
135 | 132 |
136 FAST_OPEN_MAX_VALUE | 133 FAST_OPEN_MAX_VALUE |
137 }; | 134 }; |
138 | 135 |
139 // Watcher simply forwards notifications to Closure objects set via the | 136 void AcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket, |
140 // constructor. | 137 IPEndPoint* address, |
141 class Watcher: public base::MessageLoopForIO::Watcher { | 138 const CompletionCallback& callback, |
142 public: | 139 int rv); |
143 Watcher(const base::Closure& read_ready_callback, | 140 int HandleAcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket, |
144 const base::Closure& write_ready_callback); | 141 IPEndPoint* address, |
145 virtual ~Watcher(); | 142 int rv); |
| 143 int BuildTcpSocketLibevent(scoped_ptr<TCPSocketLibevent>* tcp_socket, |
| 144 IPEndPoint* address); |
146 | 145 |
147 // base::MessageLoopForIO::Watcher methods. | 146 void ConnectCompleted(const CompletionCallback& callback, int rv) const; |
148 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 147 int HandleConnectCompleted(int rv) const; |
149 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 148 void LogConnectBegin(const AddressList& addresses) const; |
| 149 void LogConnectEnd(int net_error) const; |
150 | 150 |
151 private: | 151 void ReadCompleted(IOBuffer* buf, |
152 base::Closure read_ready_callback_; | 152 const CompletionCallback& callback, |
153 base::Closure write_ready_callback_; | 153 int rv); |
| 154 int HandleReadCompleted(IOBuffer* buf, int rv); |
154 | 155 |
155 DISALLOW_COPY_AND_ASSIGN(Watcher); | 156 void WriteCompleted(IOBuffer* buf, |
156 }; | 157 const CompletionCallback& callback, |
157 | 158 int rv) const; |
158 int AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket, | 159 int HandleWriteCompleted(IOBuffer* buf, int rv) const; |
159 IPEndPoint* address); | 160 int TcpFastOpenWrite(IOBuffer* buf, |
160 | 161 int buf_len, |
161 int DoConnect(); | 162 const CompletionCallback& callback); |
162 void DoConnectComplete(int result); | |
163 | |
164 void LogConnectBegin(const AddressList& addresses); | |
165 void LogConnectEnd(int net_error); | |
166 | |
167 void DidCompleteRead(); | |
168 void DidCompleteWrite(); | |
169 void DidCompleteConnect(); | |
170 void DidCompleteConnectOrWrite(); | |
171 void DidCompleteAccept(); | |
172 | |
173 // Internal function to write to a socket. Returns an OS error. | |
174 int InternalWrite(IOBuffer* buf, int buf_len); | |
175 | 163 |
176 // Called when the socket is known to be in a connected state. | 164 // Called when the socket is known to be in a connected state. |
177 void RecordFastOpenStatus(); | 165 void RecordFastOpenStatus(); |
178 | 166 |
179 int socket_; | 167 scoped_ptr<SocketLibevent> socket_; |
180 | 168 scoped_ptr<SocketLibevent> accept_socket_; |
181 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; | |
182 Watcher accept_watcher_; | |
183 | |
184 scoped_ptr<TCPSocketLibevent>* accept_socket_; | |
185 IPEndPoint* accept_address_; | |
186 CompletionCallback accept_callback_; | |
187 | |
188 // The socket's libevent wrappers for reads and writes. | |
189 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | |
190 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | |
191 | |
192 // The corresponding watchers for reads and writes. | |
193 Watcher read_watcher_; | |
194 Watcher write_watcher_; | |
195 | |
196 // The buffer used for reads. | |
197 scoped_refptr<IOBuffer> read_buf_; | |
198 int read_buf_len_; | |
199 | |
200 // The buffer used for writes. | |
201 scoped_refptr<IOBuffer> write_buf_; | |
202 int write_buf_len_; | |
203 | |
204 // External callback; called when read is complete. | |
205 CompletionCallback read_callback_; | |
206 | |
207 // External callback; called when write or connect is complete. | |
208 CompletionCallback write_callback_; | |
209 | 169 |
210 // Enables experimental TCP FastOpen option. | 170 // Enables experimental TCP FastOpen option. |
211 const bool use_tcp_fastopen_; | 171 const bool use_tcp_fastopen_; |
212 | 172 |
213 // True when TCP FastOpen is in use and we have done the connect. | 173 // True when TCP FastOpen is in use and we have done the connect. |
214 bool tcp_fastopen_connected_; | 174 bool tcp_fastopen_connected_; |
215 | 175 |
216 FastOpenStatus fast_open_status_; | 176 FastOpenStatus fast_open_status_; |
217 | 177 |
218 // A connect operation is pending. In this case, |write_callback_| needs to be | |
219 // called when connect is complete. | |
220 bool waiting_connect_; | |
221 | |
222 scoped_ptr<IPEndPoint> peer_address_; | |
223 // The OS error that a connect attempt last completed with. | |
224 int connect_os_error_; | |
225 | |
226 bool logging_multiple_connect_attempts_; | 178 bool logging_multiple_connect_attempts_; |
227 | 179 |
228 BoundNetLog net_log_; | 180 BoundNetLog net_log_; |
229 | 181 |
230 DISALLOW_COPY_AND_ASSIGN(TCPSocketLibevent); | 182 DISALLOW_COPY_AND_ASSIGN(TCPSocketLibevent); |
231 }; | 183 }; |
232 | 184 |
233 } // namespace net | 185 } // namespace net |
234 | 186 |
235 #endif // NET_SOCKET_TCP_SOCKET_LIBEVENT_H_ | 187 #endif // NET_SOCKET_TCP_SOCKET_LIBEVENT_H_ |
OLD | NEW |