OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_P2P_BASE_FAKEPACKETTRANSPORT_H_ |
| 12 #define WEBRTC_P2P_BASE_FAKEPACKETTRANSPORT_H_ |
| 13 |
| 14 #include <string> |
| 15 |
| 16 #include "webrtc/base/asyncinvoker.h" |
| 17 #include "webrtc/base/copyonwritebuffer.h" |
| 18 #include "webrtc/p2p/base/packettransportinterface.h" |
| 19 |
| 20 namespace rtc { |
| 21 |
| 22 // Used to simulate a packet-based transport. |
| 23 class FakePacketTransport : public PacketTransportInterface { |
| 24 public: |
| 25 explicit FakePacketTransport(const std::string& debug_name) |
| 26 : debug_name_(debug_name) {} |
| 27 ~FakePacketTransport() override { |
| 28 if (dest_ && dest_->dest_ == this) { |
| 29 dest_->dest_ = nullptr; |
| 30 } |
| 31 } |
| 32 |
| 33 // If async, will send packets by "Post"-ing to message queue instead of |
| 34 // synchronously "Send"-ing. |
| 35 void SetAsync(bool async) { async_ = async; } |
| 36 void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; } |
| 37 |
| 38 // SetWritable, SetReceiving and SetDestination are the main methods that can |
| 39 // be used for testing, to simulate connectivity or lack thereof. |
| 40 void SetWritable(bool writable) { set_writable(writable); } |
| 41 void SetReceiving(bool receiving) { set_receiving(receiving); } |
| 42 |
| 43 // Simulates the two transports connecting to each other. |
| 44 // If |asymmetric| is true this method only affects this FakePacketTransport. |
| 45 // If false, it affects |dest| as well. |
| 46 void SetDestination(FakePacketTransport* dest, bool asymmetric) { |
| 47 if (dest) { |
| 48 dest_ = dest; |
| 49 set_writable(true); |
| 50 if (!asymmetric) { |
| 51 dest->SetDestination(this, true); |
| 52 } |
| 53 } else { |
| 54 // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
| 55 dest_ = nullptr; |
| 56 set_writable(false); |
| 57 } |
| 58 } |
| 59 |
| 60 // Fake PacketTransportInterface implementation. |
| 61 std::string debug_name() const override { return debug_name_; } |
| 62 bool writable() const override { return writable_; } |
| 63 bool receiving() const override { return receiving_; } |
| 64 int SendPacket(const char* data, |
| 65 size_t len, |
| 66 const PacketOptions& options, |
| 67 int flags) override { |
| 68 if (!dest_) { |
| 69 return -1; |
| 70 } |
| 71 CopyOnWriteBuffer packet(data, len); |
| 72 if (async_) { |
| 73 invoker_.AsyncInvokeDelayed<void>( |
| 74 RTC_FROM_HERE, Thread::Current(), |
| 75 Bind(&FakePacketTransport::SendPacketInternal, this, packet), |
| 76 async_delay_ms_); |
| 77 } else { |
| 78 SendPacketInternal(packet); |
| 79 } |
| 80 SentPacket sent_packet(options.packet_id, TimeMillis()); |
| 81 SignalSentPacket(this, sent_packet); |
| 82 return static_cast<int>(len); |
| 83 } |
| 84 int SetOption(Socket::Option opt, int value) override { return true; } |
| 85 bool GetOption(Socket::Option opt, int* value) override { return true; } |
| 86 int GetError() override { return 0; } |
| 87 |
| 88 private: |
| 89 void set_writable(bool writable) { |
| 90 if (writable_ == writable) { |
| 91 return; |
| 92 } |
| 93 writable_ = writable; |
| 94 if (writable_) { |
| 95 SignalReadyToSend(this); |
| 96 } |
| 97 SignalWritableState(this); |
| 98 } |
| 99 |
| 100 void set_receiving(bool receiving) { |
| 101 if (receiving_ == receiving) { |
| 102 return; |
| 103 } |
| 104 receiving_ = receiving; |
| 105 SignalReceivingState(this); |
| 106 } |
| 107 |
| 108 void SendPacketInternal(const CopyOnWriteBuffer& packet) { |
| 109 if (dest_) { |
| 110 dest_->SignalReadPacket(dest_, packet.data<char>(), packet.size(), |
| 111 CreatePacketTime(0), 0); |
| 112 } |
| 113 } |
| 114 |
| 115 AsyncInvoker invoker_; |
| 116 std::string debug_name_; |
| 117 FakePacketTransport* dest_ = nullptr; |
| 118 bool async_ = false; |
| 119 int async_delay_ms_ = 0; |
| 120 bool writable_ = false; |
| 121 bool receiving_ = false; |
| 122 }; |
| 123 |
| 124 } // namespace rtc |
| 125 |
| 126 #endif // WEBRTC_P2P_BASE_FAKEPACKETTRANSPORT_H_ |
OLD | NEW |