| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "webrtc/p2p/base/pseudotcp.h" | 14 #include "webrtc/p2p/base/pseudotcp.h" |
| 15 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
| 16 #include "webrtc/base/helpers.h" | 16 #include "webrtc/base/helpers.h" |
| 17 #include "webrtc/base/messagehandler.h" | 17 #include "webrtc/base/messagehandler.h" |
| 18 #include "webrtc/base/stream.h" | 18 #include "webrtc/base/stream.h" |
| 19 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
| 20 #include "webrtc/base/timeutils.h" | 20 #include "webrtc/base/timeutils.h" |
| 21 | 21 |
| 22 using cricket::PseudoTcp; | 22 using cricket::PseudoTcp; |
| 23 | 23 |
| 24 static const int kConnectTimeoutMs = 10000; // ~3 * default RTO of 3000ms | 24 static const int kConnectTimeoutMs = 10000; // ~3 * default RTO of 3000ms |
| 25 static const int kTransferTimeoutMs = 15000; | 25 static const int kTransferTimeoutMs = 15000; |
| 26 static const int kBlockSize = 4096; | 26 static const int kBlockSize = 4096; |
| 27 | 27 |
| 28 class PseudoTcpForTest : public cricket::PseudoTcp { | 28 class PseudoTcpForTest : public cricket::PseudoTcp { |
| 29 public: | 29 public: |
| 30 PseudoTcpForTest(cricket::IPseudoTcpNotify* notify, uint32 conv) | 30 PseudoTcpForTest(cricket::IPseudoTcpNotify* notify, uint32_t conv) |
| 31 : PseudoTcp(notify, conv) { | 31 : PseudoTcp(notify, conv) {} |
| 32 } | |
| 33 | 32 |
| 34 bool isReceiveBufferFull() const { | 33 bool isReceiveBufferFull() const { |
| 35 return PseudoTcp::isReceiveBufferFull(); | 34 return PseudoTcp::isReceiveBufferFull(); |
| 36 } | 35 } |
| 37 | 36 |
| 38 void disableWindowScale() { | 37 void disableWindowScale() { |
| 39 PseudoTcp::disableWindowScale(); | 38 PseudoTcp::disableWindowScale(); |
| 40 } | 39 } |
| 41 }; | 40 }; |
| 42 | 41 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 LOG(LS_VERBOSE) << "Opened"; | 119 LOG(LS_VERBOSE) << "Opened"; |
| 121 if (tcp == &local_) { | 120 if (tcp == &local_) { |
| 122 have_connected_ = true; | 121 have_connected_ = true; |
| 123 OnTcpWriteable(tcp); | 122 OnTcpWriteable(tcp); |
| 124 } | 123 } |
| 125 } | 124 } |
| 126 // Test derived from the base should override | 125 // Test derived from the base should override |
| 127 // virtual void OnTcpReadable(PseudoTcp* tcp) | 126 // virtual void OnTcpReadable(PseudoTcp* tcp) |
| 128 // and | 127 // and |
| 129 // virtual void OnTcpWritable(PseudoTcp* tcp) | 128 // virtual void OnTcpWritable(PseudoTcp* tcp) |
| 130 virtual void OnTcpClosed(PseudoTcp* tcp, uint32 error) { | 129 virtual void OnTcpClosed(PseudoTcp* tcp, uint32_t error) { |
| 131 // Consider ourselves closed when the remote side gets OnTcpClosed. | 130 // Consider ourselves closed when the remote side gets OnTcpClosed. |
| 132 // TODO: OnTcpClosed is only ever notified in case of error in | 131 // TODO: OnTcpClosed is only ever notified in case of error in |
| 133 // the current implementation. Solicited close is not (yet) supported. | 132 // the current implementation. Solicited close is not (yet) supported. |
| 134 LOG(LS_VERBOSE) << "Closed"; | 133 LOG(LS_VERBOSE) << "Closed"; |
| 135 EXPECT_EQ(0U, error); | 134 EXPECT_EQ(0U, error); |
| 136 if (tcp == &remote_) { | 135 if (tcp == &remote_) { |
| 137 have_disconnected_ = true; | 136 have_disconnected_ = true; |
| 138 } | 137 } |
| 139 } | 138 } |
| 140 virtual WriteResult TcpWritePacket(PseudoTcp* tcp, | 139 virtual WriteResult TcpWritePacket(PseudoTcp* tcp, |
| 141 const char* buffer, size_t len) { | 140 const char* buffer, size_t len) { |
| 142 // Randomly drop the desired percentage of packets. | 141 // Randomly drop the desired percentage of packets. |
| 143 // Also drop packets that are larger than the configured MTU. | 142 // Also drop packets that are larger than the configured MTU. |
| 144 if (rtc::CreateRandomId() % 100 < static_cast<uint32>(loss_)) { | 143 if (rtc::CreateRandomId() % 100 < static_cast<uint32_t>(loss_)) { |
| 145 LOG(LS_VERBOSE) << "Randomly dropping packet, size=" << len; | 144 LOG(LS_VERBOSE) << "Randomly dropping packet, size=" << len; |
| 146 } else if (len > static_cast<size_t>(std::min(local_mtu_, remote_mtu_))) { | 145 } else if (len > static_cast<size_t>(std::min(local_mtu_, remote_mtu_))) { |
| 147 LOG(LS_VERBOSE) << "Dropping packet that exceeds path MTU, size=" << len; | 146 LOG(LS_VERBOSE) << "Dropping packet that exceeds path MTU, size=" << len; |
| 148 } else { | 147 } else { |
| 149 int id = (tcp == &local_) ? MSG_RPACKET : MSG_LPACKET; | 148 int id = (tcp == &local_) ? MSG_RPACKET : MSG_LPACKET; |
| 150 std::string packet(buffer, len); | 149 std::string packet(buffer, len); |
| 151 rtc::Thread::Current()->PostDelayed(delay_, this, id, | 150 rtc::Thread::Current()->PostDelayed(delay_, this, id, |
| 152 rtc::WrapMessageData(packet)); | 151 rtc::WrapMessageData(packet)); |
| 153 } | 152 } |
| 154 return WR_SUCCESS; | 153 return WR_SUCCESS; |
| 155 } | 154 } |
| 156 | 155 |
| 157 void UpdateLocalClock() { UpdateClock(&local_, MSG_LCLOCK); } | 156 void UpdateLocalClock() { UpdateClock(&local_, MSG_LCLOCK); } |
| 158 void UpdateRemoteClock() { UpdateClock(&remote_, MSG_RCLOCK); } | 157 void UpdateRemoteClock() { UpdateClock(&remote_, MSG_RCLOCK); } |
| 159 void UpdateClock(PseudoTcp* tcp, uint32 message) { | 158 void UpdateClock(PseudoTcp* tcp, uint32_t message) { |
| 160 long interval = 0; // NOLINT | 159 long interval = 0; // NOLINT |
| 161 tcp->GetNextClock(PseudoTcp::Now(), interval); | 160 tcp->GetNextClock(PseudoTcp::Now(), interval); |
| 162 interval = std::max<int>(interval, 0L); // sometimes interval is < 0 | 161 interval = std::max<int>(interval, 0L); // sometimes interval is < 0 |
| 163 rtc::Thread::Current()->Clear(this, message); | 162 rtc::Thread::Current()->Clear(this, message); |
| 164 rtc::Thread::Current()->PostDelayed(interval, this, message); | 163 rtc::Thread::Current()->PostDelayed(interval, this, message); |
| 165 } | 164 } |
| 166 | 165 |
| 167 virtual void OnMessage(rtc::Message* message) { | 166 virtual void OnMessage(rtc::Message* message) { |
| 168 switch (message->message_id) { | 167 switch (message->message_id) { |
| 169 case MSG_LPACKET: { | 168 case MSG_LPACKET: { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 bool have_disconnected_; | 201 bool have_disconnected_; |
| 203 int local_mtu_; | 202 int local_mtu_; |
| 204 int remote_mtu_; | 203 int remote_mtu_; |
| 205 int delay_; | 204 int delay_; |
| 206 int loss_; | 205 int loss_; |
| 207 }; | 206 }; |
| 208 | 207 |
| 209 class PseudoTcpTest : public PseudoTcpTestBase { | 208 class PseudoTcpTest : public PseudoTcpTestBase { |
| 210 public: | 209 public: |
| 211 void TestTransfer(int size) { | 210 void TestTransfer(int size) { |
| 212 uint32 start, elapsed; | 211 uint32_t start, elapsed; |
| 213 size_t received; | 212 size_t received; |
| 214 // Create some dummy data to send. | 213 // Create some dummy data to send. |
| 215 send_stream_.ReserveSize(size); | 214 send_stream_.ReserveSize(size); |
| 216 for (int i = 0; i < size; ++i) { | 215 for (int i = 0; i < size; ++i) { |
| 217 char ch = static_cast<char>(i); | 216 char ch = static_cast<char>(i); |
| 218 send_stream_.Write(&ch, 1, NULL, NULL); | 217 send_stream_.Write(&ch, 1, NULL, NULL); |
| 219 } | 218 } |
| 220 send_stream_.Rewind(); | 219 send_stream_.Rewind(); |
| 221 // Prepare the receive stream. | 220 // Prepare the receive stream. |
| 222 recv_stream_.ReserveSize(size); | 221 recv_stream_.ReserveSize(size); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 PseudoTcpTestPingPong() | 318 PseudoTcpTestPingPong() |
| 320 : iterations_remaining_(0), | 319 : iterations_remaining_(0), |
| 321 sender_(NULL), | 320 sender_(NULL), |
| 322 receiver_(NULL), | 321 receiver_(NULL), |
| 323 bytes_per_send_(0) { | 322 bytes_per_send_(0) { |
| 324 } | 323 } |
| 325 void SetBytesPerSend(int bytes) { | 324 void SetBytesPerSend(int bytes) { |
| 326 bytes_per_send_ = bytes; | 325 bytes_per_send_ = bytes; |
| 327 } | 326 } |
| 328 void TestPingPong(int size, int iterations) { | 327 void TestPingPong(int size, int iterations) { |
| 329 uint32 start, elapsed; | 328 uint32_t start, elapsed; |
| 330 iterations_remaining_ = iterations; | 329 iterations_remaining_ = iterations; |
| 331 receiver_ = &remote_; | 330 receiver_ = &remote_; |
| 332 sender_ = &local_; | 331 sender_ = &local_; |
| 333 // Create some dummy data to send. | 332 // Create some dummy data to send. |
| 334 send_stream_.ReserveSize(size); | 333 send_stream_.ReserveSize(size); |
| 335 for (int i = 0; i < size; ++i) { | 334 for (int i = 0; i < size; ++i) { |
| 336 char ch = static_cast<char>(i); | 335 char ch = static_cast<char>(i); |
| 337 send_stream_.Write(&ch, 1, NULL, NULL); | 336 send_stream_.Write(&ch, 1, NULL, NULL); |
| 338 } | 337 } |
| 339 send_stream_.Rewind(); | 338 send_stream_.Rewind(); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 switch (message_id) { | 481 switch (message_id) { |
| 483 case MSG_WRITE: { | 482 case MSG_WRITE: { |
| 484 WriteData(); | 483 WriteData(); |
| 485 break; | 484 break; |
| 486 } | 485 } |
| 487 default: | 486 default: |
| 488 break; | 487 break; |
| 489 } | 488 } |
| 490 } | 489 } |
| 491 | 490 |
| 492 uint32 EstimateReceiveWindowSize() const { | 491 uint32_t EstimateReceiveWindowSize() const { |
| 493 return static_cast<uint32>(recv_position_[0]); | 492 return static_cast<uint32_t>(recv_position_[0]); |
| 494 } | 493 } |
| 495 | 494 |
| 496 uint32 EstimateSendWindowSize() const { | 495 uint32_t EstimateSendWindowSize() const { |
| 497 return static_cast<uint32>(send_position_[0] - recv_position_[0]); | 496 return static_cast<uint32_t>(send_position_[0] - recv_position_[0]); |
| 498 } | 497 } |
| 499 | 498 |
| 500 private: | 499 private: |
| 501 // IPseudoTcpNotify interface | 500 // IPseudoTcpNotify interface |
| 502 virtual void OnTcpReadable(PseudoTcp* tcp) { | 501 virtual void OnTcpReadable(PseudoTcp* tcp) { |
| 503 } | 502 } |
| 504 | 503 |
| 505 virtual void OnTcpWriteable(PseudoTcp* tcp) { | 504 virtual void OnTcpWriteable(PseudoTcp* tcp) { |
| 506 } | 505 } |
| 507 | 506 |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 /* Test sending data with mismatched MTUs. We should detect this and reduce | 831 /* Test sending data with mismatched MTUs. We should detect this and reduce |
| 833 // our packet size accordingly. | 832 // our packet size accordingly. |
| 834 // TODO: This doesn't actually work right now. The current code | 833 // TODO: This doesn't actually work right now. The current code |
| 835 // doesn't detect if the MTU is set too high on either side. | 834 // doesn't detect if the MTU is set too high on either side. |
| 836 TEST_F(PseudoTcpTest, TestSendWithMismatchedMtus) { | 835 TEST_F(PseudoTcpTest, TestSendWithMismatchedMtus) { |
| 837 SetLocalMtu(1500); | 836 SetLocalMtu(1500); |
| 838 SetRemoteMtu(1280); | 837 SetRemoteMtu(1280); |
| 839 TestTransfer(1000000); | 838 TestTransfer(1000000); |
| 840 } | 839 } |
| 841 */ | 840 */ |
| OLD | NEW |