• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_SOCKET_WIN_H_
6 #define NET_SOCKET_TCP_SOCKET_WIN_H_
7 
8 #include <winsock2.h>
9 
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/win/object_watcher.h"
16 #include "net/base/address_family.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/net_export.h"
19 #include "net/base/net_log.h"
20 
21 namespace net {
22 
23 class AddressList;
24 class IOBuffer;
25 class IPEndPoint;
26 
NON_EXPORTED_BASE(public base::NonThreadSafe)27 class NET_EXPORT TCPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe),
28                                 public base::win::ObjectWatcher::Delegate  {
29  public:
30   TCPSocketWin(NetLog* net_log, const NetLog::Source& source);
31   virtual ~TCPSocketWin();
32 
33   int Open(AddressFamily family);
34 
35   // Both AdoptConnectedSocket and AdoptListenSocket take ownership of an
36   // existing socket. AdoptConnectedSocket takes an already connected
37   // socket. AdoptListenSocket takes a socket that is intended to accept
38   // connection. In some sense, AdoptListenSocket is more similar to Open.
39   int AdoptConnectedSocket(SOCKET socket, const IPEndPoint& peer_address);
40   int AdoptListenSocket(SOCKET socket);
41 
42   int Bind(const IPEndPoint& address);
43 
44   int Listen(int backlog);
45   int Accept(scoped_ptr<TCPSocketWin>* socket,
46              IPEndPoint* address,
47              const CompletionCallback& callback);
48 
49   int Connect(const IPEndPoint& address, const CompletionCallback& callback);
50   bool IsConnected() const;
51   bool IsConnectedAndIdle() const;
52 
53   // Multiple outstanding requests are not supported.
54   // Full duplex mode (reading and writing at the same time) is supported.
55   int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
56   int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
57 
58   int GetLocalAddress(IPEndPoint* address) const;
59   int GetPeerAddress(IPEndPoint* address) const;
60 
61   // Sets various socket options.
62   // The commonly used options for server listening sockets:
63   // - SetExclusiveAddrUse().
64   int SetDefaultOptionsForServer();
65   // The commonly used options for client sockets and accepted sockets:
66   // - Increase the socket buffer sizes for WinXP;
67   // - SetNoDelay(true);
68   // - SetKeepAlive(true, 45).
69   void SetDefaultOptionsForClient();
70   int SetExclusiveAddrUse();
71   int SetReceiveBufferSize(int32 size);
72   int SetSendBufferSize(int32 size);
73   bool SetKeepAlive(bool enable, int delay);
74   bool SetNoDelay(bool no_delay);
75 
76   void Close();
77 
78   bool UsingTCPFastOpen() const;
79   bool IsValid() const { return socket_ != INVALID_SOCKET; }
80 
81   // Marks the start/end of a series of connect attempts for logging purpose.
82   //
83   // TCPClientSocket may attempt to connect to multiple addresses until it
84   // succeeds in establishing a connection. The corresponding log will have
85   // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
86   // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
87   // NetLog::TYPE_TCP_CONNECT.
88   //
89   // TODO(yzshen): Change logging format and let TCPClientSocket log the
90   // start/end of a series of connect attempts itself.
91   void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
92   void EndLoggingMultipleConnectAttempts(int net_error);
93 
94   const BoundNetLog& net_log() const { return net_log_; }
95 
96  private:
97   class Core;
98 
99   // base::ObjectWatcher::Delegate implementation.
100   virtual void OnObjectSignaled(HANDLE object) OVERRIDE;
101 
102   int AcceptInternal(scoped_ptr<TCPSocketWin>* socket,
103                      IPEndPoint* address);
104 
105   int DoConnect();
106   void DoConnectComplete(int result);
107 
108   void LogConnectBegin(const AddressList& addresses);
109   void LogConnectEnd(int net_error);
110 
111   int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
112   void DidCompleteConnect();
113   void DidCompleteWrite();
114   void DidSignalRead();
115 
116   SOCKET socket_;
117 
118   HANDLE accept_event_;
119   base::win::ObjectWatcher accept_watcher_;
120 
121   scoped_ptr<TCPSocketWin>* accept_socket_;
122   IPEndPoint* accept_address_;
123   CompletionCallback accept_callback_;
124 
125   // The various states that the socket could be in.
126   bool waiting_connect_;
127   bool waiting_read_;
128   bool waiting_write_;
129 
130   // The core of the socket that can live longer than the socket itself. We pass
131   // resources to the Windows async IO functions and we have to make sure that
132   // they are not destroyed while the OS still references them.
133   scoped_refptr<Core> core_;
134 
135   // External callback; called when connect or read is complete.
136   CompletionCallback read_callback_;
137 
138   // External callback; called when write is complete.
139   CompletionCallback write_callback_;
140 
141   scoped_ptr<IPEndPoint> peer_address_;
142   // The OS error that a connect attempt last completed with.
143   int connect_os_error_;
144 
145   bool logging_multiple_connect_attempts_;
146 
147   BoundNetLog net_log_;
148 
149   DISALLOW_COPY_AND_ASSIGN(TCPSocketWin);
150 };
151 
152 }  // namespace net
153 
154 #endif  // NET_SOCKET_TCP_SOCKET_WIN_H_
155 
156