1 // Copyright (c) 2010 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 #pragma once 8 9 #include <winsock2.h> 10 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/client_socket.h" 16 17 namespace net { 18 19 class BoundNetLog; 20 21 class TCPClientSocketWin : public ClientSocket, base::NonThreadSafe { 22 public: 23 // The IP address(es) and port number to connect to. The TCP socket will try 24 // each IP address in the list until it succeeds in establishing a 25 // connection. 26 TCPClientSocketWin(const AddressList& addresses, 27 net::NetLog* net_log, 28 const net::NetLog::Source& source); 29 30 virtual ~TCPClientSocketWin(); 31 32 // AdoptSocket causes the given, connected socket to be adopted as a TCP 33 // socket. This object must not be connected. This object takes ownership of 34 // the given socket and then acts as if Connect() had been called. This 35 // function is used by TCPServerSocket() to adopt accepted connections 36 // and for testing. 37 void AdoptSocket(SOCKET socket); 38 39 // ClientSocket methods: 40 virtual int Connect(CompletionCallback* callback 41 #ifdef ANDROID 42 , bool wait_for_connect 43 #endif 44 ); 45 virtual void Disconnect(); 46 virtual bool IsConnected() const; 47 virtual bool IsConnectedAndIdle() const; 48 virtual int GetPeerAddress(AddressList* address) const; 49 virtual int GetLocalAddress(IPEndPoint* address) const; NetLog()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 56 // Socket methods: 57 // Multiple outstanding requests are not supported. 58 // Full duplex mode (reading and writing at the same time) is supported 59 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); 60 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); 61 62 virtual bool SetReceiveBufferSize(int32 size); 63 virtual bool SetSendBufferSize(int32 size); 64 65 private: 66 // State machine for connecting the socket. 67 enum ConnectState { 68 CONNECT_STATE_CONNECT, 69 CONNECT_STATE_CONNECT_COMPLETE, 70 CONNECT_STATE_NONE, 71 }; 72 73 class Core; 74 75 // State machine used by Connect(). 76 int DoConnectLoop(int result); 77 int DoConnect(); 78 int DoConnectComplete(int result); 79 80 // Helper used by Disconnect(), which disconnects minus the logging and 81 // resetting of current_ai_. 82 void DoDisconnect(); 83 84 // Returns true if a Connect() is in progress. waiting_connect()85 bool waiting_connect() const { 86 return next_connect_state_ != CONNECT_STATE_NONE; 87 } 88 89 // Returns the OS error code (or 0 on success). 90 int CreateSocket(const struct addrinfo* ai); 91 92 // Returns the OS error code (or 0 on success). 93 int SetupSocket(); 94 95 // Called after Connect() has completed with |net_error|. 96 void LogConnectCompletion(int net_error); 97 98 void DoReadCallback(int rv); 99 void DoWriteCallback(int rv); 100 void DidCompleteConnect(); 101 void DidCompleteRead(); 102 void DidCompleteWrite(); 103 104 SOCKET socket_; 105 106 // The list of addresses we should try in order to establish a connection. 107 AddressList addresses_; 108 109 // Where we are in above list, or NULL if all addrinfos have been tried. 110 const struct addrinfo* current_ai_; 111 112 // The various states that the socket could be in. 113 bool waiting_read_; 114 bool waiting_write_; 115 116 // The core of the socket that can live longer than the socket itself. We pass 117 // resources to the Windows async IO functions and we have to make sure that 118 // they are not destroyed while the OS still references them. 119 scoped_refptr<Core> core_; 120 121 // External callback; called when connect or read is complete. 122 CompletionCallback* read_callback_; 123 124 // External callback; called when write is complete. 125 CompletionCallback* write_callback_; 126 127 // The next state for the Connect() state machine. 128 ConnectState next_connect_state_; 129 130 // The OS error that CONNECT_STATE_CONNECT last completed with. 131 int connect_os_error_; 132 133 BoundNetLog net_log_; 134 135 // This socket was previously disconnected and has not been re-connected. 136 bool previously_disconnected_; 137 138 // Record of connectivity and transmissions, for use in speculative connection 139 // histograms. 140 UseHistory use_history_; 141 142 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketWin); 143 }; 144 145 } // namespace net 146 147 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_ 148