OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_CLIENT_SOCKET_H_ | 5 #ifndef NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
6 #define NET_SOCKET_TCP_CLIENT_SOCKET_H_ | 6 #define NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "net/base/net_export.h" | 9 #include "net/base/net_export.h" |
10 | 10 |
11 // TODO(yzshen): Switch OS_POSIX to use the same platform-independent | |
12 // TCPClientSocket. | |
11 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
12 #include "net/socket/tcp_client_socket_win.h" | 14 |
15 #include "base/basictypes.h" | |
16 #include "base/compiler_specific.h" | |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "net/base/address_list.h" | |
19 #include "net/base/completion_callback.h" | |
20 #include "net/base/net_log.h" | |
21 #include "net/socket/stream_socket.h" | |
22 #include "net/socket/tcp_socket.h" | |
23 | |
13 #elif defined(OS_POSIX) | 24 #elif defined(OS_POSIX) |
akalin
2013/09/05 22:52:28
test for OS_POSIX first to match block below?
yzshen1
2013/09/06 00:57:44
Done. It will be removed in the next CL.
| |
14 #include "net/socket/tcp_client_socket_libevent.h" | 25 #include "net/socket/tcp_client_socket_libevent.h" |
15 #endif | 26 #endif |
16 | 27 |
17 namespace net { | 28 namespace net { |
18 | 29 |
19 // A client socket that uses TCP as the transport layer. | |
20 #if defined(OS_WIN) | |
21 typedef TCPClientSocketWin TCPClientSocket; | |
22 #elif defined(OS_POSIX) | |
23 typedef TCPClientSocketLibevent TCPClientSocket; | |
24 #endif | |
25 | |
26 // Enable/disable experimental TCP FastOpen option. | 30 // Enable/disable experimental TCP FastOpen option. |
27 // Not thread safe. Must be called during initialization/startup only. | 31 // Not thread safe. Must be called during initialization/startup only. |
28 NET_EXPORT void SetTCPFastOpenEnabled(bool value); | 32 NET_EXPORT void SetTCPFastOpenEnabled(bool value); |
29 | 33 |
30 // Check if the TCP FastOpen option is enabled. | 34 // Check if the TCP FastOpen option is enabled. |
31 bool IsTCPFastOpenEnabled(); | 35 bool IsTCPFastOpenEnabled(); |
32 | 36 |
37 // A client socket that uses TCP as the transport layer. | |
38 #if defined(OS_POSIX) | |
39 typedef TCPClientSocketLibevent TCPClientSocket; | |
40 #elif defined(OS_WIN) | |
41 | |
42 class NET_EXPORT TCPClientSocket : public StreamSocket { | |
43 public: | |
44 // The IP address(es) and port number to connect to. The TCP socket will try | |
45 // each IP address in the list until it succeeds in establishing a | |
46 // connection. | |
47 TCPClientSocket(const AddressList& addresses, | |
48 net::NetLog* net_log, | |
49 const net::NetLog::Source& source); | |
50 | |
51 // Adopts the given, connected socket. This object takes ownership of the | |
52 // given socket and then acts as if Connect() had been called. This function | |
akalin
2013/09/05 22:52:28
No need to mention ownership, since scoped_ptr<> i
yzshen1
2013/09/06 00:57:44
Done.
| |
53 // is used by TCPServerSocket to adopt accepted connections and for testing. | |
54 explicit TCPClientSocket(scoped_ptr<TCPSocket> connected_socket, | |
akalin
2013/09/05 22:52:28
no need for explicit
yzshen1
2013/09/06 00:57:44
Done.
| |
55 const IPEndPoint& peer_address); | |
56 | |
57 virtual ~TCPClientSocket(); | |
58 | |
59 // Binds the socket to a local IP address and port. | |
60 int Bind(const IPEndPoint& address); | |
61 | |
62 // StreamSocket implementation. | |
63 virtual int Connect(const CompletionCallback& callback); | |
64 virtual void Disconnect(); | |
65 virtual bool IsConnected() const; | |
66 virtual bool IsConnectedAndIdle() const; | |
67 virtual int GetPeerAddress(IPEndPoint* address) const; | |
68 virtual int GetLocalAddress(IPEndPoint* address) const; | |
69 virtual const BoundNetLog& NetLog() const { return socket_->net_log(); } | |
70 virtual void SetSubresourceSpeculation(); | |
71 virtual void SetOmniboxSpeculation(); | |
72 virtual bool WasEverUsed() const; | |
73 virtual bool UsingTCPFastOpen() const; | |
74 virtual bool WasNpnNegotiated() const OVERRIDE; | |
75 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; | |
76 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; | |
77 | |
78 // Socket implementation. | |
79 // Multiple outstanding requests are not supported. | |
80 // Full duplex mode (reading and writing at the same time) is supported | |
akalin
2013/09/05 22:52:28
period at end
yzshen1
2013/09/06 00:57:44
Done.
| |
81 virtual int Read(IOBuffer* buf, int buf_len, | |
82 const CompletionCallback& callback) OVERRIDE; | |
83 virtual int Write(IOBuffer* buf, int buf_len, | |
84 const CompletionCallback& callback) OVERRIDE; | |
85 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; | |
86 virtual bool SetSendBufferSize(int32 size) OVERRIDE; | |
87 | |
88 virtual bool SetKeepAlive(bool enable, int delay); | |
89 virtual bool SetNoDelay(bool no_delay); | |
90 | |
91 private: | |
92 // State machine for connecting the socket. | |
93 enum ConnectState { | |
94 CONNECT_STATE_CONNECT, | |
95 CONNECT_STATE_CONNECT_COMPLETE, | |
96 CONNECT_STATE_NONE, | |
97 }; | |
98 | |
99 // State machine used by Connect(). | |
100 int DoConnectLoop(int result); | |
101 int DoConnect(); | |
102 int DoConnectComplete(int result); | |
103 | |
104 // Helper used by Disconnect(), which disconnects minus resetting | |
105 // current_address_index_ and bind_address_. | |
106 void DoDisconnect(); | |
107 | |
108 void DidCompleteConnect(int result); | |
109 void DidCompleteReadWrite(const CompletionCallback& forward_callback, | |
110 int result); | |
111 | |
112 int CreateSocket(AddressFamily family); | |
113 | |
114 scoped_ptr<TCPSocket> socket_; | |
115 | |
116 // Local IP address and port we are bound to. Set to NULL if Bind() | |
117 // was't called (in that cases OS chooses address/port). | |
akalin
2013/09/05 22:52:28
was't -> wasn't
in that cases -> in that case
yzshen1
2013/09/06 00:57:44
Done.
| |
118 scoped_ptr<IPEndPoint> bind_address_; | |
119 | |
120 // The list of addresses we should try in order to establish a connection. | |
121 AddressList addresses_; | |
122 | |
123 // Where we are in above list. Set to -1 if uninitialized. | |
124 int current_address_index_; | |
125 | |
126 // External callback; called when connect is complete. | |
127 CompletionCallback connect_callback_; | |
128 | |
129 // The next state for the Connect() state machine. | |
130 ConnectState next_connect_state_; | |
131 | |
132 // This socket was previously disconnected and has not been re-connected. | |
133 bool previously_disconnected_; | |
134 | |
135 // Record of connectivity and transmissions, for use in speculative connection | |
136 // histograms. | |
137 UseHistory use_history_; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket); | |
140 }; | |
141 | |
142 #endif | |
143 | |
33 } // namespace net | 144 } // namespace net |
34 | 145 |
35 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ | 146 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
OLD | NEW |