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.
5 #ifndef NET_SOCKET_TCP_SOCKET_WIN_H_
6 #define NET_SOCKET_TCP_SOCKET_WIN_H_
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"
27 class NET_EXPORT TCPSocketWin
: NON_EXPORTED_BASE(public base::NonThreadSafe
),
28 public base::win::ObjectWatcher::Delegate
{
30 TCPSocketWin(NetLog
* net_log
, const NetLog::Source
& source
);
31 virtual ~TCPSocketWin();
33 int Open(AddressFamily family
);
34 // Takes ownership of |socket|.
35 int AdoptConnectedSocket(SOCKET socket
, const IPEndPoint
& peer_address
);
37 int Bind(const IPEndPoint
& address
);
39 int Listen(int backlog
);
40 int Accept(scoped_ptr
<TCPSocketWin
>* socket
,
42 const CompletionCallback
& callback
);
44 int Connect(const IPEndPoint
& address
, const CompletionCallback
& callback
);
45 bool IsConnected() const;
46 bool IsConnectedAndIdle() const;
48 // Multiple outstanding requests are not supported.
49 // Full duplex mode (reading and writing at the same time) is supported.
50 int Read(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
51 int Write(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
53 int GetLocalAddress(IPEndPoint
* address
) const;
54 int GetPeerAddress(IPEndPoint
* address
) const;
56 // Sets various socket options.
57 // The commonly used options for server listening sockets:
58 // - SetExclusiveAddrUse().
59 int SetDefaultOptionsForServer();
60 // The commonly used options for client sockets and accepted sockets:
61 // - Increase the socket buffer sizes for WinXP;
62 // - SetNoDelay(true);
63 // - SetKeepAlive(true, 45).
64 void SetDefaultOptionsForClient();
65 int SetExclusiveAddrUse();
66 bool SetReceiveBufferSize(int32 size
);
67 bool SetSendBufferSize(int32 size
);
68 bool SetKeepAlive(bool enable
, int delay
);
69 bool SetNoDelay(bool no_delay
);
73 bool UsingTCPFastOpen() const;
74 bool IsValid() const { return socket_
!= INVALID_SOCKET
; }
76 // Marks the start/end of a series of connect attempts for logging purpose.
78 // TCPClientSocket may attempt to connect to multiple addresses until it
79 // succeeds in establishing a connection. The corresponding log will have
80 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
81 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
82 // NetLog::TYPE_TCP_CONNECT.
84 // TODO(yzshen): Change logging format and let TCPClientSocket log the
85 // start/end of a series of connect attempts itself.
86 void StartLoggingMultipleConnectAttempts(const AddressList
& addresses
);
87 void EndLoggingMultipleConnectAttempts(int net_error
);
89 const BoundNetLog
& net_log() const { return net_log_
; }
94 // base::ObjectWatcher::Delegate implementation.
95 virtual void OnObjectSignaled(HANDLE object
) OVERRIDE
;
97 int AcceptInternal(scoped_ptr
<TCPSocketWin
>* socket
,
101 void DoConnectComplete(int result
);
103 void LogConnectBegin(const AddressList
& addresses
);
104 void LogConnectEnd(int net_error
);
106 int DoRead(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
107 void DidCompleteConnect();
108 void DidCompleteWrite();
109 void DidSignalRead();
113 HANDLE accept_event_
;
114 base::win::ObjectWatcher accept_watcher_
;
116 scoped_ptr
<TCPSocketWin
>* accept_socket_
;
117 IPEndPoint
* accept_address_
;
118 CompletionCallback accept_callback_
;
120 // The various states that the socket could be in.
121 bool waiting_connect_
;
125 // The core of the socket that can live longer than the socket itself. We pass
126 // resources to the Windows async IO functions and we have to make sure that
127 // they are not destroyed while the OS still references them.
128 scoped_refptr
<Core
> core_
;
130 // External callback; called when connect or read is complete.
131 CompletionCallback read_callback_
;
133 // External callback; called when write is complete.
134 CompletionCallback write_callback_
;
136 scoped_ptr
<IPEndPoint
> peer_address_
;
137 // The OS error that a connect attempt last completed with.
138 int connect_os_error_
;
140 bool logging_multiple_connect_attempts_
;
142 BoundNetLog net_log_
;
144 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin
);
149 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_