OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "net/udp/udp_socket_win.h" | 5 #include "net/udp/udp_socket_win.h" |
6 | 6 |
7 #include <mstcpip.h> | 7 #include <mstcpip.h> |
8 | 8 |
9 #include "base/eintr_wrapper.h" | 9 #include "base/eintr_wrapper.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 const RandIntCallback& rand_int_cb, | 45 const RandIntCallback& rand_int_cb, |
46 net::NetLog* net_log, | 46 net::NetLog* net_log, |
47 const net::NetLog::Source& source) | 47 const net::NetLog::Source& source) |
48 : socket_(INVALID_SOCKET), | 48 : socket_(INVALID_SOCKET), |
49 bind_type_(bind_type), | 49 bind_type_(bind_type), |
50 rand_int_cb_(rand_int_cb), | 50 rand_int_cb_(rand_int_cb), |
51 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), | 51 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), |
52 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), | 52 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), |
53 recv_from_address_(NULL), | 53 recv_from_address_(NULL), |
54 old_read_callback_(NULL), | 54 old_read_callback_(NULL), |
55 old_write_callback_(NULL), | 55 write_callback_(NULL), |
56 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { | 56 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { |
57 EnsureWinsockInit(); | 57 EnsureWinsockInit(); |
58 scoped_refptr<NetLog::EventParameters> params; | 58 scoped_refptr<NetLog::EventParameters> params; |
59 if (source.is_valid()) | 59 if (source.is_valid()) |
60 params = new NetLogSourceParameter("source_dependency", source); | 60 params = new NetLogSourceParameter("source_dependency", source); |
61 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | 61 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); |
62 memset(&read_overlapped_, 0, sizeof(read_overlapped_)); | 62 memset(&read_overlapped_, 0, sizeof(read_overlapped_)); |
63 read_overlapped_.hEvent = WSACreateEvent(); | 63 read_overlapped_.hEvent = WSACreateEvent(); |
64 memset(&write_overlapped_, 0, sizeof(write_overlapped_)); | 64 memset(&write_overlapped_, 0, sizeof(write_overlapped_)); |
65 write_overlapped_.hEvent = WSACreateEvent(); | 65 write_overlapped_.hEvent = WSACreateEvent(); |
66 if (bind_type == DatagramSocket::RANDOM_BIND) | 66 if (bind_type == DatagramSocket::RANDOM_BIND) |
67 DCHECK(!rand_int_cb.is_null()); | 67 DCHECK(!rand_int_cb.is_null()); |
68 } | 68 } |
69 | 69 |
70 UDPSocketWin::~UDPSocketWin() { | 70 UDPSocketWin::~UDPSocketWin() { |
71 Close(); | 71 Close(); |
72 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); | 72 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); |
73 } | 73 } |
74 | 74 |
75 void UDPSocketWin::Close() { | 75 void UDPSocketWin::Close() { |
76 DCHECK(CalledOnValidThread()); | 76 DCHECK(CalledOnValidThread()); |
77 | 77 |
78 if (!is_connected()) | 78 if (!is_connected()) |
79 return; | 79 return; |
80 | 80 |
81 // Zero out any pending read/write callback state. | 81 // Zero out any pending read/write callback state. |
82 old_read_callback_ = NULL; | 82 old_read_callback_ = NULL; |
83 read_callback_.Reset(); | 83 read_callback_.Reset(); |
84 recv_from_address_ = NULL; | 84 recv_from_address_ = NULL; |
85 old_write_callback_ = NULL; | 85 write_callback_ = NULL; |
86 write_callback_.Reset(); | |
87 | 86 |
88 read_watcher_.StopWatching(); | 87 read_watcher_.StopWatching(); |
89 write_watcher_.StopWatching(); | 88 write_watcher_.StopWatching(); |
90 | 89 |
91 closesocket(socket_); | 90 closesocket(socket_); |
92 socket_ = INVALID_SOCKET; | 91 socket_ = INVALID_SOCKET; |
93 } | 92 } |
94 | 93 |
95 int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const { | 94 int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const { |
96 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 read_callback_ = callback; | 185 read_callback_ = callback; |
187 recv_from_address_ = address; | 186 recv_from_address_ = address; |
188 return ERR_IO_PENDING; | 187 return ERR_IO_PENDING; |
189 } | 188 } |
190 | 189 |
191 int UDPSocketWin::Write(IOBuffer* buf, | 190 int UDPSocketWin::Write(IOBuffer* buf, |
192 int buf_len, | 191 int buf_len, |
193 OldCompletionCallback* callback) { | 192 OldCompletionCallback* callback) { |
194 return SendToOrWrite(buf, buf_len, NULL, callback); | 193 return SendToOrWrite(buf, buf_len, NULL, callback); |
195 } | 194 } |
196 int UDPSocketWin::Write(IOBuffer* buf, | |
197 int buf_len, | |
198 const CompletionCallback& callback) { | |
199 return SendToOrWrite(buf, buf_len, NULL, callback); | |
200 } | |
201 | 195 |
202 int UDPSocketWin::SendTo(IOBuffer* buf, | 196 int UDPSocketWin::SendTo(IOBuffer* buf, |
203 int buf_len, | 197 int buf_len, |
204 const IPEndPoint& address, | 198 const IPEndPoint& address, |
205 OldCompletionCallback* callback) { | 199 OldCompletionCallback* callback) { |
206 return SendToOrWrite(buf, buf_len, &address, callback); | 200 return SendToOrWrite(buf, buf_len, &address, callback); |
207 } | 201 } |
208 | 202 |
209 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, | 203 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, |
210 int buf_len, | 204 int buf_len, |
211 const IPEndPoint* address, | 205 const IPEndPoint* address, |
212 OldCompletionCallback* callback) { | 206 OldCompletionCallback* callback) { |
213 DCHECK(CalledOnValidThread()); | 207 DCHECK(CalledOnValidThread()); |
214 DCHECK_NE(INVALID_SOCKET, socket_); | 208 DCHECK_NE(INVALID_SOCKET, socket_); |
215 DCHECK(!old_write_callback_ && write_callback_.is_null()); | 209 DCHECK(!write_callback_); |
216 DCHECK(callback); // Synchronous operation not supported. | 210 DCHECK(callback); // Synchronous operation not supported. |
217 DCHECK_GT(buf_len, 0); | 211 DCHECK_GT(buf_len, 0); |
218 DCHECK(!send_to_address_.get()); | 212 DCHECK(!send_to_address_.get()); |
219 | 213 |
220 int nwrite = InternalSendTo(buf, buf_len, address); | 214 int nwrite = InternalSendTo(buf, buf_len, address); |
221 if (nwrite != ERR_IO_PENDING) | 215 if (nwrite != ERR_IO_PENDING) |
222 return nwrite; | 216 return nwrite; |
223 | 217 |
224 if (address) | 218 if (address) |
225 send_to_address_.reset(new IPEndPoint(*address)); | 219 send_to_address_.reset(new IPEndPoint(*address)); |
226 write_iobuffer_ = buf; | 220 write_iobuffer_ = buf; |
227 old_write_callback_ = callback; | |
228 return ERR_IO_PENDING; | |
229 } | |
230 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, | |
231 int buf_len, | |
232 const IPEndPoint* address, | |
233 const CompletionCallback& callback) { | |
234 DCHECK(CalledOnValidThread()); | |
235 DCHECK_NE(INVALID_SOCKET, socket_); | |
236 DCHECK(!old_write_callback_ && write_callback_.is_null()); | |
237 DCHECK(!callback.is_null()); // Synchronous operation not supported. | |
238 DCHECK_GT(buf_len, 0); | |
239 DCHECK(!send_to_address_.get()); | |
240 | |
241 int nwrite = InternalSendTo(buf, buf_len, address); | |
242 if (nwrite != ERR_IO_PENDING) | |
243 return nwrite; | |
244 | |
245 if (address) | |
246 send_to_address_.reset(new IPEndPoint(*address)); | |
247 write_iobuffer_ = buf; | |
248 write_callback_ = callback; | 221 write_callback_ = callback; |
249 return ERR_IO_PENDING; | 222 return ERR_IO_PENDING; |
250 } | 223 } |
251 | 224 |
252 int UDPSocketWin::Connect(const IPEndPoint& address) { | 225 int UDPSocketWin::Connect(const IPEndPoint& address) { |
253 net_log_.BeginEvent( | 226 net_log_.BeginEvent( |
254 NetLog::TYPE_UDP_CONNECT, | 227 NetLog::TYPE_UDP_CONNECT, |
255 make_scoped_refptr(new NetLogStringParameter("address", | 228 make_scoped_refptr(new NetLogStringParameter("address", |
256 address.ToString()))); | 229 address.ToString()))); |
257 int rv = InternalConnect(address); | 230 int rv = InternalConnect(address); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 c->Run(rv); | 307 c->Run(rv); |
335 } else { | 308 } else { |
336 CompletionCallback c = read_callback_; | 309 CompletionCallback c = read_callback_; |
337 read_callback_.Reset(); | 310 read_callback_.Reset(); |
338 c.Run(rv); | 311 c.Run(rv); |
339 } | 312 } |
340 } | 313 } |
341 | 314 |
342 void UDPSocketWin::DoWriteCallback(int rv) { | 315 void UDPSocketWin::DoWriteCallback(int rv) { |
343 DCHECK_NE(rv, ERR_IO_PENDING); | 316 DCHECK_NE(rv, ERR_IO_PENDING); |
344 DCHECK(old_write_callback_ && !write_callback_.is_null()); | 317 DCHECK(write_callback_); |
345 | 318 |
346 // since Run may result in Write being called, clear write_callback_ up front. | 319 // since Run may result in Write being called, clear write_callback_ up front. |
347 if (old_write_callback_) { | 320 OldCompletionCallback* c = write_callback_; |
348 OldCompletionCallback* c = old_write_callback_; | 321 write_callback_ = NULL; |
349 old_write_callback_ = NULL; | 322 c->Run(rv); |
350 c->Run(rv); | |
351 } else { | |
352 CompletionCallback c = write_callback_; | |
353 write_callback_.Reset(); | |
354 c.Run(rv); | |
355 } | |
356 } | 323 } |
357 | 324 |
358 void UDPSocketWin::DidCompleteRead() { | 325 void UDPSocketWin::DidCompleteRead() { |
359 DWORD num_bytes, flags; | 326 DWORD num_bytes, flags; |
360 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_, | 327 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_, |
361 &num_bytes, FALSE, &flags); | 328 &num_bytes, FALSE, &flags); |
362 WSAResetEvent(read_overlapped_.hEvent); | 329 WSAResetEvent(read_overlapped_.hEvent); |
363 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); | 330 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); |
364 // Convert address. | 331 // Convert address. |
365 if (recv_from_address_ && result >= 0) { | 332 if (recv_from_address_ && result >= 0) { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 return DoBind(IPEndPoint(ip, 0)); | 503 return DoBind(IPEndPoint(ip, 0)); |
537 } | 504 } |
538 | 505 |
539 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { | 506 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { |
540 const struct sockaddr* addr = | 507 const struct sockaddr* addr = |
541 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_); | 508 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_); |
542 return address->FromSockAddr(addr, recv_addr_len_); | 509 return address->FromSockAddr(addr, recv_addr_len_); |
543 } | 510 } |
544 | 511 |
545 } // namespace net | 512 } // namespace net |
OLD | NEW |