QUIC - enable persisting of QUICServerInfo (server config) to disk
[chromium-blink-merge.git] / net / socket / tcp_socket_libevent.h
blob0335c9ea2dea0587ad6ff29421f3a2b3176363dd
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 "net/base/address_family.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/net_export.h"
15 #include "net/base/net_log.h"
17 namespace net {
19 class AddressList;
20 class IOBuffer;
21 class IPEndPoint;
22 class SocketLibevent;
24 class NET_EXPORT TCPSocketLibevent {
25 public:
26 TCPSocketLibevent(NetLog* net_log, const NetLog::Source& source);
27 virtual ~TCPSocketLibevent();
29 int Open(AddressFamily family);
30 // Takes ownership of |socket_fd|.
31 int AdoptConnectedSocket(int socket_fd, const IPEndPoint& peer_address);
33 int Bind(const IPEndPoint& address);
35 int Listen(int backlog);
36 int Accept(scoped_ptr<TCPSocketLibevent>* socket,
37 IPEndPoint* address,
38 const CompletionCallback& callback);
40 int Connect(const IPEndPoint& address, const CompletionCallback& callback);
41 bool IsConnected() const;
42 bool IsConnectedAndIdle() const;
44 // Multiple outstanding requests are not supported.
45 // Full duplex mode (reading and writing at the same time) is supported.
46 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
47 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
49 int GetLocalAddress(IPEndPoint* address) const;
50 int GetPeerAddress(IPEndPoint* address) const;
52 // Sets various socket options.
53 // The commonly used options for server listening sockets:
54 // - SetAddressReuse(true).
55 int SetDefaultOptionsForServer();
56 // The commonly used options for client sockets and accepted sockets:
57 // - SetNoDelay(true);
58 // - SetKeepAlive(true, 45).
59 void SetDefaultOptionsForClient();
60 int SetAddressReuse(bool allow);
61 int SetReceiveBufferSize(int32 size);
62 int SetSendBufferSize(int32 size);
63 bool SetKeepAlive(bool enable, int delay);
64 bool SetNoDelay(bool no_delay);
66 void Close();
68 bool UsingTCPFastOpen() const;
69 bool IsValid() const;
71 // Marks the start/end of a series of connect attempts for logging purpose.
73 // TCPClientSocket may attempt to connect to multiple addresses until it
74 // succeeds in establishing a connection. The corresponding log will have
75 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
76 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
77 // NetLog::TYPE_TCP_CONNECT.
79 // TODO(yzshen): Change logging format and let TCPClientSocket log the
80 // start/end of a series of connect attempts itself.
81 void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
82 void EndLoggingMultipleConnectAttempts(int net_error);
84 const BoundNetLog& net_log() const { return net_log_; }
86 private:
87 // States that a fast open socket attempt can result in.
88 enum FastOpenStatus {
89 FAST_OPEN_STATUS_UNKNOWN,
91 // The initial fast open connect attempted returned synchronously,
92 // indicating that we had and sent a cookie along with the initial data.
93 FAST_OPEN_FAST_CONNECT_RETURN,
95 // The initial fast open connect attempted returned asynchronously,
96 // indicating that we did not have a cookie for the server.
97 FAST_OPEN_SLOW_CONNECT_RETURN,
99 // Some other error occurred on connection, so we couldn't tell if
100 // fast open would have worked.
101 FAST_OPEN_ERROR,
103 // An attempt to do a fast open succeeded immediately
104 // (FAST_OPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
105 // had acked the data we sent.
106 FAST_OPEN_SYN_DATA_ACK,
108 // An attempt to do a fast open succeeded immediately
109 // (FAST_OPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
110 // had nacked the data we sent.
111 FAST_OPEN_SYN_DATA_NACK,
113 // An attempt to do a fast open succeeded immediately
114 // (FAST_OPEN_FAST_CONNECT_RETURN) and our probe to determine if the
115 // socket was using fast open failed.
116 FAST_OPEN_SYN_DATA_FAILED,
118 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN)
119 // and we later confirmed that the server had acked initial data. This
120 // should never happen (we didn't send data, so it shouldn't have
121 // been acked).
122 FAST_OPEN_NO_SYN_DATA_ACK,
124 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN)
125 // and we later discovered that the server had nacked initial data. This
126 // is the expected case results for FAST_OPEN_SLOW_CONNECT_RETURN.
127 FAST_OPEN_NO_SYN_DATA_NACK,
129 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN)
130 // and our later probe for ack/nack state failed.
131 FAST_OPEN_NO_SYN_DATA_FAILED,
133 FAST_OPEN_MAX_VALUE
136 void AcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket,
137 IPEndPoint* address,
138 const CompletionCallback& callback,
139 int rv);
140 int HandleAcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket,
141 IPEndPoint* address,
142 int rv);
143 int BuildTcpSocketLibevent(scoped_ptr<TCPSocketLibevent>* tcp_socket,
144 IPEndPoint* address);
146 void ConnectCompleted(const CompletionCallback& callback, int rv) const;
147 int HandleConnectCompleted(int rv) const;
148 void LogConnectBegin(const AddressList& addresses) const;
149 void LogConnectEnd(int net_error) const;
151 void ReadCompleted(const scoped_refptr<IOBuffer>& buf,
152 const CompletionCallback& callback,
153 int rv);
154 int HandleReadCompleted(IOBuffer* buf, int rv);
156 void WriteCompleted(const scoped_refptr<IOBuffer>& buf,
157 const CompletionCallback& callback,
158 int rv) const;
159 int HandleWriteCompleted(IOBuffer* buf, int rv) const;
160 int TcpFastOpenWrite(IOBuffer* buf,
161 int buf_len,
162 const CompletionCallback& callback);
164 // Called when the socket is known to be in a connected state.
165 void RecordFastOpenStatus();
167 scoped_ptr<SocketLibevent> socket_;
168 scoped_ptr<SocketLibevent> accept_socket_;
170 // Enables experimental TCP FastOpen option.
171 const bool use_tcp_fastopen_;
173 // True when TCP FastOpen is in use and we have done the connect.
174 bool tcp_fastopen_connected_;
176 FastOpenStatus fast_open_status_;
178 bool logging_multiple_connect_attempts_;
180 BoundNetLog net_log_;
182 DISALLOW_COPY_AND_ASSIGN(TCPSocketLibevent);
185 } // namespace net
187 #endif // NET_SOCKET_TCP_SOCKET_LIBEVENT_H_