Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: net/socket/tcp_socket_libevent.h

Issue 23454010: POSIX only: Move client socket functionality from TCPClientSocket into TCPSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
12 #include "base/threading/non_thread_safe.h" 14 #include "base/threading/non_thread_safe.h"
13 #include "net/base/address_family.h" 15 #include "net/base/address_family.h"
14 #include "net/base/completion_callback.h" 16 #include "net/base/completion_callback.h"
17 #include "net/base/ip_endpoint.h"
15 #include "net/base/net_export.h" 18 #include "net/base/net_export.h"
16 #include "net/base/net_log.h" 19 #include "net/base/net_log.h"
20 #include "net/socket/socket_descriptor.h"
17 21
18 namespace net { 22 namespace net {
19 23
20 class IPEndPoint; 24 class AddressList;
akalin 2013/09/05 22:58:12 i'm assuming this is all moved from tcp_client_soc
yzshen1 2013/09/06 17:19:45 Mostly yes. A few major changes include: - ReadWat
21 25 class IOBuffer;
22 // TODO(yzshen): This class is incomplete. TCP client operations (Connect/Read/ 26
23 // Write/etc.) will be added. And TCPClientSocket will be changed to be a 27 class NET_EXPORT TCPSocketLibevent : public base::NonThreadSafe {
24 // wrapper around TCPSocket.
25 class NET_EXPORT TCPSocketLibevent : public base::NonThreadSafe,
26 public base::MessageLoopForIO::Watcher {
27 public: 28 public:
28 TCPSocketLibevent(NetLog* net_log, const NetLog::Source& source); 29 TCPSocketLibevent(NetLog* net_log, const NetLog::Source& source);
29 virtual ~TCPSocketLibevent(); 30 virtual ~TCPSocketLibevent();
30 31
31 int Create(AddressFamily family); 32 int Create(AddressFamily family);
32 // Takes ownership of |socket|. 33 // Takes ownership of |socket|.
33 int Adopt(int socket); 34 int AdoptConnectedSocket(int socket, const IPEndPoint& peer_address);
34 // Returns a socket file descriptor. The ownership is transferred to the 35
35 // caller.
36 int Release();
37 int Bind(const IPEndPoint& address); 36 int Bind(const IPEndPoint& address);
38 int GetLocalAddress(IPEndPoint* address) const; 37 int GetLocalAddress(IPEndPoint* address) const;
38
39 int Listen(int backlog); 39 int Listen(int backlog);
40 int Accept(scoped_ptr<TCPSocketLibevent>* socket, 40 int Accept(scoped_ptr<TCPSocketLibevent>* socket,
41 IPEndPoint* address, 41 IPEndPoint* address,
42 const CompletionCallback& callback); 42 const CompletionCallback& callback);
43
44 int Connect(const IPEndPoint& address, const CompletionCallback& callback);
45 bool IsConnected() const;
46 bool IsConnectedAndIdle() const;
47 int GetPeerAddress(IPEndPoint* address) const;
48
49 // Multiple outstanding requests are not supported.
50 // Full duplex mode (reading and writing at the same time) is supported.
51 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
52 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
53
43 int SetDefaultOptionsForServer(); 54 int SetDefaultOptionsForServer();
55 int SetDefaultOptionsForClient();
44 int SetAddressReuse(bool allow); 56 int SetAddressReuse(bool allow);
57 bool SetReceiveBufferSize(int32 size);
58 bool SetSendBufferSize(int32 size);
59 bool SetKeepAlive(bool enable, int delay);
60 bool SetNoDelay(bool no_delay);
61
45 void Close(); 62 void Close();
46 63
64 bool UsingTCPFastOpen() const;
65 bool IsValid() const { return socket_ != kInvalidSocket; }
66
67 // Marks the start/end of a series of connect attempts for logging purpose.
68 //
69 // TCPClientSocket may attempt to connect to multiple addresses until it
70 // succeeds in establishing a connection. The corresponding log will be
71 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
72 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
73 // NetLog::TYPE_TCP_CONNECT.
74 void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
75 void EndLoggingMultipleConnectAttempts(int net_error);
76
47 const BoundNetLog& net_log() const { return net_log_; } 77 const BoundNetLog& net_log() const { return net_log_; }
48 78
49 // MessageLoopForIO::Watcher implementation.
50 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
51 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
52
53 private: 79 private:
80 // States that a fast open socket attempt can result in.
81 enum FastOpenStatus {
82 FAST_OPEN_STATUS_UNKNOWN,
83
84 // The initial fast open connect attempted returned synchronously,
85 // indicating that we had and sent a cookie along with the initial data.
86 FAST_OPEN_FAST_CONNECT_RETURN,
87
88 // The initial fast open connect attempted returned asynchronously,
89 // indicating that we did not have a cookie for the server.
90 FAST_OPEN_SLOW_CONNECT_RETURN,
91
92 // Some other error occurred on connection, so we couldn't tell if
93 // fast open would have worked.
94 FAST_OPEN_ERROR,
95
96 // An attempt to do a fast open succeeded immediately
97 // (FAST_OPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
98 // had acked the data we sent.
99 FAST_OPEN_SYN_DATA_ACK,
100
101 // An attempt to do a fast open succeeded immediately
102 // (FAST_OPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
103 // had nacked the data we sent.
104 FAST_OPEN_SYN_DATA_NACK,
105
106 // An attempt to do a fast open succeeded immediately
107 // (FAST_OPEN_FAST_CONNECT_RETURN) and our probe to determine if the
108 // socket was using fast open failed.
109 FAST_OPEN_SYN_DATA_FAILED,
110
111 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN)
112 // and we later confirmed that the server had acked initial data. This
113 // should never happen (we didn't send data, so it shouldn't have
114 // been acked).
115 FAST_OPEN_NO_SYN_DATA_ACK,
116
117 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN)
118 // and we later discovered that the server had nacked initial data. This
119 // is the expected case results for FAST_OPEN_SLOW_CONNECT_RETURN.
120 FAST_OPEN_NO_SYN_DATA_NACK,
121
122 // An attempt to do a fast open failed (FAST_OPEN_SLOW_CONNECT_RETURN)
123 // and our later probe for ack/nack state failed.
124 FAST_OPEN_NO_SYN_DATA_FAILED,
125
126 FAST_OPEN_MAX_VALUE
127 };
128
129 // Watcher simply forwards notifications to Closure objects set via the
130 // constructor.
131 class Watcher: public base::MessageLoopForIO::Watcher {
132 public:
133 Watcher(const base::Closure& read_ready_callback,
134 const base::Closure& write_ready_callback);
135 virtual ~Watcher();
136
137 // base::MessageLoopForIO::Watcher methods.
138 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
139 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
140
141 private:
142 base::Closure read_ready_callback_;
143 base::Closure write_ready_callback_;
144
145 DISALLOW_COPY_AND_ASSIGN(Watcher);
146 };
147
54 int AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket, 148 int AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket,
55 IPEndPoint* address); 149 IPEndPoint* address);
56 150
151 int DoConnect();
152 void DoConnectComplete(int result);
153
154 void LogConnectStart(const AddressList& addresses);
155 void LogConnectCompletion(int net_error);
156
157 void DoReadCallback(int rv);
158 void DoWriteCallback(int rv);
159 void DidCompleteRead();
160 void DidCompleteWrite();
161 void DidCompleteConnect();
162 void DidCompleteConnectOrWrite();
163 void DidCompleteAccept();
164
165 // Internal function to write to a socket. Returns an OS error.
166 int InternalWrite(IOBuffer* buf, int buf_len);
167
168 // Called when the socket is known to be in a connected state.
169 void RecordFastOpenStatus();
170
57 int socket_; 171 int socket_;
58 172
59 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; 173 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_;
174 Watcher accept_watcher_;
60 175
61 scoped_ptr<TCPSocketLibevent>* accept_socket_; 176 scoped_ptr<TCPSocketLibevent>* accept_socket_;
62 IPEndPoint* accept_address_; 177 IPEndPoint* accept_address_;
63 CompletionCallback accept_callback_; 178 CompletionCallback accept_callback_;
64 179
180 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
181 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
182
183 Watcher read_watcher_;
184 Watcher write_watcher_;
185
186 scoped_refptr<IOBuffer> read_buf_;
187 int read_buf_len_;
188
189 scoped_refptr<IOBuffer> write_buf_;
190 int write_buf_len_;
191
192 // External callback; called when read is complete.
193 CompletionCallback read_callback_;
194
195 // External callback; called when write or connect is complete.
196 CompletionCallback write_callback_;
197
198 // Enables experimental TCP FastOpen option.
199 const bool use_tcp_fastopen_;
200
201 // True when TCP FastOpen is in use and we have done the connect.
202 bool tcp_fastopen_connected_;
203
204 enum FastOpenStatus fast_open_status_;
205
206 // A connect operation is pending. In this case, |write_callback_| needs to be
207 // called when connect is complete.
208 bool waiting_connect_;
209
210 IPEndPoint peer_address_;
211 // The OS error that a connect attempt last completed with.
212 int connect_os_error_;
213
214 bool logging_multiple_connect_attempts_;
215
65 BoundNetLog net_log_; 216 BoundNetLog net_log_;
66 217
67 DISALLOW_COPY_AND_ASSIGN(TCPSocketLibevent); 218 DISALLOW_COPY_AND_ASSIGN(TCPSocketLibevent);
68 }; 219 };
69 220
70 } // namespace net 221 } // namespace net
71 222
72 #endif // NET_SOCKET_TCP_SOCKET_LIBEVENT_H_ 223 #endif // NET_SOCKET_TCP_SOCKET_LIBEVENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698