| OLD | NEW |
| (Empty) |
| 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 | |
| 17 namespace net { | |
| 18 | |
| 19 class BoundNetLog; | |
| 20 | |
| 21 class 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 private: | |
| 74 // State machine for connecting the socket. | |
| 75 enum ConnectState { | |
| 76 CONNECT_STATE_CONNECT, | |
| 77 CONNECT_STATE_CONNECT_COMPLETE, | |
| 78 CONNECT_STATE_NONE, | |
| 79 }; | |
| 80 | |
| 81 class Core; | |
| 82 | |
| 83 // State machine used by Connect(). | |
| 84 int DoConnectLoop(int result); | |
| 85 int DoConnect(); | |
| 86 int DoConnectComplete(int result); | |
| 87 | |
| 88 // Helper used by Disconnect(), which disconnects minus the logging and | |
| 89 // resetting of current_address_index_. | |
| 90 void DoDisconnect(); | |
| 91 | |
| 92 // Returns true if a Connect() is in progress. | |
| 93 bool waiting_connect() const { | |
| 94 return next_connect_state_ != CONNECT_STATE_NONE; | |
| 95 } | |
| 96 | |
| 97 // Called after Connect() has completed with |net_error|. | |
| 98 void LogConnectCompletion(int net_error); | |
| 99 | |
| 100 int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
| 101 void DoReadCallback(int rv); | |
| 102 void DoWriteCallback(int rv); | |
| 103 void DidCompleteConnect(); | |
| 104 void DidCompleteWrite(); | |
| 105 void DidSignalRead(); | |
| 106 | |
| 107 SOCKET socket_; | |
| 108 | |
| 109 // Local IP address and port we are bound to. Set to NULL if Bind() | |
| 110 // was't called (in that cases OS chooses address/port). | |
| 111 scoped_ptr<IPEndPoint> bind_address_; | |
| 112 | |
| 113 // Stores bound socket between Bind() and Connect() calls. | |
| 114 SOCKET bound_socket_; | |
| 115 | |
| 116 // The list of addresses we should try in order to establish a connection. | |
| 117 AddressList addresses_; | |
| 118 | |
| 119 // Where we are in above list. Set to -1 if uninitialized. | |
| 120 int current_address_index_; | |
| 121 | |
| 122 // The various states that the socket could be in. | |
| 123 bool waiting_read_; | |
| 124 bool waiting_write_; | |
| 125 | |
| 126 // The core of the socket that can live longer than the socket itself. We pass | |
| 127 // resources to the Windows async IO functions and we have to make sure that | |
| 128 // they are not destroyed while the OS still references them. | |
| 129 scoped_refptr<Core> core_; | |
| 130 | |
| 131 // External callback; called when connect or read is complete. | |
| 132 CompletionCallback read_callback_; | |
| 133 | |
| 134 // External callback; called when write is complete. | |
| 135 CompletionCallback write_callback_; | |
| 136 | |
| 137 // The next state for the Connect() state machine. | |
| 138 ConnectState next_connect_state_; | |
| 139 | |
| 140 // The OS error that CONNECT_STATE_CONNECT last completed with. | |
| 141 int connect_os_error_; | |
| 142 | |
| 143 BoundNetLog net_log_; | |
| 144 | |
| 145 // This socket was previously disconnected and has not been re-connected. | |
| 146 bool previously_disconnected_; | |
| 147 | |
| 148 // Record of connectivity and transmissions, for use in speculative connection | |
| 149 // histograms. | |
| 150 UseHistory use_history_; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketWin); | |
| 153 }; | |
| 154 | |
| 155 } // namespace net | |
| 156 | |
| 157 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_WIN_H_ | |
| OLD | NEW |