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 #if defined(OS_WIN) | 11 // TODO(yzshen): Switch OS_POSIX to use the same platform-independent |
12 #include "net/socket/tcp_client_socket_win.h" | 12 // TCPClientSocket. |
13 #elif defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
14 | |
14 #include "net/socket/tcp_client_socket_libevent.h" | 15 #include "net/socket/tcp_client_socket_libevent.h" |
16 | |
17 #elif defined(OS_WIN) | |
18 | |
19 #include "base/basictypes.h" | |
20 #include "base/compiler_specific.h" | |
21 #include "base/memory/scoped_ptr.h" | |
22 #include "net/base/address_list.h" | |
23 #include "net/base/completion_callback.h" | |
24 #include "net/base/net_log.h" | |
25 #include "net/socket/stream_socket.h" | |
26 #include "net/socket/tcp_socket.h" | |
27 | |
15 #endif | 28 #endif |
16 | 29 |
17 namespace net { | 30 namespace net { |
18 | 31 |
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. | 32 // Enable/disable experimental TCP FastOpen option. |
27 // Not thread safe. Must be called during initialization/startup only. | 33 // Not thread safe. Must be called during initialization/startup only. |
28 NET_EXPORT void SetTCPFastOpenEnabled(bool value); | 34 NET_EXPORT void SetTCPFastOpenEnabled(bool value); |
29 | 35 |
30 // Check if the TCP FastOpen option is enabled. | 36 // Check if the TCP FastOpen option is enabled. |
31 bool IsTCPFastOpenEnabled(); | 37 bool IsTCPFastOpenEnabled(); |
32 | 38 |
39 // A client socket that uses TCP as the transport layer. | |
40 #if defined(OS_POSIX) | |
41 typedef TCPClientSocketLibevent TCPClientSocket; | |
42 #elif defined(OS_WIN) | |
43 | |
44 class NET_EXPORT TCPClientSocket : public StreamSocket { | |
45 public: | |
46 // The IP address(es) and port number to connect to. The TCP socket will try | |
47 // each IP address in the list until it succeeds in establishing a | |
48 // connection. | |
49 TCPClientSocket(const AddressList& addresses, | |
50 net::NetLog* net_log, | |
51 const net::NetLog::Source& source); | |
52 | |
53 // Adopts the given, connected socket and then acts as if Connect() had been | |
54 // called. This function is used by TCPServerSocket and for testing. | |
55 TCPClientSocket(scoped_ptr<TCPSocket> connected_socket, | |
56 const IPEndPoint& peer_address); | |
57 | |
58 virtual ~TCPClientSocket(); | |
59 | |
60 // Binds the socket to a local IP address and port. | |
61 int Bind(const IPEndPoint& address); | |
62 | |
63 // StreamSocket implementation. | |
64 virtual int Connect(const CompletionCallback& callback); | |
akalin
2013/09/12 23:21:37
can you mark these functions as OVERRIDE here? (I
yzshen1
2013/09/13 05:07:52
Done.
| |
65 virtual void Disconnect(); | |
66 virtual bool IsConnected() const; | |
67 virtual bool IsConnectedAndIdle() const; | |
68 virtual int GetPeerAddress(IPEndPoint* address) const; | |
69 virtual int GetLocalAddress(IPEndPoint* address) const; | |
70 virtual const BoundNetLog& NetLog() const { return socket_->net_log(); } | |
71 virtual void SetSubresourceSpeculation(); | |
72 virtual void SetOmniboxSpeculation(); | |
73 virtual bool WasEverUsed() const; | |
74 virtual bool UsingTCPFastOpen() const; | |
75 virtual bool WasNpnNegotiated() const OVERRIDE; | |
76 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; | |
77 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; | |
78 | |
79 // Socket implementation. | |
80 // Multiple outstanding requests are not supported. | |
81 // Full duplex mode (reading and writing at the same time) is supported. | |
82 virtual int Read(IOBuffer* buf, int buf_len, | |
83 const CompletionCallback& callback) OVERRIDE; | |
84 virtual int Write(IOBuffer* buf, int buf_len, | |
85 const CompletionCallback& callback) OVERRIDE; | |
86 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; | |
87 virtual bool SetSendBufferSize(int32 size) OVERRIDE; | |
88 | |
89 virtual bool SetKeepAlive(bool enable, int delay); | |
90 virtual bool SetNoDelay(bool no_delay); | |
91 | |
92 private: | |
93 // State machine for connecting the socket. | |
94 enum ConnectState { | |
95 CONNECT_STATE_CONNECT, | |
96 CONNECT_STATE_CONNECT_COMPLETE, | |
97 CONNECT_STATE_NONE, | |
98 }; | |
99 | |
100 // State machine used by Connect(). | |
101 int DoConnectLoop(int result); | |
102 int DoConnect(); | |
103 int DoConnectComplete(int result); | |
104 | |
105 // Helper used by Disconnect(), which disconnects minus resetting | |
106 // current_address_index_ and bind_address_. | |
107 void DoDisconnect(); | |
108 | |
109 void DidCompleteConnect(int result); | |
110 void DidCompleteReadWrite(const CompletionCallback& callback, int result); | |
111 | |
112 int OpenSocket(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 // wasn't called (in that case OS chooses address/port). | |
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 |