Evict resources from resource pool after timeout
[chromium-blink-merge.git] / net / socket / tcp_socket_libevent.h
blob479b3a62f4dd47099748598fe7b7d1e95723a0f2
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
3 // found in the LICENSE file.
5 #ifndef NET_SOCKET_TCP_SOCKET_LIBEVENT_H_
6 #define NET_SOCKET_TCP_SOCKET_LIBEVENT_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "net/base/address_family.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/net_export.h"
16 #include "net/log/net_log.h"
18 namespace net {
20 class AddressList;
21 class IOBuffer;
22 class IPEndPoint;
23 class SocketLibevent;
25 class NET_EXPORT TCPSocketLibevent {
26 public:
27 TCPSocketLibevent(NetLog* net_log, const NetLog::Source& source);
28 virtual ~TCPSocketLibevent();
30 int Open(AddressFamily family);
31 // Takes ownership of |socket_fd|.
32 int AdoptConnectedSocket(int socket_fd, const IPEndPoint& peer_address);
34 int Bind(const IPEndPoint& address);
36 int Listen(int backlog);
37 int Accept(scoped_ptr<TCPSocketLibevent>* socket,
38 IPEndPoint* address,
39 const CompletionCallback& callback);
41 int Connect(const IPEndPoint& address, const CompletionCallback& callback);
42 bool IsConnected() const;
43 bool IsConnectedAndIdle() const;
45 // Multiple outstanding requests are not supported.
46 // Full duplex mode (reading and writing at the same time) is supported.
47 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
48 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
50 int GetLocalAddress(IPEndPoint* address) const;
51 int GetPeerAddress(IPEndPoint* address) const;
53 // Sets various socket options.
54 // The commonly used options for server listening sockets:
55 // - SetAddressReuse(true).
56 int SetDefaultOptionsForServer();
57 // The commonly used options for client sockets and accepted sockets:
58 // - SetNoDelay(true);
59 // - SetKeepAlive(true, 45).
60 void SetDefaultOptionsForClient();
61 int SetAddressReuse(bool allow);
62 int SetReceiveBufferSize(int32 size);
63 int SetSendBufferSize(int32 size);
64 bool SetKeepAlive(bool enable, int delay);
65 bool SetNoDelay(bool no_delay);
67 // Gets the estimated RTT. Returns false if the RTT is
68 // unavailable. May also return false when estimated RTT is 0.
69 bool GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const
70 WARN_UNUSED_RESULT;
72 void Close();
74 // Setter/Getter methods for TCP FastOpen socket option.
75 bool UsingTCPFastOpen() const;
76 void EnableTCPFastOpenIfSupported();
78 bool IsValid() const;
80 // Marks the start/end of a series of connect attempts for logging purpose.
82 // TCPClientSocket may attempt to connect to multiple addresses until it
83 // succeeds in establishing a connection. The corresponding log will have
84 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
85 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
86 // NetLog::TYPE_TCP_CONNECT.
88 // TODO(yzshen): Change logging format and let TCPClientSocket log the
89 // start/end of a series of connect attempts itself.
90 void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
91 void EndLoggingMultipleConnectAttempts(int net_error);
93 const BoundNetLog& net_log() const { return net_log_; }
95 private:
96 // States that using a socket with TCP FastOpen can lead to.
97 enum TCPFastOpenStatus {
98 TCP_FASTOPEN_STATUS_UNKNOWN,
100 // The initial FastOpen connect attempted returned synchronously,
101 // indicating that we had and sent a cookie along with the initial data.
102 TCP_FASTOPEN_FAST_CONNECT_RETURN,
104 // The initial FastOpen connect attempted returned asynchronously,
105 // indicating that we did not have a cookie for the server.
106 TCP_FASTOPEN_SLOW_CONNECT_RETURN,
108 // Some other error occurred on connection, so we couldn't tell if
109 // FastOpen would have worked.
110 TCP_FASTOPEN_ERROR,
112 // An attempt to do a FastOpen succeeded immediately
113 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
114 // had acked the data we sent.
115 TCP_FASTOPEN_SYN_DATA_ACK,
117 // An attempt to do a FastOpen succeeded immediately
118 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
119 // had nacked the data we sent.
120 TCP_FASTOPEN_SYN_DATA_NACK,
122 // An attempt to do a FastOpen succeeded immediately
123 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and our probe to determine if the
124 // socket was using FastOpen failed.
125 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED,
127 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
128 // and we later confirmed that the server had acked initial data. This
129 // should never happen (we didn't send data, so it shouldn't have
130 // been acked).
131 TCP_FASTOPEN_NO_SYN_DATA_ACK,
133 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
134 // and we later discovered that the server had nacked initial data. This
135 // is the expected case results for TCP_FASTOPEN_SLOW_CONNECT_RETURN.
136 TCP_FASTOPEN_NO_SYN_DATA_NACK,
138 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
139 // and our later probe for ack/nack state failed.
140 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED,
142 // The initial FastOpen connect+write succeeded immediately
143 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and a subsequent attempt to read from
144 // the connection failed.
145 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED,
147 // The initial FastOpen connect+write failed
148 // (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
149 // and a subsequent attempt to read from the connection failed.
150 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED,
152 // We didn't try FastOpen because it had failed in the past
153 // (g_tcp_fastopen_has_failed was true.)
154 // NOTE: This status is currently registered before a connect/write call
155 // is attempted, and may capture some cases where the status is registered
156 // but no connect is subsequently attempted.
157 // TODO(jri): The expectation is that such cases are not the common case
158 // with TCP FastOpen for SSL sockets however. Change code to be more
159 // accurate when TCP FastOpen is used for more than just SSL sockets.
160 TCP_FASTOPEN_PREVIOUSLY_FAILED,
162 TCP_FASTOPEN_MAX_VALUE
165 void AcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket,
166 IPEndPoint* address,
167 const CompletionCallback& callback,
168 int rv);
169 int HandleAcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket,
170 IPEndPoint* address,
171 int rv);
172 int BuildTcpSocketLibevent(scoped_ptr<TCPSocketLibevent>* tcp_socket,
173 IPEndPoint* address);
175 void ConnectCompleted(const CompletionCallback& callback, int rv) const;
176 int HandleConnectCompleted(int rv) const;
177 void LogConnectBegin(const AddressList& addresses) const;
178 void LogConnectEnd(int net_error) const;
180 void ReadCompleted(const scoped_refptr<IOBuffer>& buf,
181 const CompletionCallback& callback,
182 int rv);
183 int HandleReadCompleted(IOBuffer* buf, int rv);
185 void WriteCompleted(const scoped_refptr<IOBuffer>& buf,
186 const CompletionCallback& callback,
187 int rv);
188 int HandleWriteCompleted(IOBuffer* buf, int rv);
189 int TcpFastOpenWrite(IOBuffer* buf,
190 int buf_len,
191 const CompletionCallback& callback);
193 // Called after the first read completes on a TCP FastOpen socket.
194 void UpdateTCPFastOpenStatusAfterRead();
196 scoped_ptr<SocketLibevent> socket_;
197 scoped_ptr<SocketLibevent> accept_socket_;
199 // Enables experimental TCP FastOpen option.
200 bool use_tcp_fastopen_;
202 // True when TCP FastOpen is in use and we have attempted the
203 // connect with write.
204 bool tcp_fastopen_write_attempted_;
206 // True when TCP FastOpen is in use and we have done the connect.
207 bool tcp_fastopen_connected_;
209 TCPFastOpenStatus tcp_fastopen_status_;
211 bool logging_multiple_connect_attempts_;
213 BoundNetLog net_log_;
215 DISALLOW_COPY_AND_ASSIGN(TCPSocketLibevent);
218 } // namespace net
220 #endif // NET_SOCKET_TCP_SOCKET_LIBEVENT_H_