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/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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
310 | 310 |
311 TCPClientSocketWin::TCPClientSocketWin(const AddressList& addresses, | 311 TCPClientSocketWin::TCPClientSocketWin(const AddressList& addresses, |
312 net::NetLog* net_log, | 312 net::NetLog* net_log, |
313 const net::NetLog::Source& source) | 313 const net::NetLog::Source& source) |
314 : socket_(INVALID_SOCKET), | 314 : socket_(INVALID_SOCKET), |
315 bound_socket_(INVALID_SOCKET), | 315 bound_socket_(INVALID_SOCKET), |
316 addresses_(addresses), | 316 addresses_(addresses), |
317 current_ai_(NULL), | 317 current_ai_(NULL), |
318 waiting_read_(false), | 318 waiting_read_(false), |
319 waiting_write_(false), | 319 waiting_write_(false), |
320 read_callback_(NULL), | 320 old_read_callback_(NULL), |
321 write_callback_(NULL), | 321 write_callback_(NULL), |
322 next_connect_state_(CONNECT_STATE_NONE), | 322 next_connect_state_(CONNECT_STATE_NONE), |
323 connect_os_error_(0), | 323 connect_os_error_(0), |
324 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), | 324 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), |
325 previously_disconnected_(false), | 325 previously_disconnected_(false), |
326 num_bytes_read_(0) { | 326 num_bytes_read_(0) { |
327 scoped_refptr<NetLog::EventParameters> params; | 327 scoped_refptr<NetLog::EventParameters> params; |
328 if (source.is_valid()) | 328 if (source.is_valid()) |
329 params = new NetLogSourceParameter("source_dependency", source); | 329 params = new NetLogSourceParameter("source_dependency", source); |
330 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | 330 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
397 | 397 |
398 // We will try to connect to each address in addresses_. Start with the | 398 // We will try to connect to each address in addresses_. Start with the |
399 // first one in the list. | 399 // first one in the list. |
400 next_connect_state_ = CONNECT_STATE_CONNECT; | 400 next_connect_state_ = CONNECT_STATE_CONNECT; |
401 current_ai_ = addresses_.head(); | 401 current_ai_ = addresses_.head(); |
402 | 402 |
403 int rv = DoConnectLoop(OK); | 403 int rv = DoConnectLoop(OK); |
404 if (rv == ERR_IO_PENDING) { | 404 if (rv == ERR_IO_PENDING) { |
405 // Synchronous operation not supported. | 405 // Synchronous operation not supported. |
406 DCHECK(callback); | 406 DCHECK(callback); |
407 old_read_callback_ = callback; | |
408 } else { | |
409 LogConnectCompletion(rv); | |
410 } | |
411 | |
412 return rv; | |
413 } | |
414 int TCPClientSocketWin::Connect(const CompletionCallback& callback) { | |
415 DCHECK(CalledOnValidThread()); | |
416 | |
417 // If already connected, then just return OK. | |
418 if (socket_ != INVALID_SOCKET) | |
419 return OK; | |
420 | |
421 base::StatsCounter connects("tcp.connect"); | |
422 connects.Increment(); | |
423 | |
424 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, | |
425 new AddressListNetLogParam(addresses_)); | |
426 | |
427 // We will try to connect to each address in addresses_. Start with the | |
428 // first one in the list. | |
429 next_connect_state_ = CONNECT_STATE_CONNECT; | |
430 current_ai_ = addresses_.head(); | |
431 | |
432 int rv = DoConnectLoop(OK); | |
433 if (rv == ERR_IO_PENDING) { | |
434 // Synchronous operation not supported. | |
435 DCHECK(!callback.is_null()); | |
407 read_callback_ = callback; | 436 read_callback_ = callback; |
408 } else { | 437 } else { |
409 LogConnectCompletion(rv); | 438 LogConnectCompletion(rv); |
410 } | 439 } |
411 | 440 |
412 return rv; | 441 return rv; |
413 } | 442 } |
414 | 443 |
415 int TCPClientSocketWin::DoConnectLoop(int result) { | 444 int TCPClientSocketWin::DoConnectLoop(int result) { |
416 DCHECK_NE(next_connect_state_, CONNECT_STATE_NONE); | 445 DCHECK_NE(next_connect_state_, CONNECT_STATE_NONE); |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
673 base::TimeDelta TCPClientSocketWin::GetConnectTimeMicros() const { | 702 base::TimeDelta TCPClientSocketWin::GetConnectTimeMicros() const { |
674 return connect_time_micros_; | 703 return connect_time_micros_; |
675 } | 704 } |
676 | 705 |
677 int TCPClientSocketWin::Read(IOBuffer* buf, | 706 int TCPClientSocketWin::Read(IOBuffer* buf, |
678 int buf_len, | 707 int buf_len, |
679 OldCompletionCallback* callback) { | 708 OldCompletionCallback* callback) { |
680 DCHECK(CalledOnValidThread()); | 709 DCHECK(CalledOnValidThread()); |
681 DCHECK_NE(socket_, INVALID_SOCKET); | 710 DCHECK_NE(socket_, INVALID_SOCKET); |
682 DCHECK(!waiting_read_); | 711 DCHECK(!waiting_read_); |
683 DCHECK(!read_callback_); | 712 DCHECK(!old_read_callback_); |
csilv
2011/12/06 21:03:18
also check for read_callback here?
James Hawkins
2011/12/06 22:19:30
Done.
| |
684 DCHECK(!core_->read_iobuffer_); | 713 DCHECK(!core_->read_iobuffer_); |
685 | 714 |
686 buf_len = core_->ThrottleReadSize(buf_len); | 715 buf_len = core_->ThrottleReadSize(buf_len); |
687 | 716 |
688 core_->read_buffer_.len = buf_len; | 717 core_->read_buffer_.len = buf_len; |
689 core_->read_buffer_.buf = buf->data(); | 718 core_->read_buffer_.buf = buf->data(); |
690 | 719 |
691 // TODO(wtc): Remove the assertion after enough testing. | 720 // TODO(wtc): Remove the assertion after enough testing. |
692 AssertEventNotSignaled(core_->read_overlapped_.hEvent); | 721 AssertEventNotSignaled(core_->read_overlapped_.hEvent); |
693 DWORD num, flags = 0; | 722 DWORD num, flags = 0; |
(...skipping 10 matching lines...) Expand all Loading... | |
704 core_->read_buffer_.buf); | 733 core_->read_buffer_.buf); |
705 return static_cast<int>(num); | 734 return static_cast<int>(num); |
706 } | 735 } |
707 } else { | 736 } else { |
708 int os_error = WSAGetLastError(); | 737 int os_error = WSAGetLastError(); |
709 if (os_error != WSA_IO_PENDING) | 738 if (os_error != WSA_IO_PENDING) |
710 return MapSystemError(os_error); | 739 return MapSystemError(os_error); |
711 } | 740 } |
712 core_->WatchForRead(); | 741 core_->WatchForRead(); |
713 waiting_read_ = true; | 742 waiting_read_ = true; |
714 read_callback_ = callback; | 743 old_read_callback_ = callback; |
715 core_->read_iobuffer_ = buf; | 744 core_->read_iobuffer_ = buf; |
716 return ERR_IO_PENDING; | 745 return ERR_IO_PENDING; |
717 } | 746 } |
718 | 747 |
719 int TCPClientSocketWin::Write(IOBuffer* buf, | 748 int TCPClientSocketWin::Write(IOBuffer* buf, |
720 int buf_len, | 749 int buf_len, |
721 OldCompletionCallback* callback) { | 750 OldCompletionCallback* callback) { |
722 DCHECK(CalledOnValidThread()); | 751 DCHECK(CalledOnValidThread()); |
723 DCHECK_NE(socket_, INVALID_SOCKET); | 752 DCHECK_NE(socket_, INVALID_SOCKET); |
724 DCHECK(!waiting_write_); | 753 DCHECK(!waiting_write_); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
804 reinterpret_cast<const struct sockaddr*>(&source_address), | 833 reinterpret_cast<const struct sockaddr*>(&source_address), |
805 sizeof(source_address)); | 834 sizeof(source_address)); |
806 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, | 835 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, |
807 make_scoped_refptr(new NetLogStringParameter( | 836 make_scoped_refptr(new NetLogStringParameter( |
808 "source_address", | 837 "source_address", |
809 source_address_str))); | 838 source_address_str))); |
810 } | 839 } |
811 | 840 |
812 void TCPClientSocketWin::DoReadCallback(int rv) { | 841 void TCPClientSocketWin::DoReadCallback(int rv) { |
813 DCHECK_NE(rv, ERR_IO_PENDING); | 842 DCHECK_NE(rv, ERR_IO_PENDING); |
814 DCHECK(read_callback_); | 843 DCHECK(old_read_callback_ || !read_callback_.is_null()); |
815 | 844 |
816 // since Run may result in Read being called, clear read_callback_ up front. | 845 // since Run may result in Read being called, clear read_callback_ up front. |
817 OldCompletionCallback* c = read_callback_; | 846 if (old_read_callback_) { |
818 read_callback_ = NULL; | 847 OldCompletionCallback* c = old_read_callback_; |
819 c->Run(rv); | 848 old_read_callback_ = NULL; |
849 c->Run(rv); | |
850 } else { | |
851 CompletionCallback c = read_callback_; | |
852 read_callback_.Reset(); | |
853 c.Run(rv); | |
854 } | |
820 } | 855 } |
821 | 856 |
822 void TCPClientSocketWin::DoWriteCallback(int rv) { | 857 void TCPClientSocketWin::DoWriteCallback(int rv) { |
823 DCHECK_NE(rv, ERR_IO_PENDING); | 858 DCHECK_NE(rv, ERR_IO_PENDING); |
824 DCHECK(write_callback_); | 859 DCHECK(write_callback_); |
825 | 860 |
826 // since Run may result in Write being called, clear write_callback_ up front. | 861 // since Run may result in Write being called, clear write_callback_ up front. |
827 OldCompletionCallback* c = write_callback_; | 862 OldCompletionCallback* c = write_callback_; |
828 write_callback_ = NULL; | 863 write_callback_ = NULL; |
829 c->Run(rv); | 864 c->Run(rv); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
904 use_history_.set_was_used_to_convey_data(); | 939 use_history_.set_was_used_to_convey_data(); |
905 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, | 940 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, |
906 core_->write_buffer_.buf); | 941 core_->write_buffer_.buf); |
907 } | 942 } |
908 } | 943 } |
909 core_->write_iobuffer_ = NULL; | 944 core_->write_iobuffer_ = NULL; |
910 DoWriteCallback(rv); | 945 DoWriteCallback(rv); |
911 } | 946 } |
912 | 947 |
913 } // namespace net | 948 } // namespace net |
OLD | NEW |