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/log/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 ~TCPSocketWin() override
;
33 int Open(AddressFamily family
);
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
);
42 int Bind(const IPEndPoint
& address
);
44 int Listen(int backlog
);
45 int Accept(scoped_ptr
<TCPSocketWin
>* socket
,
47 const CompletionCallback
& callback
);
49 int Connect(const IPEndPoint
& address
, const CompletionCallback
& callback
);
50 bool IsConnected() const;
51 bool IsConnectedAndIdle() const;
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
);
58 int GetLocalAddress(IPEndPoint
* address
) const;
59 int GetPeerAddress(IPEndPoint
* address
) const;
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
);
76 // Gets the estimated RTT. Returns false if the RTT is
77 // unavailable. May also return false when estimated RTT is 0.
78 bool GetEstimatedRoundTripTime(base::TimeDelta
* out_rtt
) const
83 // Setter/Getter methods for TCP FastOpen socket option.
84 // NOOPs since TCP FastOpen is not implemented in Windows.
85 bool UsingTCPFastOpen() const { return false; }
86 void EnableTCPFastOpenIfSupported() {}
88 bool IsValid() const { return socket_
!= INVALID_SOCKET
; }
90 // Marks the start/end of a series of connect attempts for logging purpose.
92 // TCPClientSocket may attempt to connect to multiple addresses until it
93 // succeeds in establishing a connection. The corresponding log will have
94 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
95 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
96 // NetLog::TYPE_TCP_CONNECT.
98 // TODO(yzshen): Change logging format and let TCPClientSocket log the
99 // start/end of a series of connect attempts itself.
100 void StartLoggingMultipleConnectAttempts(const AddressList
& addresses
);
101 void EndLoggingMultipleConnectAttempts(int net_error
);
103 const BoundNetLog
& net_log() const { return net_log_
; }
108 // base::ObjectWatcher::Delegate implementation.
109 void OnObjectSignaled(HANDLE object
) override
;
111 int AcceptInternal(scoped_ptr
<TCPSocketWin
>* socket
,
112 IPEndPoint
* address
);
115 void DoConnectComplete(int result
);
117 void LogConnectBegin(const AddressList
& addresses
);
118 void LogConnectEnd(int net_error
);
120 int DoRead(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
121 void DidCompleteConnect();
122 void DidCompleteWrite();
123 void DidSignalRead();
127 HANDLE accept_event_
;
128 base::win::ObjectWatcher accept_watcher_
;
130 scoped_ptr
<TCPSocketWin
>* accept_socket_
;
131 IPEndPoint
* accept_address_
;
132 CompletionCallback accept_callback_
;
134 // The various states that the socket could be in.
135 bool waiting_connect_
;
139 // The core of the socket that can live longer than the socket itself. We pass
140 // resources to the Windows async IO functions and we have to make sure that
141 // they are not destroyed while the OS still references them.
142 scoped_refptr
<Core
> core_
;
144 // External callback; called when connect or read is complete.
145 CompletionCallback read_callback_
;
147 // External callback; called when write is complete.
148 CompletionCallback write_callback_
;
150 scoped_ptr
<IPEndPoint
> peer_address_
;
151 // The OS error that a connect attempt last completed with.
152 int connect_os_error_
;
154 bool logging_multiple_connect_attempts_
;
156 BoundNetLog net_log_
;
158 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin
);
163 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_