1 // Copyright (c) 2012 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_CLIENT_SOCKET_LIBEVENT_H_
6 #define NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/threading/non_thread_safe.h"
12 #include "net/base/address_list.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/net_log.h"
15 #include "net/socket/stream_socket.h"
21 // A client socket that uses TCP as the transport layer.
22 class NET_EXPORT_PRIVATE TCPClientSocketLibevent
: public StreamSocket
,
23 public base::NonThreadSafe
{
25 // The IP address(es) and port number to connect to. The TCP socket will try
26 // each IP address in the list until it succeeds in establishing a
28 TCPClientSocketLibevent(const AddressList
& addresses
,
30 const net::NetLog::Source
& source
);
32 virtual ~TCPClientSocketLibevent();
34 // AdoptSocket causes the given, connected socket to be adopted as a TCP
35 // socket. This object must not be connected. This object takes ownership of
36 // the given socket and then acts as if Connect() had been called. This
37 // function is used by TCPServerSocket() to adopt accepted connections
39 int AdoptSocket(int socket
);
41 // Binds the socket to a local IP address and port.
42 int Bind(const IPEndPoint
& address
);
44 // StreamSocket implementation.
45 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
46 virtual void Disconnect() OVERRIDE
;
47 virtual bool IsConnected() const OVERRIDE
;
48 virtual bool IsConnectedAndIdle() const OVERRIDE
;
49 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
50 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
51 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
52 virtual void SetSubresourceSpeculation() OVERRIDE
;
53 virtual void SetOmniboxSpeculation() OVERRIDE
;
54 virtual bool WasEverUsed() const OVERRIDE
;
55 virtual bool UsingTCPFastOpen() const OVERRIDE
;
56 virtual int64
NumBytesRead() const OVERRIDE
;
57 virtual base::TimeDelta
GetConnectTimeMicros() const OVERRIDE
;
58 virtual bool WasNpnNegotiated() const OVERRIDE
;
59 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
60 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
62 // Socket implementation.
63 // Multiple outstanding requests are not supported.
64 // Full duplex mode (reading and writing at the same time) is supported
65 virtual int Read(IOBuffer
* buf
,
67 const CompletionCallback
& callback
) OVERRIDE
;
68 virtual int Write(IOBuffer
* buf
,
70 const CompletionCallback
& callback
) OVERRIDE
;
71 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
72 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
74 virtual bool SetKeepAlive(bool enable
, int delay
);
75 virtual bool SetNoDelay(bool no_delay
);
78 // State machine for connecting the socket.
80 CONNECT_STATE_CONNECT
,
81 CONNECT_STATE_CONNECT_COMPLETE
,
85 class ReadWatcher
: public MessageLoopForIO::Watcher
{
87 explicit ReadWatcher(TCPClientSocketLibevent
* socket
) : socket_(socket
) {}
89 // MessageLoopForIO::Watcher methods
91 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE
;
93 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
{}
96 TCPClientSocketLibevent
* const socket_
;
98 DISALLOW_COPY_AND_ASSIGN(ReadWatcher
);
101 class WriteWatcher
: public MessageLoopForIO::Watcher
{
103 explicit WriteWatcher(TCPClientSocketLibevent
* socket
) : socket_(socket
) {}
105 // MessageLoopForIO::Watcher implementation.
106 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE
{}
107 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE
;
110 TCPClientSocketLibevent
* const socket_
;
112 DISALLOW_COPY_AND_ASSIGN(WriteWatcher
);
115 // State machine used by Connect().
116 int DoConnectLoop(int result
);
118 int DoConnectComplete(int result
);
120 // Helper used by Disconnect(), which disconnects minus the logging and
121 // resetting of current_address_index_.
124 void DoReadCallback(int rv
);
125 void DoWriteCallback(int rv
);
126 void DidCompleteRead();
127 void DidCompleteWrite();
128 void DidCompleteConnect();
130 // Returns true if a Connect() is in progress.
131 bool waiting_connect() const {
132 return next_connect_state_
!= CONNECT_STATE_NONE
;
135 // Helper to add a TCP_CONNECT (end) event to the NetLog.
136 void LogConnectCompletion(int net_error
);
138 // Internal function to write to a socket.
139 int InternalWrite(IOBuffer
* buf
, int buf_len
);
143 // Local IP address and port we are bound to. Set to NULL if Bind()
144 // was't called (in that cases OS chooses address/port).
145 scoped_ptr
<IPEndPoint
> bind_address_
;
147 // Stores bound socket between Bind() and Connect() calls.
150 // The list of addresses we should try in order to establish a connection.
151 AddressList addresses_
;
153 // Where we are in above list. Set to -1 if uninitialized.
154 int current_address_index_
;
156 // The socket's libevent wrappers
157 MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_
;
158 MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_
;
160 // The corresponding watchers for reads and writes.
161 ReadWatcher read_watcher_
;
162 WriteWatcher write_watcher_
;
164 // The buffer used by OnSocketReady to retry Read requests
165 scoped_refptr
<IOBuffer
> read_buf_
;
168 // The buffer used by OnSocketReady to retry Write requests
169 scoped_refptr
<IOBuffer
> write_buf_
;
172 // External callback; called when read is complete.
173 CompletionCallback read_callback_
;
175 // External callback; called when write is complete.
176 CompletionCallback write_callback_
;
178 // The next state for the Connect() state machine.
179 ConnectState next_connect_state_
;
181 // The OS error that CONNECT_STATE_CONNECT last completed with.
182 int connect_os_error_
;
184 BoundNetLog net_log_
;
186 // This socket was previously disconnected and has not been re-connected.
187 bool previously_disconnected_
;
189 // Record of connectivity and transmissions, for use in speculative connection
191 UseHistory use_history_
;
193 // Enables experimental TCP FastOpen option.
194 bool use_tcp_fastopen_
;
196 // True when TCP FastOpen is in use and we have done the connect.
197 bool tcp_fastopen_connected_
;
199 base::TimeTicks connect_start_time_
;
200 base::TimeDelta connect_time_micros_
;
201 int64 num_bytes_read_
;
203 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketLibevent
);
208 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_LIBEVENT_H_