Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 net::NetLog* net_log, | 254 net::NetLog* net_log, |
| 255 const net::NetLog::Source& source) | 255 const net::NetLog::Source& source) |
| 256 : socket_(INVALID_SOCKET), | 256 : socket_(INVALID_SOCKET), |
| 257 addr_family_(0), | 257 addr_family_(0), |
| 258 is_connected_(false), | 258 is_connected_(false), |
| 259 socket_options_(SOCKET_OPTION_MULTICAST_LOOP), | 259 socket_options_(SOCKET_OPTION_MULTICAST_LOOP), |
| 260 multicast_interface_(0), | 260 multicast_interface_(0), |
| 261 multicast_time_to_live_(1), | 261 multicast_time_to_live_(1), |
| 262 bind_type_(bind_type), | 262 bind_type_(bind_type), |
| 263 rand_int_cb_(rand_int_cb), | 263 rand_int_cb_(rand_int_cb), |
| 264 use_non_blocking_io_(false), | |
| 265 watching_read_write_event_(false), | |
| 266 read_iobuffer_len_(0), | |
| 267 write_iobuffer_len_(0), | |
| 264 recv_from_address_(NULL), | 268 recv_from_address_(NULL), |
| 265 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)), | 269 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)), |
| 266 qos_handle_(NULL), | 270 qos_handle_(NULL), |
| 267 qos_flow_id_(0) { | 271 qos_flow_id_(0) { |
| 268 EnsureWinsockInit(); | 272 EnsureWinsockInit(); |
| 269 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 273 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 270 source.ToEventParametersCallback()); | 274 source.ToEventParametersCallback()); |
| 271 if (bind_type == DatagramSocket::RANDOM_BIND) | 275 if (bind_type == DatagramSocket::RANDOM_BIND) |
| 272 DCHECK(!rand_int_cb.is_null()); | 276 DCHECK(!rand_int_cb.is_null()); |
| 273 } | 277 } |
| 274 | 278 |
| 275 UDPSocketWin::~UDPSocketWin() { | 279 UDPSocketWin::~UDPSocketWin() { |
| 276 Close(); | 280 Close(); |
| 277 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 281 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 278 } | 282 } |
| 279 | 283 |
| 280 int UDPSocketWin::Open(AddressFamily address_family) { | 284 int UDPSocketWin::Open(AddressFamily address_family) { |
| 281 DCHECK(CalledOnValidThread()); | 285 DCHECK(CalledOnValidThread()); |
| 282 DCHECK_EQ(socket_, INVALID_SOCKET); | 286 DCHECK_EQ(socket_, INVALID_SOCKET); |
| 283 | 287 |
| 284 addr_family_ = ConvertAddressFamily(address_family); | 288 addr_family_ = ConvertAddressFamily(address_family); |
| 285 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, IPPROTO_UDP); | 289 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, IPPROTO_UDP); |
| 286 if (socket_ == INVALID_SOCKET) | 290 if (socket_ == INVALID_SOCKET) |
| 287 return MapSystemError(WSAGetLastError()); | 291 return MapSystemError(WSAGetLastError()); |
| 288 core_ = new Core(this); | 292 if (!use_non_blocking_io_) { |
| 293 core_ = new Core(this); | |
| 294 } else { | |
| 295 read_write_event_.Set(WSACreateEvent()); | |
| 296 WSAEventSelect(socket_, read_write_event_.Get(), FD_READ | FD_WRITE); | |
| 297 } | |
| 289 return OK; | 298 return OK; |
| 290 } | 299 } |
| 291 | 300 |
| 292 void UDPSocketWin::Close() { | 301 void UDPSocketWin::Close() { |
| 293 DCHECK(CalledOnValidThread()); | 302 DCHECK(CalledOnValidThread()); |
| 294 | 303 |
| 295 if (socket_ == INVALID_SOCKET) | 304 if (socket_ == INVALID_SOCKET) |
| 296 return; | 305 return; |
| 297 | 306 |
| 298 if (qos_handle_) { | 307 if (qos_handle_) { |
| 299 QwaveAPI::Get().CloseHandle(qos_handle_); | 308 QwaveAPI::Get().CloseHandle(qos_handle_); |
| 300 } | 309 } |
| 301 | 310 |
| 302 // Zero out any pending read/write callback state. | 311 // Zero out any pending read/write callback state. |
| 303 read_callback_.Reset(); | 312 read_callback_.Reset(); |
| 304 recv_from_address_ = NULL; | 313 recv_from_address_ = NULL; |
| 305 write_callback_.Reset(); | 314 write_callback_.Reset(); |
| 306 | 315 |
| 307 base::TimeTicks start_time = base::TimeTicks::Now(); | 316 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 308 closesocket(socket_); | 317 closesocket(socket_); |
| 309 UMA_HISTOGRAM_TIMES("Net.UDPSocketWinClose", | 318 UMA_HISTOGRAM_TIMES("Net.UDPSocketWinClose", |
| 310 base::TimeTicks::Now() - start_time); | 319 base::TimeTicks::Now() - start_time); |
| 311 socket_ = INVALID_SOCKET; | 320 socket_ = INVALID_SOCKET; |
| 312 addr_family_ = 0; | 321 addr_family_ = 0; |
| 313 is_connected_ = false; | 322 is_connected_ = false; |
| 314 | 323 |
| 315 core_->Detach(); | 324 read_write_watcher_.StopWatching(); |
| 316 core_ = NULL; | 325 read_write_event_.Close(); |
| 326 | |
| 327 if (core_) { | |
| 328 core_->Detach(); | |
| 329 core_ = NULL; | |
| 330 } | |
| 317 } | 331 } |
| 318 | 332 |
| 319 int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const { | 333 int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const { |
| 320 DCHECK(CalledOnValidThread()); | 334 DCHECK(CalledOnValidThread()); |
| 321 DCHECK(address); | 335 DCHECK(address); |
| 322 if (!is_connected()) | 336 if (!is_connected()) |
| 323 return ERR_SOCKET_NOT_CONNECTED; | 337 return ERR_SOCKET_NOT_CONNECTED; |
| 324 | 338 |
| 325 // TODO(szym): Simplify. http://crbug.com/126152 | 339 // TODO(szym): Simplify. http://crbug.com/126152 |
| 326 if (!remote_address_.get()) { | 340 if (!remote_address_.get()) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 int buf_len, | 384 int buf_len, |
| 371 IPEndPoint* address, | 385 IPEndPoint* address, |
| 372 const CompletionCallback& callback) { | 386 const CompletionCallback& callback) { |
| 373 DCHECK(CalledOnValidThread()); | 387 DCHECK(CalledOnValidThread()); |
| 374 DCHECK_NE(INVALID_SOCKET, socket_); | 388 DCHECK_NE(INVALID_SOCKET, socket_); |
| 375 CHECK(read_callback_.is_null()); | 389 CHECK(read_callback_.is_null()); |
| 376 DCHECK(!recv_from_address_); | 390 DCHECK(!recv_from_address_); |
| 377 DCHECK(!callback.is_null()); // Synchronous operation not supported. | 391 DCHECK(!callback.is_null()); // Synchronous operation not supported. |
| 378 DCHECK_GT(buf_len, 0); | 392 DCHECK_GT(buf_len, 0); |
| 379 | 393 |
| 380 int nread = InternalRecvFrom(buf, buf_len, address); | 394 int nread = core_ ? InternalRecvFromOverlapped(buf, buf_len, address) |
| 395 : InternalRecvFromNonBlocking(buf, buf_len, address); | |
| 381 if (nread != ERR_IO_PENDING) | 396 if (nread != ERR_IO_PENDING) |
| 382 return nread; | 397 return nread; |
| 383 | 398 |
| 384 read_callback_ = callback; | 399 read_callback_ = callback; |
| 385 recv_from_address_ = address; | 400 recv_from_address_ = address; |
| 386 return ERR_IO_PENDING; | 401 return ERR_IO_PENDING; |
| 387 } | 402 } |
| 388 | 403 |
| 389 int UDPSocketWin::Write(IOBuffer* buf, | 404 int UDPSocketWin::Write(IOBuffer* buf, |
| 390 int buf_len, | 405 int buf_len, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 403 int buf_len, | 418 int buf_len, |
| 404 const IPEndPoint* address, | 419 const IPEndPoint* address, |
| 405 const CompletionCallback& callback) { | 420 const CompletionCallback& callback) { |
| 406 DCHECK(CalledOnValidThread()); | 421 DCHECK(CalledOnValidThread()); |
| 407 DCHECK_NE(INVALID_SOCKET, socket_); | 422 DCHECK_NE(INVALID_SOCKET, socket_); |
| 408 CHECK(write_callback_.is_null()); | 423 CHECK(write_callback_.is_null()); |
| 409 DCHECK(!callback.is_null()); // Synchronous operation not supported. | 424 DCHECK(!callback.is_null()); // Synchronous operation not supported. |
| 410 DCHECK_GT(buf_len, 0); | 425 DCHECK_GT(buf_len, 0); |
| 411 DCHECK(!send_to_address_.get()); | 426 DCHECK(!send_to_address_.get()); |
| 412 | 427 |
| 413 int nwrite = InternalSendTo(buf, buf_len, address); | 428 int nwrite = core_ ? InternalSendToOverlapped(buf, buf_len, address) |
| 429 : InternalSendToNonBlocking(buf, buf_len, address); | |
| 414 if (nwrite != ERR_IO_PENDING) | 430 if (nwrite != ERR_IO_PENDING) |
| 415 return nwrite; | 431 return nwrite; |
| 416 | 432 |
| 417 if (address) | 433 if (address) |
| 418 send_to_address_.reset(new IPEndPoint(*address)); | 434 send_to_address_.reset(new IPEndPoint(*address)); |
| 419 write_callback_ = callback; | 435 write_callback_ = callback; |
| 420 return ERR_IO_PENDING; | 436 return ERR_IO_PENDING; |
| 421 } | 437 } |
| 422 | 438 |
| 423 int UDPSocketWin::Connect(const IPEndPoint& address) { | 439 int UDPSocketWin::Connect(const IPEndPoint& address) { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 c.Run(rv); | 582 c.Run(rv); |
| 567 } | 583 } |
| 568 | 584 |
| 569 void UDPSocketWin::DidCompleteRead() { | 585 void UDPSocketWin::DidCompleteRead() { |
| 570 DWORD num_bytes, flags; | 586 DWORD num_bytes, flags; |
| 571 BOOL ok = WSAGetOverlappedResult(socket_, &core_->read_overlapped_, | 587 BOOL ok = WSAGetOverlappedResult(socket_, &core_->read_overlapped_, |
| 572 &num_bytes, FALSE, &flags); | 588 &num_bytes, FALSE, &flags); |
| 573 WSAResetEvent(core_->read_overlapped_.hEvent); | 589 WSAResetEvent(core_->read_overlapped_.hEvent); |
| 574 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); | 590 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); |
| 575 // Convert address. | 591 // Convert address. |
| 576 if (recv_from_address_ && result >= 0) { | 592 IPEndPoint address; |
| 577 if (!ReceiveAddressToIPEndpoint(recv_from_address_)) | 593 IPEndPoint* address_to_log = NULL; |
| 594 if (result >= 0) { | |
| 595 if (address.FromSockAddr(core_->recv_addr_storage_.addr, | |
| 596 core_->recv_addr_storage_.addr_len)) { | |
| 597 if (recv_from_address_) | |
| 598 *recv_from_address_ = address; | |
| 599 address_to_log = &address; | |
| 600 } else { | |
| 578 result = ERR_ADDRESS_INVALID; | 601 result = ERR_ADDRESS_INVALID; |
| 602 } | |
| 579 } | 603 } |
| 580 LogRead(result, core_->read_iobuffer_->data()); | 604 LogRead(result, core_->read_iobuffer_->data(), address_to_log); |
| 581 core_->read_iobuffer_ = NULL; | 605 core_->read_iobuffer_ = NULL; |
| 582 recv_from_address_ = NULL; | 606 recv_from_address_ = NULL; |
| 583 DoReadCallback(result); | 607 DoReadCallback(result); |
| 584 } | 608 } |
| 585 | 609 |
| 586 void UDPSocketWin::LogRead(int result, const char* bytes) const { | 610 void UDPSocketWin::DidCompleteWrite() { |
| 611 DWORD num_bytes, flags; | |
| 612 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, | |
| 613 &num_bytes, FALSE, &flags); | |
| 614 WSAResetEvent(core_->write_overlapped_.hEvent); | |
| 615 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); | |
| 616 LogWrite(result, core_->write_iobuffer_->data(), send_to_address_.get()); | |
| 617 | |
| 618 send_to_address_.reset(); | |
| 619 core_->write_iobuffer_ = NULL; | |
| 620 DoWriteCallback(result); | |
| 621 } | |
| 622 | |
| 623 void UDPSocketWin::OnObjectSignaled(HANDLE object) { | |
| 624 DCHECK(object == read_write_event_.Get()); | |
| 625 watching_read_write_event_ = false; | |
| 626 WSANETWORKEVENTS network_events; | |
| 627 int os_error = 0; | |
| 628 int rv = | |
| 629 WSAEnumNetworkEvents(socket_, read_write_event_.Get(), &network_events); | |
| 630 if (rv == SOCKET_ERROR) { | |
| 631 os_error = WSAGetLastError(); | |
| 632 rv = MapSystemError(os_error); | |
| 633 if (read_iobuffer_) { | |
| 634 read_iobuffer_ = NULL; | |
| 635 read_iobuffer_len_ = 0; | |
| 636 recv_from_address_ = NULL; | |
| 637 DoReadCallback(rv); | |
| 638 } | |
| 639 if (write_iobuffer_) { | |
| 640 write_iobuffer_ = NULL; | |
| 641 write_iobuffer_len_ = 0; | |
| 642 send_to_address_.reset(); | |
| 643 DoWriteCallback(rv); | |
| 644 } | |
| 645 return; | |
| 646 } | |
| 647 if ((network_events.lNetworkEvents & FD_READ) && read_iobuffer_) { | |
| 648 OnReadSignaled(); | |
| 649 } | |
| 650 if ((network_events.lNetworkEvents & FD_WRITE) && write_iobuffer_) { | |
| 651 OnWriteSignaled(); | |
| 652 } | |
| 653 | |
| 654 // There's still pending read / write. Watch for further events. | |
| 655 if (read_iobuffer_ || write_iobuffer_) { | |
| 656 WatchForReadWrite(); | |
| 657 } | |
| 658 } | |
| 659 | |
| 660 void UDPSocketWin::OnReadSignaled() { | |
| 661 int rv = InternalRecvFromNonBlocking(read_iobuffer_.get(), read_iobuffer_len_, | |
| 662 recv_from_address_); | |
| 663 if (rv == ERR_IO_PENDING) | |
| 664 return; | |
| 665 read_iobuffer_ = NULL; | |
| 666 read_iobuffer_len_ = 0; | |
| 667 recv_from_address_ = NULL; | |
| 668 DoReadCallback(rv); | |
| 669 } | |
| 670 | |
| 671 void UDPSocketWin::OnWriteSignaled() { | |
| 672 int rv = InternalSendToNonBlocking(write_iobuffer_.get(), write_iobuffer_len_, | |
| 673 send_to_address_.get()); | |
| 674 if (rv == ERR_IO_PENDING) | |
| 675 return; | |
| 676 write_iobuffer_ = NULL; | |
| 677 write_iobuffer_len_ = 0; | |
| 678 send_to_address_.reset(); | |
| 679 DoWriteCallback(rv); | |
| 680 } | |
| 681 | |
| 682 void UDPSocketWin::WatchForReadWrite() { | |
| 683 if (watching_read_write_event_) { | |
|
rvargas (doing something else)
2015/02/05 00:44:28
BTW, you _could_ change this after https://coderev
| |
| 684 DCHECK(read_write_watcher_.GetWatchedObject()); | |
| 685 return; | |
| 686 } | |
| 687 watching_read_write_event_ = true; | |
| 688 bool watched = read_write_watcher_.StartWatching(read_write_event_.Get(), | |
| 689 this); | |
| 690 DCHECK(watched); | |
| 691 } | |
| 692 | |
| 693 void UDPSocketWin::LogRead(int result, | |
| 694 const char* bytes, | |
| 695 const IPEndPoint* address) const { | |
| 587 if (result < 0) { | 696 if (result < 0) { |
| 588 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); | 697 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); |
| 589 return; | 698 return; |
| 590 } | 699 } |
| 591 | 700 |
| 592 if (net_log_.IsLogging()) { | 701 if (net_log_.IsLogging()) { |
| 593 // Get address for logging, if |address| is NULL. | |
| 594 IPEndPoint address; | |
| 595 bool is_address_valid = ReceiveAddressToIPEndpoint(&address); | |
| 596 net_log_.AddEvent( | 702 net_log_.AddEvent( |
| 597 NetLog::TYPE_UDP_BYTES_RECEIVED, | 703 NetLog::TYPE_UDP_BYTES_RECEIVED, |
| 598 CreateNetLogUDPDataTranferCallback( | 704 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
| 599 result, bytes, | |
| 600 is_address_valid ? &address : NULL)); | |
| 601 } | 705 } |
| 602 | 706 |
| 603 base::StatsCounter read_bytes("udp.read_bytes"); | 707 base::StatsCounter read_bytes("udp.read_bytes"); |
| 604 read_bytes.Add(result); | 708 read_bytes.Add(result); |
| 605 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(result); | 709 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(result); |
| 606 } | 710 } |
| 607 | 711 |
| 608 void UDPSocketWin::DidCompleteWrite() { | |
| 609 DWORD num_bytes, flags; | |
| 610 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, | |
| 611 &num_bytes, FALSE, &flags); | |
| 612 WSAResetEvent(core_->write_overlapped_.hEvent); | |
| 613 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); | |
| 614 LogWrite(result, core_->write_iobuffer_->data(), send_to_address_.get()); | |
| 615 | |
| 616 send_to_address_.reset(); | |
| 617 core_->write_iobuffer_ = NULL; | |
| 618 DoWriteCallback(result); | |
| 619 } | |
| 620 | |
| 621 void UDPSocketWin::LogWrite(int result, | 712 void UDPSocketWin::LogWrite(int result, |
| 622 const char* bytes, | 713 const char* bytes, |
| 623 const IPEndPoint* address) const { | 714 const IPEndPoint* address) const { |
| 624 if (result < 0) { | 715 if (result < 0) { |
| 625 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); | 716 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); |
| 626 return; | 717 return; |
| 627 } | 718 } |
| 628 | 719 |
| 629 if (net_log_.IsLogging()) { | 720 if (net_log_.IsLogging()) { |
| 630 net_log_.AddEvent( | 721 net_log_.AddEvent( |
| 631 NetLog::TYPE_UDP_BYTES_SENT, | 722 NetLog::TYPE_UDP_BYTES_SENT, |
| 632 CreateNetLogUDPDataTranferCallback(result, bytes, address)); | 723 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
| 633 } | 724 } |
| 634 | 725 |
| 635 base::StatsCounter write_bytes("udp.write_bytes"); | 726 base::StatsCounter write_bytes("udp.write_bytes"); |
| 636 write_bytes.Add(result); | 727 write_bytes.Add(result); |
| 637 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result); | 728 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result); |
| 638 } | 729 } |
| 639 | 730 |
| 640 int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len, | 731 int UDPSocketWin::InternalRecvFromOverlapped(IOBuffer* buf, |
| 641 IPEndPoint* address) { | 732 int buf_len, |
| 733 IPEndPoint* address) { | |
| 642 DCHECK(!core_->read_iobuffer_.get()); | 734 DCHECK(!core_->read_iobuffer_.get()); |
| 643 SockaddrStorage& storage = core_->recv_addr_storage_; | 735 SockaddrStorage& storage = core_->recv_addr_storage_; |
| 644 storage.addr_len = sizeof(storage.addr_storage); | 736 storage.addr_len = sizeof(storage.addr_storage); |
| 645 | 737 |
| 646 WSABUF read_buffer; | 738 WSABUF read_buffer; |
| 647 read_buffer.buf = buf->data(); | 739 read_buffer.buf = buf->data(); |
| 648 read_buffer.len = buf_len; | 740 read_buffer.len = buf_len; |
| 649 | 741 |
| 650 DWORD flags = 0; | 742 DWORD flags = 0; |
| 651 DWORD num; | 743 DWORD num; |
| 652 CHECK_NE(INVALID_SOCKET, socket_); | 744 CHECK_NE(INVALID_SOCKET, socket_); |
| 653 AssertEventNotSignaled(core_->read_overlapped_.hEvent); | 745 AssertEventNotSignaled(core_->read_overlapped_.hEvent); |
| 654 int rv = WSARecvFrom(socket_, &read_buffer, 1, &num, &flags, storage.addr, | 746 int rv = WSARecvFrom(socket_, &read_buffer, 1, &num, &flags, storage.addr, |
| 655 &storage.addr_len, &core_->read_overlapped_, NULL); | 747 &storage.addr_len, &core_->read_overlapped_, NULL); |
| 656 if (rv == 0) { | 748 if (rv == 0) { |
| 657 if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) { | 749 if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) { |
| 658 int result = num; | 750 int result = num; |
| 659 // Convert address. | 751 // Convert address. |
| 660 if (address && result >= 0) { | 752 IPEndPoint address_storage; |
| 661 if (!ReceiveAddressToIPEndpoint(address)) | 753 IPEndPoint* address_to_log = NULL; |
| 754 if (result >= 0) { | |
| 755 if (address_storage.FromSockAddr(core_->recv_addr_storage_.addr, | |
| 756 core_->recv_addr_storage_.addr_len)) { | |
| 757 if (address) | |
| 758 *address = address_storage; | |
| 759 address_to_log = &address_storage; | |
| 760 } else { | |
| 662 result = ERR_ADDRESS_INVALID; | 761 result = ERR_ADDRESS_INVALID; |
| 762 } | |
| 663 } | 763 } |
| 664 LogRead(result, buf->data()); | 764 LogRead(result, buf->data(), address_to_log); |
| 665 return result; | 765 return result; |
| 666 } | 766 } |
| 667 } else { | 767 } else { |
| 668 int os_error = WSAGetLastError(); | 768 int os_error = WSAGetLastError(); |
| 669 if (os_error != WSA_IO_PENDING) { | 769 if (os_error != WSA_IO_PENDING) { |
| 670 int result = MapSystemError(os_error); | 770 int result = MapSystemError(os_error); |
| 671 LogRead(result, NULL); | 771 LogRead(result, NULL, NULL); |
| 672 return result; | 772 return result; |
| 673 } | 773 } |
| 674 } | 774 } |
| 675 core_->WatchForRead(); | 775 core_->WatchForRead(); |
| 676 core_->read_iobuffer_ = buf; | 776 core_->read_iobuffer_ = buf; |
| 677 return ERR_IO_PENDING; | 777 return ERR_IO_PENDING; |
| 678 } | 778 } |
| 679 | 779 |
| 680 int UDPSocketWin::InternalSendTo(IOBuffer* buf, int buf_len, | 780 int UDPSocketWin::InternalSendToOverlapped(IOBuffer* buf, |
| 681 const IPEndPoint* address) { | 781 int buf_len, |
| 782 const IPEndPoint* address) { | |
| 682 DCHECK(!core_->write_iobuffer_.get()); | 783 DCHECK(!core_->write_iobuffer_.get()); |
| 683 SockaddrStorage storage; | 784 SockaddrStorage storage; |
| 684 struct sockaddr* addr = storage.addr; | 785 struct sockaddr* addr = storage.addr; |
| 685 // Convert address. | 786 // Convert address. |
| 686 if (!address) { | 787 if (!address) { |
| 687 addr = NULL; | 788 addr = NULL; |
| 688 storage.addr_len = 0; | 789 storage.addr_len = 0; |
| 689 } else { | 790 } else { |
| 690 if (!address->ToSockAddr(addr, &storage.addr_len)) { | 791 if (!address->ToSockAddr(addr, &storage.addr_len)) { |
| 691 int result = ERR_ADDRESS_INVALID; | 792 int result = ERR_ADDRESS_INVALID; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 716 LogWrite(result, NULL, NULL); | 817 LogWrite(result, NULL, NULL); |
| 717 return result; | 818 return result; |
| 718 } | 819 } |
| 719 } | 820 } |
| 720 | 821 |
| 721 core_->WatchForWrite(); | 822 core_->WatchForWrite(); |
| 722 core_->write_iobuffer_ = buf; | 823 core_->write_iobuffer_ = buf; |
| 723 return ERR_IO_PENDING; | 824 return ERR_IO_PENDING; |
| 724 } | 825 } |
| 725 | 826 |
| 827 int UDPSocketWin::InternalRecvFromNonBlocking(IOBuffer* buf, | |
| 828 int buf_len, | |
| 829 IPEndPoint* address) { | |
| 830 DCHECK(!read_iobuffer_ || read_iobuffer_.get() == buf); | |
| 831 SockaddrStorage storage; | |
| 832 storage.addr_len = sizeof(storage.addr_storage); | |
| 833 | |
| 834 CHECK_NE(INVALID_SOCKET, socket_); | |
| 835 int rv = recvfrom(socket_, buf->data(), buf_len, 0, storage.addr, | |
| 836 &storage.addr_len); | |
| 837 if (rv == SOCKET_ERROR) { | |
| 838 int os_error = WSAGetLastError(); | |
| 839 if (os_error == WSAEWOULDBLOCK) { | |
| 840 read_iobuffer_ = buf; | |
| 841 read_iobuffer_len_ = buf_len; | |
| 842 WatchForReadWrite(); | |
| 843 return ERR_IO_PENDING; | |
| 844 } | |
| 845 rv = MapSystemError(os_error); | |
| 846 LogRead(rv, NULL, NULL); | |
| 847 return rv; | |
| 848 } | |
| 849 IPEndPoint address_storage; | |
| 850 IPEndPoint* address_to_log = NULL; | |
| 851 if (rv >= 0) { | |
| 852 if (address_storage.FromSockAddr(storage.addr, storage.addr_len)) { | |
| 853 if (address) | |
| 854 *address = address_storage; | |
| 855 address_to_log = &address_storage; | |
| 856 } else { | |
| 857 rv = ERR_ADDRESS_INVALID; | |
| 858 } | |
| 859 } | |
| 860 LogRead(rv, buf->data(), address_to_log); | |
| 861 return rv; | |
| 862 } | |
| 863 | |
| 864 int UDPSocketWin::InternalSendToNonBlocking(IOBuffer* buf, | |
| 865 int buf_len, | |
| 866 const IPEndPoint* address) { | |
| 867 DCHECK(!write_iobuffer_ || write_iobuffer_.get() == buf); | |
| 868 SockaddrStorage storage; | |
| 869 struct sockaddr* addr = storage.addr; | |
| 870 // Convert address. | |
| 871 if (address) { | |
| 872 if (!address->ToSockAddr(addr, &storage.addr_len)) { | |
| 873 int result = ERR_ADDRESS_INVALID; | |
| 874 LogWrite(result, NULL, NULL); | |
| 875 return result; | |
| 876 } | |
| 877 } else { | |
| 878 addr = NULL; | |
| 879 storage.addr_len = 0; | |
| 880 } | |
| 881 | |
| 882 int rv = sendto(socket_, buf->data(), buf_len, 0, addr, storage.addr_len); | |
| 883 if (rv == SOCKET_ERROR) { | |
| 884 int os_error = WSAGetLastError(); | |
| 885 if (os_error == WSAEWOULDBLOCK) { | |
| 886 write_iobuffer_ = buf; | |
| 887 write_iobuffer_len_ = buf_len; | |
| 888 WatchForReadWrite(); | |
| 889 return ERR_IO_PENDING; | |
| 890 } | |
| 891 rv = MapSystemError(os_error); | |
| 892 LogWrite(rv, NULL, NULL); | |
| 893 return rv; | |
| 894 } | |
| 895 LogWrite(rv, buf->data(), address); | |
| 896 return rv; | |
| 897 } | |
| 898 | |
| 726 int UDPSocketWin::SetMulticastOptions() { | 899 int UDPSocketWin::SetMulticastOptions() { |
| 727 if (!(socket_options_ & SOCKET_OPTION_MULTICAST_LOOP)) { | 900 if (!(socket_options_ & SOCKET_OPTION_MULTICAST_LOOP)) { |
| 728 DWORD loop = 0; | 901 DWORD loop = 0; |
| 729 int protocol_level = | 902 int protocol_level = |
| 730 addr_family_ == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | 903 addr_family_ == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
| 731 int option = | 904 int option = |
| 732 addr_family_ == AF_INET ? IP_MULTICAST_LOOP: IPV6_MULTICAST_LOOP; | 905 addr_family_ == AF_INET ? IP_MULTICAST_LOOP: IPV6_MULTICAST_LOOP; |
| 733 int rv = setsockopt(socket_, protocol_level, option, | 906 int rv = setsockopt(socket_, protocol_level, option, |
| 734 reinterpret_cast<const char*>(&loop), sizeof(loop)); | 907 reinterpret_cast<const char*>(&loop), sizeof(loop)); |
| 735 if (rv < 0) | 908 if (rv < 0) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 800 | 973 |
| 801 for (int i = 0; i < kBindRetries; ++i) { | 974 for (int i = 0; i < kBindRetries; ++i) { |
| 802 int rv = DoBind(IPEndPoint( | 975 int rv = DoBind(IPEndPoint( |
| 803 address, static_cast<uint16>(rand_int_cb_.Run(kPortStart, kPortEnd)))); | 976 address, static_cast<uint16>(rand_int_cb_.Run(kPortStart, kPortEnd)))); |
| 804 if (rv == OK || rv != ERR_ADDRESS_IN_USE) | 977 if (rv == OK || rv != ERR_ADDRESS_IN_USE) |
| 805 return rv; | 978 return rv; |
| 806 } | 979 } |
| 807 return DoBind(IPEndPoint(address, 0)); | 980 return DoBind(IPEndPoint(address, 0)); |
| 808 } | 981 } |
| 809 | 982 |
| 810 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { | |
| 811 SockaddrStorage& storage = core_->recv_addr_storage_; | |
| 812 return address->FromSockAddr(storage.addr, storage.addr_len); | |
| 813 } | |
| 814 | |
| 815 int UDPSocketWin::JoinGroup( | 983 int UDPSocketWin::JoinGroup( |
| 816 const IPAddressNumber& group_address) const { | 984 const IPAddressNumber& group_address) const { |
| 817 DCHECK(CalledOnValidThread()); | 985 DCHECK(CalledOnValidThread()); |
| 818 if (!is_connected()) | 986 if (!is_connected()) |
| 819 return ERR_SOCKET_NOT_CONNECTED; | 987 return ERR_SOCKET_NOT_CONNECTED; |
| 820 | 988 |
| 821 switch (group_address.size()) { | 989 switch (group_address.size()) { |
| 822 case kIPv4AddressSize: { | 990 case kIPv4AddressSize: { |
| 823 if (addr_family_ != AF_INET) | 991 if (addr_family_ != AF_INET) |
| 824 return ERR_ADDRESS_INVALID; | 992 return ERR_ADDRESS_INVALID; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1009 0, | 1177 0, |
| 1010 NULL); | 1178 NULL); |
| 1011 | 1179 |
| 1012 return OK; | 1180 return OK; |
| 1013 } | 1181 } |
| 1014 | 1182 |
| 1015 void UDPSocketWin::DetachFromThread() { | 1183 void UDPSocketWin::DetachFromThread() { |
| 1016 base::NonThreadSafe::DetachFromThread(); | 1184 base::NonThreadSafe::DetachFromThread(); |
| 1017 } | 1185 } |
| 1018 | 1186 |
| 1187 void UDPSocketWin::UseNonBlockingIO() { | |
| 1188 DCHECK(!core_); | |
| 1189 use_non_blocking_io_ = true; | |
| 1190 } | |
| 1191 | |
| 1019 } // namespace net | 1192 } // namespace net |
| OLD | NEW |