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. 4 5#ifndef NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_ 6#define NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_ 7 8#include <winsock2.h> 9 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" 16 17namespace net { 18 19class BoundNetLog; 20 21class NET_EXPORT TCPClientSocketWin : public StreamSocket, 22 NON_EXPORTED_BASE(base::NonThreadSafe) { 23 public: 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 26 // connection. 27 TCPClientSocketWin(const AddressList& addresses, 28 net::NetLog* net_log, 29 const net::NetLog::Source& source); 30 31 virtual ~TCPClientSocketWin(); 32 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 37 // and for testing. 38 int AdoptSocket(SOCKET socket); 39 40 // Binds the socket to a local IP address and port. 41 int Bind(const IPEndPoint& address); 42 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; 58 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); 66 67 virtual bool SetReceiveBufferSize(int32 size); 68 virtual bool SetSendBufferSize(int32 size); 69 70 virtual bool SetKeepAlive(bool enable, int delay); 71 virtual bool SetNoDelay(bool no_delay); 72 73 // Perform reads in non-blocking mode instead of overlapped mode. 74 // Used for experiments. 75 static void DisableOverlappedReads(); 76 77 private: 78 // State machine for connecting the socket. 79 enum ConnectState { 80 CONNECT_STATE_CONNECT, 81 CONNECT_STATE_CONNECT_COMPLETE, 82 CONNECT_STATE_NONE, 83 }; 84 85 class Core; 86 87 // State machine used by Connect(). 88 int DoConnectLoop(int result); 89 int DoConnect(); 90 int DoConnectComplete(int result); 91 92 // Helper used by Disconnect(), which disconnects minus the logging and 93 // resetting of current_address_index_. 94 void DoDisconnect(); 95 96 // Returns true if a Connect() is in progress. 97 bool waiting_connect() const { 98 return next_connect_state_ != CONNECT_STATE_NONE; 99 } 100 101 // Called after Connect() has completed with |net_error|. 102 void LogConnectCompletion(int net_error); 103 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(); 111 112 SOCKET socket_; 113 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_; 117 118 // Stores bound socket between Bind() and Connect() calls. 119 SOCKET bound_socket_; 120 121 // The list of addresses we should try in order to establish a connection. 122 AddressList addresses_; 123 124 // Where we are in above list. Set to -1 if uninitialized. 125 int current_address_index_; 126 127 // The various states that the socket could be in. 128 bool waiting_read_; 129 bool waiting_write_; 130 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_; 135 136 // External callback; called when connect or read is complete. 137 CompletionCallback read_callback_; 138 139 // External callback; called when write is complete. 140 CompletionCallback write_callback_; 141 142 // The next state for the Connect() state machine. 143 ConnectState next_connect_state_; 144 145 // The OS error that CONNECT_STATE_CONNECT last completed with. 146 int connect_os_error_; 147 148 BoundNetLog net_log_; 149 150 // This socket was previously disconnected and has not been re-connected. 151 bool previously_disconnected_; 152 153 // Record of connectivity and transmissions, for use in speculative connection 154 // histograms. 155 UseHistory use_history_; 156 157 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketWin); 158}; 159 160} // namespace net 161 162#endif // NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_ 163