Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / socket / tcp_socket_win.h
blob1012259806cd0e773d4ddeba45fc45d6d9ca1f4c
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_
8 #include <winsock2.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"
21 namespace net {
23 class AddressList;
24 class IOBuffer;
25 class IPEndPoint;
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 ~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,
46 IPEndPoint* address,
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 void Close();
78 // Setter/Getter methods for TCP FastOpen socket option.
79 // NOOPs since TCP FastOpen is not implemented in Windows.
80 bool UsingTCPFastOpen() const { return false; }
81 void EnableTCPFastOpenIfSupported() {}
83 bool IsValid() const { return socket_ != INVALID_SOCKET; }
85 // Marks the start/end of a series of connect attempts for logging purpose.
87 // TCPClientSocket may attempt to connect to multiple addresses until it
88 // succeeds in establishing a connection. The corresponding log will have
89 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
90 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
91 // NetLog::TYPE_TCP_CONNECT.
93 // TODO(yzshen): Change logging format and let TCPClientSocket log the
94 // start/end of a series of connect attempts itself.
95 void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
96 void EndLoggingMultipleConnectAttempts(int net_error);
98 const BoundNetLog& net_log() const { return net_log_; }
100 private:
101 class Core;
103 // base::ObjectWatcher::Delegate implementation.
104 void OnObjectSignaled(HANDLE object) override;
106 int AcceptInternal(scoped_ptr<TCPSocketWin>* socket,
107 IPEndPoint* address);
109 int DoConnect();
110 void DoConnectComplete(int result);
112 void LogConnectBegin(const AddressList& addresses);
113 void LogConnectEnd(int net_error);
115 int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
116 void DidCompleteConnect();
117 void DidCompleteWrite();
118 void DidSignalRead();
120 SOCKET socket_;
122 HANDLE accept_event_;
123 base::win::ObjectWatcher accept_watcher_;
125 scoped_ptr<TCPSocketWin>* accept_socket_;
126 IPEndPoint* accept_address_;
127 CompletionCallback accept_callback_;
129 // The various states that the socket could be in.
130 bool waiting_connect_;
131 bool waiting_read_;
132 bool waiting_write_;
134 // The core of the socket that can live longer than the socket itself. We pass
135 // resources to the Windows async IO functions and we have to make sure that
136 // they are not destroyed while the OS still references them.
137 scoped_refptr<Core> core_;
139 // External callback; called when connect or read is complete.
140 CompletionCallback read_callback_;
142 // External callback; called when write is complete.
143 CompletionCallback write_callback_;
145 scoped_ptr<IPEndPoint> peer_address_;
146 // The OS error that a connect attempt last completed with.
147 int connect_os_error_;
149 bool logging_multiple_connect_attempts_;
151 BoundNetLog net_log_;
153 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin);
156 } // namespace net
158 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_