Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: net/socket/tcp_client_socket_win.cc

Issue 8831001: base::Bind: Convert Socket::Write. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix alignment. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/socket/tcp_client_socket_win.h ('k') | net/socket/transport_client_socket_pool_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 old_read_callback_(NULL), 320 old_read_callback_(NULL),
321 write_callback_(NULL), 321 old_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);
331 EnsureWinsockInit(); 331 EnsureWinsockInit();
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 core_->read_iobuffer_ = buf; 785 core_->read_iobuffer_ = buf;
786 return ERR_IO_PENDING; 786 return ERR_IO_PENDING;
787 } 787 }
788 788
789 int TCPClientSocketWin::Write(IOBuffer* buf, 789 int TCPClientSocketWin::Write(IOBuffer* buf,
790 int buf_len, 790 int buf_len,
791 OldCompletionCallback* callback) { 791 OldCompletionCallback* callback) {
792 DCHECK(CalledOnValidThread()); 792 DCHECK(CalledOnValidThread());
793 DCHECK_NE(socket_, INVALID_SOCKET); 793 DCHECK_NE(socket_, INVALID_SOCKET);
794 DCHECK(!waiting_write_); 794 DCHECK(!waiting_write_);
795 DCHECK(!write_callback_); 795 DCHECK(!old_write_callback_ && write_callback_.is_null());
796 DCHECK_GT(buf_len, 0); 796 DCHECK_GT(buf_len, 0);
797 DCHECK(!core_->write_iobuffer_); 797 DCHECK(!core_->write_iobuffer_);
798 798
799 base::StatsCounter writes("tcp.writes");
800 writes.Increment();
801
802 core_->write_buffer_.len = buf_len;
803 core_->write_buffer_.buf = buf->data();
804 core_->write_buffer_length_ = buf_len;
805
806 // TODO(wtc): Remove the assertion after enough testing.
807 AssertEventNotSignaled(core_->write_overlapped_.hEvent);
808 DWORD num;
809 int rv = WSASend(socket_, &core_->write_buffer_, 1, &num, 0,
810 &core_->write_overlapped_, NULL);
811 if (rv == 0) {
812 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) {
813 rv = static_cast<int>(num);
814 if (rv > buf_len || rv < 0) {
815 // It seems that some winsock interceptors report that more was written
816 // than was available. Treat this as an error. http://crbug.com/27870
817 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len
818 << " bytes, but " << rv << " bytes reported.";
819 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES;
820 }
821 base::StatsCounter write_bytes("tcp.write_bytes");
822 write_bytes.Add(rv);
823 if (rv > 0)
824 use_history_.set_was_used_to_convey_data();
825 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv,
826 core_->write_buffer_.buf);
827 return rv;
828 }
829 } else {
830 int os_error = WSAGetLastError();
831 if (os_error != WSA_IO_PENDING)
832 return MapSystemError(os_error);
833 }
834 core_->WatchForWrite();
835 waiting_write_ = true;
836 old_write_callback_ = callback;
837 core_->write_iobuffer_ = buf;
838 return ERR_IO_PENDING;
839 }
840 int TCPClientSocketWin::Write(IOBuffer* buf,
841 int buf_len,
842 const CompletionCallback& callback) {
843 DCHECK(CalledOnValidThread());
844 DCHECK_NE(socket_, INVALID_SOCKET);
845 DCHECK(!waiting_write_);
846 DCHECK(!old_write_callback_ && write_callback_.is_null());
847 DCHECK_GT(buf_len, 0);
848 DCHECK(!core_->write_iobuffer_);
849
799 base::StatsCounter writes("tcp.writes"); 850 base::StatsCounter writes("tcp.writes");
800 writes.Increment(); 851 writes.Increment();
801 852
802 core_->write_buffer_.len = buf_len; 853 core_->write_buffer_.len = buf_len;
803 core_->write_buffer_.buf = buf->data(); 854 core_->write_buffer_.buf = buf->data();
804 core_->write_buffer_length_ = buf_len; 855 core_->write_buffer_length_ = buf_len;
805 856
806 // TODO(wtc): Remove the assertion after enough testing. 857 // TODO(wtc): Remove the assertion after enough testing.
807 AssertEventNotSignaled(core_->write_overlapped_.hEvent); 858 AssertEventNotSignaled(core_->write_overlapped_.hEvent);
808 DWORD num; 859 DWORD num;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 c->Run(rv); 941 c->Run(rv);
891 } else { 942 } else {
892 CompletionCallback c = read_callback_; 943 CompletionCallback c = read_callback_;
893 read_callback_.Reset(); 944 read_callback_.Reset();
894 c.Run(rv); 945 c.Run(rv);
895 } 946 }
896 } 947 }
897 948
898 void TCPClientSocketWin::DoWriteCallback(int rv) { 949 void TCPClientSocketWin::DoWriteCallback(int rv) {
899 DCHECK_NE(rv, ERR_IO_PENDING); 950 DCHECK_NE(rv, ERR_IO_PENDING);
900 DCHECK(write_callback_); 951 DCHECK(old_write_callback_ || !write_callback_.is_null());
901 952
902 // since Run may result in Write being called, clear write_callback_ up front. 953 // Since Run may result in Write being called, clear old_write_callback_ up
903 OldCompletionCallback* c = write_callback_; 954 // front.
904 write_callback_ = NULL; 955 if (old_write_callback_) {
905 c->Run(rv); 956 OldCompletionCallback* c = old_write_callback_;
957 old_write_callback_ = NULL;
958 c->Run(rv);
959 } else {
960 CompletionCallback c = write_callback_;
961 write_callback_.Reset();
962 c.Run(rv);
963 }
906 } 964 }
907 965
908 void TCPClientSocketWin::DidCompleteConnect() { 966 void TCPClientSocketWin::DidCompleteConnect() {
909 DCHECK_EQ(next_connect_state_, CONNECT_STATE_CONNECT_COMPLETE); 967 DCHECK_EQ(next_connect_state_, CONNECT_STATE_CONNECT_COMPLETE);
910 int result; 968 int result;
911 969
912 WSANETWORKEVENTS events; 970 WSANETWORKEVENTS events;
913 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent, 971 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent,
914 &events); 972 &events);
915 int os_error = 0; 973 int os_error = 0;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 use_history_.set_was_used_to_convey_data(); 1038 use_history_.set_was_used_to_convey_data();
981 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, 1039 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes,
982 core_->write_buffer_.buf); 1040 core_->write_buffer_.buf);
983 } 1041 }
984 } 1042 }
985 core_->write_iobuffer_ = NULL; 1043 core_->write_iobuffer_ = NULL;
986 DoWriteCallback(rv); 1044 DoWriteCallback(rv);
987 } 1045 }
988 1046
989 } // namespace net 1047 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/tcp_client_socket_win.h ('k') | net/socket/transport_client_socket_pool_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698