fake_packet_transport.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef P2P_BASE_FAKE_PACKET_TRANSPORT_H_
  11. #define P2P_BASE_FAKE_PACKET_TRANSPORT_H_
  12. #include <map>
  13. #include <string>
  14. #include "p2p/base/packet_transport_internal.h"
  15. #include "rtc_base/async_invoker.h"
  16. #include "rtc_base/copy_on_write_buffer.h"
  17. namespace rtc {
  18. // Used to simulate a packet-based transport.
  19. class FakePacketTransport : public PacketTransportInternal {
  20. public:
  21. explicit FakePacketTransport(const std::string& transport_name)
  22. : transport_name_(transport_name) {}
  23. ~FakePacketTransport() override {
  24. if (dest_ && dest_->dest_ == this) {
  25. dest_->dest_ = nullptr;
  26. }
  27. }
  28. // If async, will send packets by "Post"-ing to message queue instead of
  29. // synchronously "Send"-ing.
  30. void SetAsync(bool async) { async_ = async; }
  31. void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; }
  32. // SetWritable, SetReceiving and SetDestination are the main methods that can
  33. // be used for testing, to simulate connectivity or lack thereof.
  34. void SetWritable(bool writable) { set_writable(writable); }
  35. void SetReceiving(bool receiving) { set_receiving(receiving); }
  36. // Simulates the two transports connecting to each other.
  37. // If |asymmetric| is true this method only affects this FakePacketTransport.
  38. // If false, it affects |dest| as well.
  39. void SetDestination(FakePacketTransport* dest, bool asymmetric) {
  40. if (dest) {
  41. dest_ = dest;
  42. set_writable(true);
  43. if (!asymmetric) {
  44. dest->SetDestination(this, true);
  45. }
  46. } else {
  47. // Simulates loss of connectivity, by asymmetrically forgetting dest_.
  48. dest_ = nullptr;
  49. set_writable(false);
  50. }
  51. }
  52. // Fake PacketTransportInternal implementation.
  53. const std::string& transport_name() const override { return transport_name_; }
  54. bool writable() const override { return writable_; }
  55. bool receiving() const override { return receiving_; }
  56. int SendPacket(const char* data,
  57. size_t len,
  58. const PacketOptions& options,
  59. int flags) override {
  60. if (!dest_) {
  61. return -1;
  62. }
  63. CopyOnWriteBuffer packet(data, len);
  64. if (async_) {
  65. invoker_.AsyncInvokeDelayed<void>(
  66. RTC_FROM_HERE, Thread::Current(),
  67. Bind(&FakePacketTransport::SendPacketInternal, this, packet),
  68. async_delay_ms_);
  69. } else {
  70. SendPacketInternal(packet);
  71. }
  72. SentPacket sent_packet(options.packet_id, TimeMillis());
  73. SignalSentPacket(this, sent_packet);
  74. return static_cast<int>(len);
  75. }
  76. int SetOption(Socket::Option opt, int value) override {
  77. options_[opt] = value;
  78. return 0;
  79. }
  80. bool GetOption(Socket::Option opt, int* value) override {
  81. auto it = options_.find(opt);
  82. if (it == options_.end()) {
  83. return false;
  84. }
  85. *value = it->second;
  86. return true;
  87. }
  88. int GetError() override { return error_; }
  89. void SetError(int error) { error_ = error; }
  90. const CopyOnWriteBuffer* last_sent_packet() { return &last_sent_packet_; }
  91. absl::optional<NetworkRoute> network_route() const override {
  92. return network_route_;
  93. }
  94. void SetNetworkRoute(absl::optional<NetworkRoute> network_route) {
  95. network_route_ = network_route;
  96. SignalNetworkRouteChanged(network_route);
  97. }
  98. private:
  99. void set_writable(bool writable) {
  100. if (writable_ == writable) {
  101. return;
  102. }
  103. writable_ = writable;
  104. if (writable_) {
  105. SignalReadyToSend(this);
  106. }
  107. SignalWritableState(this);
  108. }
  109. void set_receiving(bool receiving) {
  110. if (receiving_ == receiving) {
  111. return;
  112. }
  113. receiving_ = receiving;
  114. SignalReceivingState(this);
  115. }
  116. void SendPacketInternal(const CopyOnWriteBuffer& packet) {
  117. last_sent_packet_ = packet;
  118. if (dest_) {
  119. dest_->SignalReadPacket(dest_, packet.data<char>(), packet.size(),
  120. TimeMicros(), 0);
  121. }
  122. }
  123. CopyOnWriteBuffer last_sent_packet_;
  124. AsyncInvoker invoker_;
  125. std::string transport_name_;
  126. FakePacketTransport* dest_ = nullptr;
  127. bool async_ = false;
  128. int async_delay_ms_ = 0;
  129. bool writable_ = false;
  130. bool receiving_ = false;
  131. std::map<Socket::Option, int> options_;
  132. int error_ = 0;
  133. absl::optional<NetworkRoute> network_route_;
  134. };
  135. } // namespace rtc
  136. #endif // P2P_BASE_FAKE_PACKET_TRANSPORT_H_