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_WIN_H_
6 #define NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_
10 #include "base/memory/scoped_ptr.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 class NET_EXPORT TCPClientSocketWin
: public StreamSocket
,
22 NON_EXPORTED_BASE(base::NonThreadSafe
) {
24 // The IP address(es) and port number to connect to. The TCP socket will try
25 // each IP address in the list until it succeeds in establishing a
27 TCPClientSocketWin(const AddressList
& addresses
,
29 const net::NetLog::Source
& source
);
31 virtual ~TCPClientSocketWin();
33 // AdoptSocket causes the given, connected socket to be adopted as a TCP
34 // socket. This object must not be connected. This object takes ownership of
35 // the given socket and then acts as if Connect() had been called. This
36 // function is used by TCPServerSocket() to adopt accepted connections
38 int AdoptSocket(SOCKET socket
);
40 // Binds the socket to a local IP address and port.
41 int Bind(const IPEndPoint
& address
);
43 // StreamSocket implementation.
44 virtual int Connect(const CompletionCallback
& callback
);
45 virtual void Disconnect();
46 virtual bool IsConnected() const;
47 virtual bool IsConnectedAndIdle() const;
48 virtual int GetPeerAddress(IPEndPoint
* address
) const;
49 virtual int GetLocalAddress(IPEndPoint
* address
) const;
50 virtual const BoundNetLog
& NetLog() const { return net_log_
; }
51 virtual void SetSubresourceSpeculation();
52 virtual void SetOmniboxSpeculation();
53 virtual bool WasEverUsed() const;
54 virtual bool UsingTCPFastOpen() const;
55 virtual int64
NumBytesRead() const;
56 virtual base::TimeDelta
GetConnectTimeMicros() const;
57 virtual bool WasNpnNegotiated() const OVERRIDE
;
58 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
59 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
61 // Socket implementation.
62 // Multiple outstanding requests are not supported.
63 // Full duplex mode (reading and writing at the same time) is supported
64 virtual int Read(IOBuffer
* buf
, int buf_len
,
65 const CompletionCallback
& callback
);
66 virtual int Write(IOBuffer
* buf
, int buf_len
,
67 const CompletionCallback
& callback
);
69 virtual bool SetReceiveBufferSize(int32 size
);
70 virtual bool SetSendBufferSize(int32 size
);
72 virtual bool SetKeepAlive(bool enable
, int delay
);
73 virtual bool SetNoDelay(bool no_delay
);
75 // Perform reads in non-blocking mode instead of overlapped mode.
76 // Used for experiments.
77 static void DisableOverlappedReads();
80 // State machine for connecting the socket.
82 CONNECT_STATE_CONNECT
,
83 CONNECT_STATE_CONNECT_COMPLETE
,
89 // State machine used by Connect().
90 int DoConnectLoop(int result
);
92 int DoConnectComplete(int result
);
94 // Helper used by Disconnect(), which disconnects minus the logging and
95 // resetting of current_address_index_.
98 // Returns true if a Connect() is in progress.
99 bool waiting_connect() const {
100 return next_connect_state_
!= CONNECT_STATE_NONE
;
103 // Called after Connect() has completed with |net_error|.
104 void LogConnectCompletion(int net_error
);
106 int DoRead(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
107 void DoReadCallback(int rv
);
108 void DoWriteCallback(int rv
);
109 void DidCompleteConnect();
110 void DidCompleteRead();
111 void DidCompleteWrite();
112 void DidSignalRead();
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).
118 scoped_ptr
<IPEndPoint
> bind_address_
;
120 // Stores bound socket between Bind() and Connect() calls.
121 SOCKET bound_socket_
;
123 // The list of addresses we should try in order to establish a connection.
124 AddressList addresses_
;
126 // Where we are in above list. Set to -1 if uninitialized.
127 int current_address_index_
;
129 // The various states that the socket could be in.
133 // The core of the socket that can live longer than the socket itself. We pass
134 // resources to the Windows async IO functions and we have to make sure that
135 // they are not destroyed while the OS still references them.
136 scoped_refptr
<Core
> core_
;
138 // External callback; called when connect or read is complete.
139 CompletionCallback read_callback_
;
141 // External callback; called when write is complete.
142 CompletionCallback write_callback_
;
144 // The next state for the Connect() state machine.
145 ConnectState next_connect_state_
;
147 // The OS error that CONNECT_STATE_CONNECT last completed with.
148 int connect_os_error_
;
150 BoundNetLog net_log_
;
152 // This socket was previously disconnected and has not been re-connected.
153 bool previously_disconnected_
;
155 // Record of connectivity and transmissions, for use in speculative connection
157 UseHistory use_history_
;
159 base::TimeTicks connect_start_time_
;
160 base::TimeDelta connect_time_micros_
;
161 int64 num_bytes_read_
;
163 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketWin
);
168 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_