OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SOCKET_TCP_SOCKET_WIN_H_ | 5 #ifndef NET_SOCKET_TCP_SOCKET_WIN_H_ |
6 #define NET_SOCKET_TCP_SOCKET_WIN_H_ | 6 #define NET_SOCKET_TCP_SOCKET_WIN_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <winsock2.h> | 9 #include <winsock2.h> |
10 | 10 |
| 11 #include <memory> |
| 12 |
11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
16 #include "base/win/object_watcher.h" | 17 #include "base/win/object_watcher.h" |
17 #include "net/base/address_family.h" | 18 #include "net/base/address_family.h" |
18 #include "net/base/completion_callback.h" | 19 #include "net/base/completion_callback.h" |
19 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
20 #include "net/base/socket_performance_watcher.h" | 21 #include "net/base/socket_performance_watcher.h" |
21 #include "net/log/net_log.h" | 22 #include "net/log/net_log.h" |
22 | 23 |
23 namespace net { | 24 namespace net { |
24 | 25 |
25 class AddressList; | 26 class AddressList; |
26 class IOBuffer; | 27 class IOBuffer; |
27 class IPEndPoint; | 28 class IPEndPoint; |
28 | 29 |
29 class NET_EXPORT TCPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe), | 30 class NET_EXPORT TCPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe), |
30 public base::win::ObjectWatcher::Delegate { | 31 public base::win::ObjectWatcher::Delegate { |
31 public: | 32 public: |
32 TCPSocketWin(scoped_ptr<SocketPerformanceWatcher> socket_performance_watcher, | 33 TCPSocketWin( |
33 NetLog* net_log, | 34 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
34 const NetLog::Source& source); | 35 NetLog* net_log, |
| 36 const NetLog::Source& source); |
35 ~TCPSocketWin() override; | 37 ~TCPSocketWin() override; |
36 | 38 |
37 int Open(AddressFamily family); | 39 int Open(AddressFamily family); |
38 | 40 |
39 // Both AdoptConnectedSocket and AdoptListenSocket take ownership of an | 41 // Both AdoptConnectedSocket and AdoptListenSocket take ownership of an |
40 // existing socket. AdoptConnectedSocket takes an already connected | 42 // existing socket. AdoptConnectedSocket takes an already connected |
41 // socket. AdoptListenSocket takes a socket that is intended to accept | 43 // socket. AdoptListenSocket takes a socket that is intended to accept |
42 // connection. In some sense, AdoptListenSocket is more similar to Open. | 44 // connection. In some sense, AdoptListenSocket is more similar to Open. |
43 int AdoptConnectedSocket(SOCKET socket, const IPEndPoint& peer_address); | 45 int AdoptConnectedSocket(SOCKET socket, const IPEndPoint& peer_address); |
44 int AdoptListenSocket(SOCKET socket); | 46 int AdoptListenSocket(SOCKET socket); |
45 | 47 |
46 int Bind(const IPEndPoint& address); | 48 int Bind(const IPEndPoint& address); |
47 | 49 |
48 int Listen(int backlog); | 50 int Listen(int backlog); |
49 int Accept(scoped_ptr<TCPSocketWin>* socket, | 51 int Accept(std::unique_ptr<TCPSocketWin>* socket, |
50 IPEndPoint* address, | 52 IPEndPoint* address, |
51 const CompletionCallback& callback); | 53 const CompletionCallback& callback); |
52 | 54 |
53 int Connect(const IPEndPoint& address, const CompletionCallback& callback); | 55 int Connect(const IPEndPoint& address, const CompletionCallback& callback); |
54 bool IsConnected() const; | 56 bool IsConnected() const; |
55 bool IsConnectedAndIdle() const; | 57 bool IsConnectedAndIdle() const; |
56 | 58 |
57 // Multiple outstanding requests are not supported. | 59 // Multiple outstanding requests are not supported. |
58 // Full duplex mode (reading and writing at the same time) is supported. | 60 // Full duplex mode (reading and writing at the same time) is supported. |
59 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 61 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 void EndLoggingMultipleConnectAttempts(int net_error); | 110 void EndLoggingMultipleConnectAttempts(int net_error); |
109 | 111 |
110 const BoundNetLog& net_log() const { return net_log_; } | 112 const BoundNetLog& net_log() const { return net_log_; } |
111 | 113 |
112 private: | 114 private: |
113 class Core; | 115 class Core; |
114 | 116 |
115 // base::ObjectWatcher::Delegate implementation. | 117 // base::ObjectWatcher::Delegate implementation. |
116 void OnObjectSignaled(HANDLE object) override; | 118 void OnObjectSignaled(HANDLE object) override; |
117 | 119 |
118 int AcceptInternal(scoped_ptr<TCPSocketWin>* socket, | 120 int AcceptInternal(std::unique_ptr<TCPSocketWin>* socket, |
119 IPEndPoint* address); | 121 IPEndPoint* address); |
120 | 122 |
121 int DoConnect(); | 123 int DoConnect(); |
122 void DoConnectComplete(int result); | 124 void DoConnectComplete(int result); |
123 | 125 |
124 void LogConnectBegin(const AddressList& addresses); | 126 void LogConnectBegin(const AddressList& addresses); |
125 void LogConnectEnd(int net_error); | 127 void LogConnectEnd(int net_error); |
126 | 128 |
127 int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 129 int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
128 void DidCompleteConnect(); | 130 void DidCompleteConnect(); |
129 void DidCompleteWrite(); | 131 void DidCompleteWrite(); |
130 void DidSignalRead(); | 132 void DidSignalRead(); |
131 | 133 |
132 SOCKET socket_; | 134 SOCKET socket_; |
133 | 135 |
134 // |socket_performance_watcher_| may be nullptr. | 136 // |socket_performance_watcher_| may be nullptr. |
135 scoped_ptr<SocketPerformanceWatcher> socket_performance_watcher_; | 137 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher_; |
136 | 138 |
137 HANDLE accept_event_; | 139 HANDLE accept_event_; |
138 base::win::ObjectWatcher accept_watcher_; | 140 base::win::ObjectWatcher accept_watcher_; |
139 | 141 |
140 scoped_ptr<TCPSocketWin>* accept_socket_; | 142 std::unique_ptr<TCPSocketWin>* accept_socket_; |
141 IPEndPoint* accept_address_; | 143 IPEndPoint* accept_address_; |
142 CompletionCallback accept_callback_; | 144 CompletionCallback accept_callback_; |
143 | 145 |
144 // The various states that the socket could be in. | 146 // The various states that the socket could be in. |
145 bool waiting_connect_; | 147 bool waiting_connect_; |
146 bool waiting_read_; | 148 bool waiting_read_; |
147 bool waiting_write_; | 149 bool waiting_write_; |
148 | 150 |
149 // The core of the socket that can live longer than the socket itself. We pass | 151 // The core of the socket that can live longer than the socket itself. We pass |
150 // resources to the Windows async IO functions and we have to make sure that | 152 // resources to the Windows async IO functions and we have to make sure that |
151 // they are not destroyed while the OS still references them. | 153 // they are not destroyed while the OS still references them. |
152 scoped_refptr<Core> core_; | 154 scoped_refptr<Core> core_; |
153 | 155 |
154 // External callback; called when connect or read is complete. | 156 // External callback; called when connect or read is complete. |
155 CompletionCallback read_callback_; | 157 CompletionCallback read_callback_; |
156 | 158 |
157 // External callback; called when write is complete. | 159 // External callback; called when write is complete. |
158 CompletionCallback write_callback_; | 160 CompletionCallback write_callback_; |
159 | 161 |
160 scoped_ptr<IPEndPoint> peer_address_; | 162 std::unique_ptr<IPEndPoint> peer_address_; |
161 // The OS error that a connect attempt last completed with. | 163 // The OS error that a connect attempt last completed with. |
162 int connect_os_error_; | 164 int connect_os_error_; |
163 | 165 |
164 bool logging_multiple_connect_attempts_; | 166 bool logging_multiple_connect_attempts_; |
165 | 167 |
166 BoundNetLog net_log_; | 168 BoundNetLog net_log_; |
167 | 169 |
168 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin); | 170 DISALLOW_COPY_AND_ASSIGN(TCPSocketWin); |
169 }; | 171 }; |
170 | 172 |
171 } // namespace net | 173 } // namespace net |
172 | 174 |
173 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_ | 175 #endif // NET_SOCKET_TCP_SOCKET_WIN_H_ |
OLD | NEW |