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 write_callback_(NULL), | 55 old_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 write_callback_ = NULL; | 85 old_write_callback_ = NULL; |
| 86 write_callback_.Reset(); |
86 | 87 |
87 read_watcher_.StopWatching(); | 88 read_watcher_.StopWatching(); |
88 write_watcher_.StopWatching(); | 89 write_watcher_.StopWatching(); |
89 | 90 |
90 closesocket(socket_); | 91 closesocket(socket_); |
91 socket_ = INVALID_SOCKET; | 92 socket_ = INVALID_SOCKET; |
92 } | 93 } |
93 | 94 |
94 int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const { | 95 int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const { |
95 DCHECK(CalledOnValidThread()); | 96 DCHECK(CalledOnValidThread()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 read_callback_ = callback; | 186 read_callback_ = callback; |
186 recv_from_address_ = address; | 187 recv_from_address_ = address; |
187 return ERR_IO_PENDING; | 188 return ERR_IO_PENDING; |
188 } | 189 } |
189 | 190 |
190 int UDPSocketWin::Write(IOBuffer* buf, | 191 int UDPSocketWin::Write(IOBuffer* buf, |
191 int buf_len, | 192 int buf_len, |
192 OldCompletionCallback* callback) { | 193 OldCompletionCallback* callback) { |
193 return SendToOrWrite(buf, buf_len, NULL, callback); | 194 return SendToOrWrite(buf, buf_len, NULL, callback); |
194 } | 195 } |
| 196 int UDPSocketWin::Write(IOBuffer* buf, |
| 197 int buf_len, |
| 198 const CompletionCallback& callback) { |
| 199 return SendToOrWrite(buf, buf_len, NULL, callback); |
| 200 } |
195 | 201 |
196 int UDPSocketWin::SendTo(IOBuffer* buf, | 202 int UDPSocketWin::SendTo(IOBuffer* buf, |
197 int buf_len, | 203 int buf_len, |
198 const IPEndPoint& address, | 204 const IPEndPoint& address, |
199 OldCompletionCallback* callback) { | 205 OldCompletionCallback* callback) { |
200 return SendToOrWrite(buf, buf_len, &address, callback); | 206 return SendToOrWrite(buf, buf_len, &address, callback); |
201 } | 207 } |
202 | 208 |
203 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, | 209 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, |
204 int buf_len, | 210 int buf_len, |
205 const IPEndPoint* address, | 211 const IPEndPoint* address, |
206 OldCompletionCallback* callback) { | 212 OldCompletionCallback* callback) { |
207 DCHECK(CalledOnValidThread()); | 213 DCHECK(CalledOnValidThread()); |
208 DCHECK_NE(INVALID_SOCKET, socket_); | 214 DCHECK_NE(INVALID_SOCKET, socket_); |
209 DCHECK(!write_callback_); | 215 DCHECK(!old_write_callback_ && write_callback_.is_null()); |
210 DCHECK(callback); // Synchronous operation not supported. | 216 DCHECK(callback); // Synchronous operation not supported. |
211 DCHECK_GT(buf_len, 0); | 217 DCHECK_GT(buf_len, 0); |
212 DCHECK(!send_to_address_.get()); | 218 DCHECK(!send_to_address_.get()); |
213 | 219 |
214 int nwrite = InternalSendTo(buf, buf_len, address); | 220 int nwrite = InternalSendTo(buf, buf_len, address); |
215 if (nwrite != ERR_IO_PENDING) | 221 if (nwrite != ERR_IO_PENDING) |
216 return nwrite; | 222 return nwrite; |
217 | 223 |
218 if (address) | 224 if (address) |
219 send_to_address_.reset(new IPEndPoint(*address)); | 225 send_to_address_.reset(new IPEndPoint(*address)); |
220 write_iobuffer_ = buf; | 226 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; |
221 write_callback_ = callback; | 248 write_callback_ = callback; |
222 return ERR_IO_PENDING; | 249 return ERR_IO_PENDING; |
223 } | 250 } |
224 | 251 |
225 int UDPSocketWin::Connect(const IPEndPoint& address) { | 252 int UDPSocketWin::Connect(const IPEndPoint& address) { |
226 net_log_.BeginEvent( | 253 net_log_.BeginEvent( |
227 NetLog::TYPE_UDP_CONNECT, | 254 NetLog::TYPE_UDP_CONNECT, |
228 make_scoped_refptr(new NetLogStringParameter("address", | 255 make_scoped_refptr(new NetLogStringParameter("address", |
229 address.ToString()))); | 256 address.ToString()))); |
230 int rv = InternalConnect(address); | 257 int rv = InternalConnect(address); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 c->Run(rv); | 334 c->Run(rv); |
308 } else { | 335 } else { |
309 CompletionCallback c = read_callback_; | 336 CompletionCallback c = read_callback_; |
310 read_callback_.Reset(); | 337 read_callback_.Reset(); |
311 c.Run(rv); | 338 c.Run(rv); |
312 } | 339 } |
313 } | 340 } |
314 | 341 |
315 void UDPSocketWin::DoWriteCallback(int rv) { | 342 void UDPSocketWin::DoWriteCallback(int rv) { |
316 DCHECK_NE(rv, ERR_IO_PENDING); | 343 DCHECK_NE(rv, ERR_IO_PENDING); |
317 DCHECK(write_callback_); | 344 DCHECK(old_write_callback_ && !write_callback_.is_null()); |
318 | 345 |
319 // since Run may result in Write being called, clear write_callback_ up front. | 346 // since Run may result in Write being called, clear write_callback_ up front. |
320 OldCompletionCallback* c = write_callback_; | 347 if (old_write_callback_) { |
321 write_callback_ = NULL; | 348 OldCompletionCallback* c = old_write_callback_; |
322 c->Run(rv); | 349 old_write_callback_ = NULL; |
| 350 c->Run(rv); |
| 351 } else { |
| 352 CompletionCallback c = write_callback_; |
| 353 write_callback_.Reset(); |
| 354 c.Run(rv); |
| 355 } |
323 } | 356 } |
324 | 357 |
325 void UDPSocketWin::DidCompleteRead() { | 358 void UDPSocketWin::DidCompleteRead() { |
326 DWORD num_bytes, flags; | 359 DWORD num_bytes, flags; |
327 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_, | 360 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_, |
328 &num_bytes, FALSE, &flags); | 361 &num_bytes, FALSE, &flags); |
329 WSAResetEvent(read_overlapped_.hEvent); | 362 WSAResetEvent(read_overlapped_.hEvent); |
330 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); | 363 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); |
331 // Convert address. | 364 // Convert address. |
332 if (recv_from_address_ && result >= 0) { | 365 if (recv_from_address_ && result >= 0) { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 return DoBind(IPEndPoint(ip, 0)); | 536 return DoBind(IPEndPoint(ip, 0)); |
504 } | 537 } |
505 | 538 |
506 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { | 539 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { |
507 const struct sockaddr* addr = | 540 const struct sockaddr* addr = |
508 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_); | 541 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_); |
509 return address->FromSockAddr(addr, recv_addr_len_); | 542 return address->FromSockAddr(addr, recv_addr_len_); |
510 } | 543 } |
511 | 544 |
512 } // namespace net | 545 } // namespace net |
OLD | NEW |