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/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
730 : MockClientSocket(net_log), | 730 : MockClientSocket(net_log), |
731 addresses_(addresses), | 731 addresses_(addresses), |
732 data_(data), | 732 data_(data), |
733 read_offset_(0), | 733 read_offset_(0), |
734 num_bytes_read_(0), | 734 num_bytes_read_(0), |
735 read_data_(false, net::ERR_UNEXPECTED), | 735 read_data_(false, net::ERR_UNEXPECTED), |
736 need_read_data_(true), | 736 need_read_data_(true), |
737 peer_closed_connection_(false), | 737 peer_closed_connection_(false), |
738 pending_buf_(NULL), | 738 pending_buf_(NULL), |
739 pending_buf_len_(0), | 739 pending_buf_len_(0), |
740 pending_callback_(NULL), | 740 old_pending_callback_(NULL), |
741 was_used_to_convey_data_(false) { | 741 was_used_to_convey_data_(false) { |
742 DCHECK(data_); | 742 DCHECK(data_); |
743 data_->Reset(); | 743 data_->Reset(); |
744 } | 744 } |
745 | 745 |
746 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 746 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
747 net::OldCompletionCallback* callback) { | 747 net::OldCompletionCallback* callback) { |
748 if (!connected_) | 748 if (!connected_) |
749 return net::ERR_UNEXPECTED; | 749 return net::ERR_UNEXPECTED; |
750 | 750 |
751 // If the buffer is already in use, a read is already in progress! | 751 // If the buffer is already in use, a read is already in progress! |
752 DCHECK(pending_buf_ == NULL); | 752 DCHECK(pending_buf_ == NULL); |
753 | 753 |
754 // Store our async IO data. | 754 // Store our async IO data. |
755 pending_buf_ = buf; | 755 pending_buf_ = buf; |
756 pending_buf_len_ = buf_len; | 756 pending_buf_len_ = buf_len; |
757 pending_callback_ = callback; | 757 old_pending_callback_ = callback; |
758 | 758 |
759 if (need_read_data_) { | 759 if (need_read_data_) { |
760 read_data_ = data_->GetNextRead(); | 760 read_data_ = data_->GetNextRead(); |
761 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | 761 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { |
762 // This MockRead is just a marker to instruct us to set | 762 // This MockRead is just a marker to instruct us to set |
763 // peer_closed_connection_. Skip it and get the next one. | 763 // peer_closed_connection_. Skip it and get the next one. |
764 read_data_ = data_->GetNextRead(); | 764 read_data_ = data_->GetNextRead(); |
765 peer_closed_connection_ = true; | 765 peer_closed_connection_ = true; |
766 } | 766 } |
767 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | 767 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
768 // to complete the async IO manually later (via OnReadComplete). | 768 // to complete the async IO manually later (via OnReadComplete). |
769 if (read_data_.result == ERR_IO_PENDING) { | 769 if (read_data_.result == ERR_IO_PENDING) { |
770 DCHECK(callback); // We need to be using async IO in this case. | 770 DCHECK(callback); // We need to be using async IO in this case. |
771 return ERR_IO_PENDING; | 771 return ERR_IO_PENDING; |
772 } | 772 } |
773 need_read_data_ = false; | 773 need_read_data_ = false; |
774 } | 774 } |
775 | 775 |
776 return CompleteRead(); | 776 return CompleteRead(); |
777 } | 777 } |
778 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
779 const net::CompletionCallback& callback) { | |
780 if (!connected_) | |
781 return net::ERR_UNEXPECTED; | |
782 | |
783 // If the buffer is already in use, a read is already in progress! | |
784 DCHECK(pending_buf_ == NULL); | |
785 | |
786 // Store our async IO data. | |
787 pending_buf_ = buf; | |
788 pending_buf_len_ = buf_len; | |
789 pending_callback_ = callback; | |
790 | |
791 if (need_read_data_) { | |
792 read_data_ = data_->GetNextRead(); | |
793 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { | |
794 // This MockRead is just a marker to instruct us to set | |
795 // peer_closed_connection_. Skip it and get the next one. | |
796 read_data_ = data_->GetNextRead(); | |
797 peer_closed_connection_ = true; | |
798 } | |
799 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | |
800 // to complete the async IO manually later (via OnReadComplete). | |
801 if (read_data_.result == ERR_IO_PENDING) { | |
802 // We need to be using async IO in this case. | |
803 DCHECK(!callback.is_null()); | |
804 return ERR_IO_PENDING; | |
805 } | |
806 need_read_data_ = false; | |
807 } | |
808 | |
809 return CompleteRead(); | |
810 } | |
778 | 811 |
779 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 812 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
780 net::OldCompletionCallback* callback) { | 813 net::OldCompletionCallback* callback) { |
781 DCHECK(buf); | 814 DCHECK(buf); |
782 DCHECK_GT(buf_len, 0); | 815 DCHECK_GT(buf_len, 0); |
783 | 816 |
784 if (!connected_) | 817 if (!connected_) |
785 return net::ERR_UNEXPECTED; | 818 return net::ERR_UNEXPECTED; |
786 | 819 |
787 std::string data(buf->data(), buf_len); | 820 std::string data(buf->data(), buf_len); |
(...skipping 29 matching lines...) Expand all Loading... | |
817 if (data_->connect_data().async) { | 850 if (data_->connect_data().async) { |
818 RunCallbackAsync(callback, data_->connect_data().result); | 851 RunCallbackAsync(callback, data_->connect_data().result); |
819 return net::ERR_IO_PENDING; | 852 return net::ERR_IO_PENDING; |
820 } | 853 } |
821 | 854 |
822 return data_->connect_data().result; | 855 return data_->connect_data().result; |
823 } | 856 } |
824 | 857 |
825 void MockTCPClientSocket::Disconnect() { | 858 void MockTCPClientSocket::Disconnect() { |
826 MockClientSocket::Disconnect(); | 859 MockClientSocket::Disconnect(); |
827 pending_callback_ = NULL; | 860 old_pending_callback_ = NULL; |
861 pending_callback_.Reset(); | |
828 } | 862 } |
829 | 863 |
830 bool MockTCPClientSocket::IsConnected() const { | 864 bool MockTCPClientSocket::IsConnected() const { |
831 return connected_ && !peer_closed_connection_; | 865 return connected_ && !peer_closed_connection_; |
832 } | 866 } |
833 | 867 |
834 bool MockTCPClientSocket::IsConnectedAndIdle() const { | 868 bool MockTCPClientSocket::IsConnectedAndIdle() const { |
835 return IsConnected(); | 869 return IsConnected(); |
836 } | 870 } |
837 | 871 |
(...skipping 30 matching lines...) Expand all Loading... | |
868 // Since we've been waiting for data, need_read_data_ should be true. | 902 // Since we've been waiting for data, need_read_data_ should be true. |
869 DCHECK(need_read_data_); | 903 DCHECK(need_read_data_); |
870 | 904 |
871 read_data_ = data; | 905 read_data_ = data; |
872 need_read_data_ = false; | 906 need_read_data_ = false; |
873 | 907 |
874 // The caller is simulating that this IO completes right now. Don't | 908 // The caller is simulating that this IO completes right now. Don't |
875 // let CompleteRead() schedule a callback. | 909 // let CompleteRead() schedule a callback. |
876 read_data_.async = false; | 910 read_data_.async = false; |
877 | 911 |
878 net::OldCompletionCallback* callback = pending_callback_; | 912 if (old_pending_callback_) { |
879 int rv = CompleteRead(); | 913 net::OldCompletionCallback* callback = old_pending_callback_; |
880 RunOldCallback(callback, rv); | 914 int rv = CompleteRead(); |
915 RunOldCallback(callback, rv); | |
916 } else { | |
917 net::CompletionCallback callback = pending_callback_; | |
918 int rv = CompleteRead(); | |
919 RunCallback(callback, rv); | |
920 } | |
881 } | 921 } |
882 | 922 |
883 int MockTCPClientSocket::CompleteRead() { | 923 int MockTCPClientSocket::CompleteRead() { |
884 DCHECK(pending_buf_); | 924 DCHECK(pending_buf_); |
885 DCHECK(pending_buf_len_ > 0); | 925 DCHECK(pending_buf_len_ > 0); |
886 | 926 |
887 was_used_to_convey_data_ = true; | 927 was_used_to_convey_data_ = true; |
888 | 928 |
889 // Save the pending async IO data and reset our |pending_| state. | 929 // Save the pending async IO data and reset our |pending_| state. |
890 net::IOBuffer* buf = pending_buf_; | 930 net::IOBuffer* buf = pending_buf_; |
891 int buf_len = pending_buf_len_; | 931 int buf_len = pending_buf_len_; |
892 net::OldCompletionCallback* callback = pending_callback_; | 932 net::OldCompletionCallback* old_callback = old_pending_callback_; |
933 net::CompletionCallback callback = pending_callback_; | |
893 pending_buf_ = NULL; | 934 pending_buf_ = NULL; |
894 pending_buf_len_ = 0; | 935 pending_buf_len_ = 0; |
895 pending_callback_ = NULL; | 936 old_pending_callback_ = NULL; |
937 pending_callback_.Reset(); | |
896 | 938 |
897 int result = read_data_.result; | 939 int result = read_data_.result; |
898 DCHECK(result != ERR_IO_PENDING); | 940 DCHECK(result != ERR_IO_PENDING); |
899 | 941 |
900 if (read_data_.data) { | 942 if (read_data_.data) { |
901 if (read_data_.data_len - read_offset_ > 0) { | 943 if (read_data_.data_len - read_offset_ > 0) { |
902 result = std::min(buf_len, read_data_.data_len - read_offset_); | 944 result = std::min(buf_len, read_data_.data_len - read_offset_); |
903 memcpy(buf->data(), read_data_.data + read_offset_, result); | 945 memcpy(buf->data(), read_data_.data + read_offset_, result); |
904 read_offset_ += result; | 946 read_offset_ += result; |
905 num_bytes_read_ += result; | 947 num_bytes_read_ += result; |
906 if (read_offset_ == read_data_.data_len) { | 948 if (read_offset_ == read_data_.data_len) { |
907 need_read_data_ = true; | 949 need_read_data_ = true; |
908 read_offset_ = 0; | 950 read_offset_ = 0; |
909 } | 951 } |
910 } else { | 952 } else { |
911 result = 0; // EOF | 953 result = 0; // EOF |
912 } | 954 } |
913 } | 955 } |
914 | 956 |
915 if (read_data_.async) { | 957 if (read_data_.async) { |
916 DCHECK(callback); | 958 DCHECK(old_callback || !callback.is_null()); |
917 RunCallbackAsync(callback, result); | 959 if (old_callback) |
960 RunCallbackAsync(old_callback, result); | |
961 else | |
962 RunCallbackAsync(callback, result); | |
918 return net::ERR_IO_PENDING; | 963 return net::ERR_IO_PENDING; |
919 } | 964 } |
920 return result; | 965 return result; |
921 } | 966 } |
922 | 967 |
923 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( | 968 DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( |
924 net::NetLog* net_log, net::DeterministicSocketData* data) | 969 net::NetLog* net_log, net::DeterministicSocketData* data) |
925 : MockClientSocket(net_log), | 970 : MockClientSocket(net_log), |
926 write_pending_(false), | 971 write_pending_(false), |
927 write_callback_(NULL), | 972 write_callback_(NULL), |
928 write_result_(0), | 973 write_result_(0), |
929 read_data_(), | 974 read_data_(), |
930 read_buf_(NULL), | 975 read_buf_(NULL), |
931 read_buf_len_(0), | 976 read_buf_len_(0), |
932 read_pending_(false), | 977 read_pending_(false), |
933 read_callback_(NULL), | 978 old_read_callback_(NULL), |
934 data_(data), | 979 data_(data), |
935 was_used_to_convey_data_(false) {} | 980 was_used_to_convey_data_(false) {} |
936 | 981 |
937 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} | 982 DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} |
938 | 983 |
939 void DeterministicMockTCPClientSocket::CompleteWrite() { | 984 void DeterministicMockTCPClientSocket::CompleteWrite() { |
940 was_used_to_convey_data_ = true; | 985 was_used_to_convey_data_ = true; |
941 write_pending_ = false; | 986 write_pending_ = false; |
942 write_callback_->Run(write_result_); | 987 write_callback_->Run(write_result_); |
943 } | 988 } |
(...skipping 13 matching lines...) Expand all Loading... | |
957 int result = read_data_.result; | 1002 int result = read_data_.result; |
958 | 1003 |
959 if (read_data_.data_len > 0) { | 1004 if (read_data_.data_len > 0) { |
960 DCHECK(read_data_.data); | 1005 DCHECK(read_data_.data); |
961 result = std::min(read_buf_len_, read_data_.data_len); | 1006 result = std::min(read_buf_len_, read_data_.data_len); |
962 memcpy(read_buf_->data(), read_data_.data, result); | 1007 memcpy(read_buf_->data(), read_data_.data, result); |
963 } | 1008 } |
964 | 1009 |
965 if (read_pending_) { | 1010 if (read_pending_) { |
966 read_pending_ = false; | 1011 read_pending_ = false; |
967 read_callback_->Run(result); | 1012 if (old_read_callback_) |
1013 old_read_callback_->Run(result); | |
1014 else | |
1015 read_callback_.Run(result); | |
968 } | 1016 } |
969 | 1017 |
970 return result; | 1018 return result; |
971 } | 1019 } |
972 | 1020 |
973 int DeterministicMockTCPClientSocket::Write( | 1021 int DeterministicMockTCPClientSocket::Write( |
974 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { | 1022 net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { |
975 DCHECK(buf); | 1023 DCHECK(buf); |
976 DCHECK_GT(buf_len, 0); | 1024 DCHECK_GT(buf_len, 0); |
977 | 1025 |
(...skipping 21 matching lines...) Expand all Loading... | |
999 if (!connected_) | 1047 if (!connected_) |
1000 return net::ERR_UNEXPECTED; | 1048 return net::ERR_UNEXPECTED; |
1001 | 1049 |
1002 read_data_ = data_->GetNextRead(); | 1050 read_data_ = data_->GetNextRead(); |
1003 // The buffer should always be big enough to contain all the MockRead data. To | 1051 // The buffer should always be big enough to contain all the MockRead data. To |
1004 // use small buffers, split the data into multiple MockReads. | 1052 // use small buffers, split the data into multiple MockReads. |
1005 DCHECK_LE(read_data_.data_len, buf_len); | 1053 DCHECK_LE(read_data_.data_len, buf_len); |
1006 | 1054 |
1007 read_buf_ = buf; | 1055 read_buf_ = buf; |
1008 read_buf_len_ = buf_len; | 1056 read_buf_len_ = buf_len; |
1057 old_read_callback_ = callback; | |
1058 | |
1059 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | |
1060 read_pending_ = true; | |
1061 DCHECK(old_read_callback_); | |
1062 return ERR_IO_PENDING; | |
1063 } | |
1064 | |
1065 was_used_to_convey_data_ = true; | |
1066 return CompleteRead(); | |
1067 } | |
1068 int DeterministicMockTCPClientSocket::Read( | |
1069 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { | |
1070 if (!connected_) | |
1071 return net::ERR_UNEXPECTED; | |
1072 | |
1073 read_data_ = data_->GetNextRead(); | |
1074 // The buffer should always be big enough to contain all the MockRead data. To | |
1075 // use small buffers, split the data into multiple MockReads. | |
1076 DCHECK_LE(read_data_.data_len, buf_len); | |
1077 | |
1078 read_buf_ = buf; | |
1079 read_buf_len_ = buf_len; | |
1009 read_callback_ = callback; | 1080 read_callback_ = callback; |
1010 | 1081 |
1011 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { | 1082 if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { |
1012 read_pending_ = true; | 1083 read_pending_ = true; |
1013 DCHECK(read_callback_); | 1084 DCHECK(!read_callback_.is_null()); |
1014 return ERR_IO_PENDING; | 1085 return ERR_IO_PENDING; |
1015 } | 1086 } |
1016 | 1087 |
1017 was_used_to_convey_data_ = true; | 1088 was_used_to_convey_data_ = true; |
1018 return CompleteRead(); | 1089 return CompleteRead(); |
1019 } | 1090 } |
1020 | 1091 |
1021 // TODO(erikchen): Support connect sequencing. | 1092 // TODO(erikchen): Support connect sequencing. |
1022 int DeterministicMockTCPClientSocket::Connect( | 1093 int DeterministicMockTCPClientSocket::Connect( |
1023 net::OldCompletionCallback* callback) { | 1094 net::OldCompletionCallback* callback) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1145 } | 1216 } |
1146 | 1217 |
1147 MockSSLClientSocket::~MockSSLClientSocket() { | 1218 MockSSLClientSocket::~MockSSLClientSocket() { |
1148 Disconnect(); | 1219 Disconnect(); |
1149 } | 1220 } |
1150 | 1221 |
1151 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1222 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
1152 net::OldCompletionCallback* callback) { | 1223 net::OldCompletionCallback* callback) { |
1153 return transport_->socket()->Read(buf, buf_len, callback); | 1224 return transport_->socket()->Read(buf, buf_len, callback); |
1154 } | 1225 } |
1226 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
1227 const net::CompletionCallback& callback) { | |
1228 return transport_->socket()->Read(buf, buf_len, callback); | |
1229 } | |
1155 | 1230 |
1156 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1231 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
1157 net::OldCompletionCallback* callback) { | 1232 net::OldCompletionCallback* callback) { |
1158 return transport_->socket()->Write(buf, buf_len, callback); | 1233 return transport_->socket()->Write(buf, buf_len, callback); |
1159 } | 1234 } |
1160 | 1235 |
1161 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { | 1236 int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { |
1162 OldConnectCallback* connect_callback = new OldConnectCallback( | 1237 OldConnectCallback* connect_callback = new OldConnectCallback( |
1163 this, callback, data_->connect.result); | 1238 this, callback, data_->connect.result); |
1164 int rv = transport_->socket()->Connect(connect_callback); | 1239 int rv = transport_->socket()->Connect(connect_callback); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1256 | 1331 |
1257 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, | 1332 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, |
1258 net::NetLog* net_log) | 1333 net::NetLog* net_log) |
1259 : connected_(false), | 1334 : connected_(false), |
1260 data_(data), | 1335 data_(data), |
1261 read_offset_(0), | 1336 read_offset_(0), |
1262 read_data_(false, net::ERR_UNEXPECTED), | 1337 read_data_(false, net::ERR_UNEXPECTED), |
1263 need_read_data_(true), | 1338 need_read_data_(true), |
1264 pending_buf_(NULL), | 1339 pending_buf_(NULL), |
1265 pending_buf_len_(0), | 1340 pending_buf_len_(0), |
1266 pending_callback_(NULL), | 1341 old_pending_callback_(NULL), |
1267 net_log_(NetLog::Source(), net_log), | 1342 net_log_(NetLog::Source(), net_log), |
1268 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 1343 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
1269 DCHECK(data_); | 1344 DCHECK(data_); |
1270 data_->Reset(); | 1345 data_->Reset(); |
1271 } | 1346 } |
1272 | 1347 |
1273 MockUDPClientSocket::~MockUDPClientSocket() {} | 1348 MockUDPClientSocket::~MockUDPClientSocket() {} |
1274 | 1349 |
1275 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 1350 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
1276 net::OldCompletionCallback* callback) { | 1351 net::OldCompletionCallback* callback) { |
1277 if (!connected_) | 1352 if (!connected_) |
1278 return net::ERR_UNEXPECTED; | 1353 return net::ERR_UNEXPECTED; |
1279 | 1354 |
1280 // If the buffer is already in use, a read is already in progress! | 1355 // If the buffer is already in use, a read is already in progress! |
1281 DCHECK(pending_buf_ == NULL); | 1356 DCHECK(pending_buf_ == NULL); |
1282 | 1357 |
1283 // Store our async IO data. | 1358 // Store our async IO data. |
1284 pending_buf_ = buf; | 1359 pending_buf_ = buf; |
1285 pending_buf_len_ = buf_len; | 1360 pending_buf_len_ = buf_len; |
1286 pending_callback_ = callback; | 1361 old_pending_callback_ = callback; |
1287 | 1362 |
1288 if (need_read_data_) { | 1363 if (need_read_data_) { |
1289 read_data_ = data_->GetNextRead(); | 1364 read_data_ = data_->GetNextRead(); |
1290 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | 1365 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
1291 // to complete the async IO manually later (via OnReadComplete). | 1366 // to complete the async IO manually later (via OnReadComplete). |
1292 if (read_data_.result == ERR_IO_PENDING) { | 1367 if (read_data_.result == ERR_IO_PENDING) { |
1293 DCHECK(callback); // We need to be using async IO in this case. | 1368 DCHECK(callback); // We need to be using async IO in this case. |
1294 return ERR_IO_PENDING; | 1369 return ERR_IO_PENDING; |
1295 } | 1370 } |
1296 need_read_data_ = false; | 1371 need_read_data_ = false; |
1297 } | 1372 } |
1298 | 1373 |
1299 return CompleteRead(); | 1374 return CompleteRead(); |
1300 } | 1375 } |
1376 int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
1377 const net::CompletionCallback& callback) { | |
1378 if (!connected_) | |
1379 return net::ERR_UNEXPECTED; | |
1380 | |
1381 // If the buffer is already in use, a read is already in progress! | |
1382 DCHECK(pending_buf_ == NULL); | |
1383 | |
1384 // Store our async IO data. | |
1385 pending_buf_ = buf; | |
1386 pending_buf_len_ = buf_len; | |
1387 pending_callback_ = callback; | |
1388 | |
1389 if (need_read_data_) { | |
1390 read_data_ = data_->GetNextRead(); | |
1391 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | |
1392 // to complete the async IO manually later (via OnReadComplete). | |
1393 if (read_data_.result == ERR_IO_PENDING) { | |
1394 // We need to be using async IO in this case. | |
1395 DCHECK(!callback.is_null()); | |
1396 return ERR_IO_PENDING; | |
1397 } | |
1398 need_read_data_ = false; | |
1399 } | |
1400 | |
1401 return CompleteRead(); | |
1402 } | |
1301 | 1403 |
1302 int MockUDPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 1404 int MockUDPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
1303 net::OldCompletionCallback* callback) { | 1405 net::OldCompletionCallback* callback) { |
1304 DCHECK(buf); | 1406 DCHECK(buf); |
1305 DCHECK_GT(buf_len, 0); | 1407 DCHECK_GT(buf_len, 0); |
1306 | 1408 |
1307 if (!connected_) | 1409 if (!connected_) |
1308 return ERR_UNEXPECTED; | 1410 return ERR_UNEXPECTED; |
1309 | 1411 |
1310 std::string data(buf->data(), buf_len); | 1412 std::string data(buf->data(), buf_len); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1356 // Since we've been waiting for data, need_read_data_ should be true. | 1458 // Since we've been waiting for data, need_read_data_ should be true. |
1357 DCHECK(need_read_data_); | 1459 DCHECK(need_read_data_); |
1358 | 1460 |
1359 read_data_ = data; | 1461 read_data_ = data; |
1360 need_read_data_ = false; | 1462 need_read_data_ = false; |
1361 | 1463 |
1362 // The caller is simulating that this IO completes right now. Don't | 1464 // The caller is simulating that this IO completes right now. Don't |
1363 // let CompleteRead() schedule a callback. | 1465 // let CompleteRead() schedule a callback. |
1364 read_data_.async = false; | 1466 read_data_.async = false; |
1365 | 1467 |
1366 net::OldCompletionCallback* callback = pending_callback_; | 1468 if (old_pending_callback_) { |
1367 int rv = CompleteRead(); | 1469 net::OldCompletionCallback* callback = old_pending_callback_; |
1368 RunCallback(callback, rv); | 1470 int rv = CompleteRead(); |
1471 RunOldCallback(callback, rv); | |
1472 } else { | |
1473 net::CompletionCallback callback = pending_callback_; | |
1474 int rv = CompleteRead(); | |
1475 RunCallback(callback, rv); | |
1476 } | |
1369 } | 1477 } |
1370 | 1478 |
1371 int MockUDPClientSocket::CompleteRead() { | 1479 int MockUDPClientSocket::CompleteRead() { |
1372 DCHECK(pending_buf_); | 1480 DCHECK(pending_buf_); |
1373 DCHECK(pending_buf_len_ > 0); | 1481 DCHECK(pending_buf_len_ > 0); |
1374 | 1482 |
1375 // Save the pending async IO data and reset our |pending_| state. | 1483 // Save the pending async IO data and reset our |pending_| state. |
1376 net::IOBuffer* buf = pending_buf_; | 1484 net::IOBuffer* buf = pending_buf_; |
1377 int buf_len = pending_buf_len_; | 1485 int buf_len = pending_buf_len_; |
1378 net::OldCompletionCallback* callback = pending_callback_; | 1486 net::OldCompletionCallback* old_callback = old_pending_callback_; |
1487 net::CompletionCallback callback = pending_callback_; | |
1379 pending_buf_ = NULL; | 1488 pending_buf_ = NULL; |
1380 pending_buf_len_ = 0; | 1489 pending_buf_len_ = 0; |
1381 pending_callback_ = NULL; | 1490 old_pending_callback_ = NULL; |
1491 pending_callback_.Reset(); | |
1492 pending_callback_.Reset(); | |
csilv
2011/12/07 00:03:48
kill this line
| |
1382 | 1493 |
1383 int result = read_data_.result; | 1494 int result = read_data_.result; |
1384 DCHECK(result != ERR_IO_PENDING); | 1495 DCHECK(result != ERR_IO_PENDING); |
1385 | 1496 |
1386 if (read_data_.data) { | 1497 if (read_data_.data) { |
1387 if (read_data_.data_len - read_offset_ > 0) { | 1498 if (read_data_.data_len - read_offset_ > 0) { |
1388 result = std::min(buf_len, read_data_.data_len - read_offset_); | 1499 result = std::min(buf_len, read_data_.data_len - read_offset_); |
1389 memcpy(buf->data(), read_data_.data + read_offset_, result); | 1500 memcpy(buf->data(), read_data_.data + read_offset_, result); |
1390 read_offset_ += result; | 1501 read_offset_ += result; |
1391 if (read_offset_ == read_data_.data_len) { | 1502 if (read_offset_ == read_data_.data_len) { |
1392 need_read_data_ = true; | 1503 need_read_data_ = true; |
1393 read_offset_ = 0; | 1504 read_offset_ = 0; |
1394 } | 1505 } |
1395 } else { | 1506 } else { |
1396 result = 0; // EOF | 1507 result = 0; // EOF |
1397 } | 1508 } |
1398 } | 1509 } |
1399 | 1510 |
1400 if (read_data_.async) { | 1511 if (read_data_.async) { |
1401 DCHECK(callback); | 1512 DCHECK(old_callback || !callback.is_null()); |
1402 RunCallbackAsync(callback, result); | 1513 if (old_callback) |
1514 RunCallbackAsync(old_callback, result); | |
1515 else | |
1516 RunCallbackAsync(callback, result); | |
1403 return net::ERR_IO_PENDING; | 1517 return net::ERR_IO_PENDING; |
1404 } | 1518 } |
1405 return result; | 1519 return result; |
1406 } | 1520 } |
1407 | 1521 |
1408 void MockUDPClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, | 1522 void MockUDPClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, |
1409 int result) { | 1523 int result) { |
1410 MessageLoop::current()->PostTask(FROM_HERE, | 1524 MessageLoop::current()->PostTask(FROM_HERE, |
1525 base::Bind(&MockUDPClientSocket::RunOldCallback, | |
1526 weak_factory_.GetWeakPtr(), callback, result)); | |
1527 } | |
1528 void MockUDPClientSocket::RunCallbackAsync( | |
1529 const net::CompletionCallback& callback, int result) { | |
1530 MessageLoop::current()->PostTask(FROM_HERE, | |
1411 base::Bind(&MockUDPClientSocket::RunCallback, weak_factory_.GetWeakPtr(), | 1531 base::Bind(&MockUDPClientSocket::RunCallback, weak_factory_.GetWeakPtr(), |
1412 callback, result)); | 1532 callback, result)); |
1413 } | 1533 } |
1414 | 1534 |
1415 void MockUDPClientSocket::RunCallback(net::OldCompletionCallback* callback, | 1535 void MockUDPClientSocket::RunOldCallback(net::OldCompletionCallback* callback, |
1416 int result) { | 1536 int result) { |
1417 if (callback) | 1537 if (callback) |
1418 callback->Run(result); | 1538 callback->Run(result); |
1419 } | 1539 } |
1540 void MockUDPClientSocket::RunCallback(const net::CompletionCallback& callback, | |
1541 int result) { | |
1542 if (!callback.is_null()) | |
1543 callback.Run(result); | |
1544 } | |
1420 | 1545 |
1421 TestSocketRequest::TestSocketRequest( | 1546 TestSocketRequest::TestSocketRequest( |
1422 std::vector<TestSocketRequest*>* request_order, | 1547 std::vector<TestSocketRequest*>* request_order, |
1423 size_t* completion_count) | 1548 size_t* completion_count) |
1424 : request_order_(request_order), | 1549 : request_order_(request_order), |
1425 completion_count_(completion_count) { | 1550 completion_count_(completion_count) { |
1426 DCHECK(request_order); | 1551 DCHECK(request_order); |
1427 DCHECK(completion_count); | 1552 DCHECK(completion_count); |
1428 } | 1553 } |
1429 | 1554 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1686 | 1811 |
1687 const char kSOCKS5OkRequest[] = | 1812 const char kSOCKS5OkRequest[] = |
1688 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1813 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1689 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1814 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1690 | 1815 |
1691 const char kSOCKS5OkResponse[] = | 1816 const char kSOCKS5OkResponse[] = |
1692 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1817 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1693 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1818 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1694 | 1819 |
1695 } // namespace net | 1820 } // namespace net |
OLD | NEW |