| 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/socket/tcp_client_socket_win.h" | 5 #include "net/socket/tcp_client_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/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 int TCPClientSocketWin::Bind(const IPEndPoint& address) { | 366 int TCPClientSocketWin::Bind(const IPEndPoint& address) { |
| 367 if (current_address_index_ >= 0 || bind_address_.get()) { | 367 if (current_address_index_ >= 0 || bind_address_.get()) { |
| 368 // Cannot bind the socket if we are already connected or connecting. | 368 // Cannot bind the socket if we are already connected or connecting. |
| 369 return ERR_UNEXPECTED; | 369 return ERR_UNEXPECTED; |
| 370 } | 370 } |
| 371 | 371 |
| 372 SockaddrStorage storage; | 372 SockaddrStorage storage; |
| 373 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 373 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 374 return ERR_INVALID_ARGUMENT; | 374 return ERR_INVALID_ARGUMENT; |
| 375 | 375 |
| 376 // Create |bound_socket_| and try to bound it to |address|. | 376 // Create |bound_socket_| and try to bind it to |address|. |
| 377 int error = CreateSocket(address.GetFamily(), &bound_socket_); | 377 int error = CreateSocket(address.GetFamily(), &bound_socket_); |
| 378 if (error) | 378 if (error) |
| 379 return MapSystemError(error); | 379 return MapSystemError(error); |
| 380 | 380 |
| 381 if (bind(bound_socket_, storage.addr, storage.addr_len)) { | 381 if (bind(bound_socket_, storage.addr, storage.addr_len)) { |
| 382 error = errno; | 382 error = errno; |
| 383 if (closesocket(bound_socket_) < 0) | 383 if (closesocket(bound_socket_) < 0) |
| 384 PLOG(ERROR) << "closesocket"; | 384 PLOG(ERROR) << "closesocket"; |
| 385 bound_socket_ = INVALID_SOCKET; | 385 bound_socket_ = INVALID_SOCKET; |
| 386 return MapSystemError(error); | 386 return MapSystemError(error); |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 next_connect_state_ = CONNECT_STATE_CONNECT; | 546 next_connect_state_ = CONNECT_STATE_CONNECT; |
| 547 ++current_address_index_; | 547 ++current_address_index_; |
| 548 return OK; | 548 return OK; |
| 549 } | 549 } |
| 550 | 550 |
| 551 // Otherwise there is nothing to fall back to, so give up. | 551 // Otherwise there is nothing to fall back to, so give up. |
| 552 return result; | 552 return result; |
| 553 } | 553 } |
| 554 | 554 |
| 555 void TCPClientSocketWin::Disconnect() { | 555 void TCPClientSocketWin::Disconnect() { |
| 556 DCHECK(CalledOnValidThread()); |
| 557 |
| 556 DoDisconnect(); | 558 DoDisconnect(); |
| 557 current_address_index_ = -1; | 559 current_address_index_ = -1; |
| 560 bind_address_.reset(); |
| 558 } | 561 } |
| 559 | 562 |
| 560 void TCPClientSocketWin::DoDisconnect() { | 563 void TCPClientSocketWin::DoDisconnect() { |
| 561 DCHECK(CalledOnValidThread()); | 564 DCHECK(CalledOnValidThread()); |
| 562 | 565 |
| 563 if (socket_ == INVALID_SOCKET) | 566 if (socket_ == INVALID_SOCKET) |
| 564 return; | 567 return; |
| 565 | 568 |
| 566 // Note: don't use CancelIo to cancel pending IO because it doesn't work | 569 // Note: don't use CancelIo to cancel pending IO because it doesn't work |
| 567 // when there is a Winsock layered service provider. | 570 // when there is a Winsock layered service provider. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 DCHECK(address); | 642 DCHECK(address); |
| 640 if (!IsConnected()) | 643 if (!IsConnected()) |
| 641 return ERR_SOCKET_NOT_CONNECTED; | 644 return ERR_SOCKET_NOT_CONNECTED; |
| 642 *address = addresses_[current_address_index_]; | 645 *address = addresses_[current_address_index_]; |
| 643 return OK; | 646 return OK; |
| 644 } | 647 } |
| 645 | 648 |
| 646 int TCPClientSocketWin::GetLocalAddress(IPEndPoint* address) const { | 649 int TCPClientSocketWin::GetLocalAddress(IPEndPoint* address) const { |
| 647 DCHECK(CalledOnValidThread()); | 650 DCHECK(CalledOnValidThread()); |
| 648 DCHECK(address); | 651 DCHECK(address); |
| 649 if (!IsConnected()) | 652 if (socket_ == INVALID_SOCKET) { |
| 653 if (bind_address_.get()) { |
| 654 *address = *bind_address_; |
| 655 return OK; |
| 656 } |
| 650 return ERR_SOCKET_NOT_CONNECTED; | 657 return ERR_SOCKET_NOT_CONNECTED; |
| 658 } |
| 651 | 659 |
| 652 struct sockaddr_storage addr_storage; | 660 struct sockaddr_storage addr_storage; |
| 653 socklen_t addr_len = sizeof(addr_storage); | 661 socklen_t addr_len = sizeof(addr_storage); |
| 654 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | 662 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
| 655 if (getsockname(socket_, addr, &addr_len)) | 663 if (getsockname(socket_, addr, &addr_len)) |
| 656 return MapSystemError(WSAGetLastError()); | 664 return MapSystemError(WSAGetLastError()); |
| 657 if (!address->FromSockAddr(addr, addr_len)) | 665 if (!address->FromSockAddr(addr, addr_len)) |
| 658 return ERR_FAILED; | 666 return ERR_FAILED; |
| 659 return OK; | 667 return OK; |
| 660 } | 668 } |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 941 use_history_.set_was_used_to_convey_data(); | 949 use_history_.set_was_used_to_convey_data(); |
| 942 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, | 950 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, |
| 943 core_->write_buffer_.buf); | 951 core_->write_buffer_.buf); |
| 944 } | 952 } |
| 945 } | 953 } |
| 946 core_->write_iobuffer_ = NULL; | 954 core_->write_iobuffer_ = NULL; |
| 947 DoWriteCallback(rv); | 955 DoWriteCallback(rv); |
| 948 } | 956 } |
| 949 | 957 |
| 950 } // namespace net | 958 } // namespace net |
| OLD | NEW |