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 bool WasNpnNegotiated() const OVERRIDE
;
56 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
57 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
59 // Socket implementation.
60 // Multiple outstanding requests are not supported.
61 // Full duplex mode (reading and writing at the same time) is supported
62 virtual int Read(IOBuffer
* buf
, int buf_len
,
63 const CompletionCallback
& callback
);
64 virtual int Write(IOBuffer
* buf
, int buf_len
,
65 const CompletionCallback
& callback
);
67 virtual bool SetReceiveBufferSize(int32 size
);
68 virtual bool SetSendBufferSize(int32 size
);
70 virtual bool SetKeepAlive(bool enable
, int delay
);
71 virtual bool SetNoDelay(bool no_delay
);
73 // Perform reads in non-blocking mode instead of overlapped mode.
74 // Used for experiments.
75 static void DisableOverlappedReads();
78 // State machine for connecting the socket.
80 CONNECT_STATE_CONNECT
,
81 CONNECT_STATE_CONNECT_COMPLETE
,
87 // State machine used by Connect().
88 int DoConnectLoop(int result
);
90 int DoConnectComplete(int result
);
92 // Helper used by Disconnect(), which disconnects minus the logging and
93 // resetting of current_address_index_.
96 // Returns true if a Connect() is in progress.
97 bool waiting_connect() const {
98 return next_connect_state_
!= CONNECT_STATE_NONE
;
101 // Called after Connect() has completed with |net_error|.
102 void LogConnectCompletion(int net_error
);
104 int DoRead(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
105 void DoReadCallback(int rv
);
106 void DoWriteCallback(int rv
);
107 void DidCompleteConnect();
108 void DidCompleteRead();
109 void DidCompleteWrite();
110 void DidSignalRead();
114 // Local IP address and port we are bound to. Set to NULL if Bind()
115 // was't called (in that cases OS chooses address/port).
116 scoped_ptr
<IPEndPoint
> bind_address_
;
118 // Stores bound socket between Bind() and Connect() calls.
119 SOCKET bound_socket_
;
121 // The list of addresses we should try in order to establish a connection.
122 AddressList addresses_
;
124 // Where we are in above list. Set to -1 if uninitialized.
125 int current_address_index_
;
127 // The various states that the socket could be in.
131 // The core of the socket that can live longer than the socket itself. We pass
132 // resources to the Windows async IO functions and we have to make sure that
133 // they are not destroyed while the OS still references them.
134 scoped_refptr
<Core
> core_
;
136 // External callback; called when connect or read is complete.
137 CompletionCallback read_callback_
;
139 // External callback; called when write is complete.
140 CompletionCallback write_callback_
;
142 // The next state for the Connect() state machine.
143 ConnectState next_connect_state_
;
145 // The OS error that CONNECT_STATE_CONNECT last completed with.
146 int connect_os_error_
;
148 BoundNetLog net_log_
;
150 // This socket was previously disconnected and has not been re-connected.
151 bool previously_disconnected_
;
153 // Record of connectivity and transmissions, for use in speculative connection
155 UseHistory use_history_
;
157 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketWin
);
162 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_