| 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 | #include "rtc_base/test_client.h" |
| 12 |
| 13 | #include <string.h> |
| 14 |
| 15 | #include <memory> |
| 16 | #include <utility> |
| 17 |
| 18 | #include "api/units/timestamp.h" |
| 19 | #include "rtc_base/gunit.h" |
| 20 | #include "rtc_base/network/received_packet.h" |
| 21 | #include "rtc_base/thread.h" |
| 22 | #include "rtc_base/time_utils.h" |
| 23 |
| 24 | namespace rtc { |
| 25 |
| 26 | // DESIGN: Each packet received is put it into a list of packets. |
| 27 | // Callers can retrieve received packets from any thread by calling |
| 28 | // NextPacket. |
| 29 |
| 30 | TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket) |
| 31 | : TestClient(std::move(socket), nullptr) {} |
| 32 |
| 33 | TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket, |
| 34 | ThreadProcessingFakeClock* fake_clock) |
| 35 | : fake_clock_(fake_clock), socket_(std::move(socket)) { |
| 36 | socket_->RegisterReceivedPacketCallback( |
| 37 | [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { |
| 38 | OnPacket(socket, packet); |
| 39 | }); |
| 40 | socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); |
| 41 | } |
| 42 |
| 43 | TestClient::~TestClient() {} |
| 44 |
| 45 | bool TestClient::CheckConnState(AsyncPacketSocket::State state) { |
| 46 | // Wait for our timeout value until the socket reaches the desired state. |
| 47 | int64_t end = TimeAfter(kTimeoutMs); |
| 48 | while (socket_->GetState() != state && TimeUntil(end) > 0) { |
| 49 | AdvanceTime(1); |
| 50 | } |
| 51 | return (socket_->GetState() == state); |
| 52 | } |
| 53 |
| 54 | int TestClient::Send(const char* buf, size_t size) { |
| 55 | rtc::PacketOptions options; |
| 56 | return socket_->Send(buf, size, options); |
| 57 | } |
| 58 |
| 59 | int TestClient::SendTo(const char* buf, |
| 60 | size_t size, |
| 61 | const SocketAddress& dest) { |
| 62 | rtc::PacketOptions options; |
| 63 | return socket_->SendTo(buf, size, dest, options); |
| 64 | } |
| 65 |
| 66 | std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) { |
| 67 | // If no packets are currently available, we go into a get/dispatch loop for |
| 68 | // at most timeout_ms. If, during the loop, a packet arrives, then we can |
| 69 | // stop early and return it. |
| 70 |
| 71 | // Note that the case where no packet arrives is important. We often want to |
| 72 | // test that a packet does not arrive. |
| 73 |
| 74 | // Note also that we only try to pump our current thread's message queue. |
| 75 | // Pumping another thread's queue could lead to messages being dispatched from |
| 76 | // the wrong thread to non-thread-safe objects. |
| 77 |
| 78 | int64_t end = TimeAfter(timeout_ms); |
| 79 | while (TimeUntil(end) > 0) { |
| 80 | { |
| 81 | webrtc::MutexLock lock(&mutex_); |
| 82 | if (packets_.size() != 0) { |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | AdvanceTime(1); |
| 87 | } |
| 88 |
| 89 | // Return the first packet placed in the queue. |
| 90 | std::unique_ptr<Packet> packet; |
| 91 | webrtc::MutexLock lock(&mutex_); |
| 92 | if (packets_.size() > 0) { |
| 93 | packet = std::move(packets_.front()); |
| 94 | packets_.erase(packets_.begin()); |
| 95 | } |
| 96 |
| 97 | return packet; |
| 98 | } |
| 99 |
| 100 | bool TestClient::CheckNextPacket(const char* buf, |
| 101 | size_t size, |
| 102 | SocketAddress* addr) { |
| 103 | bool res = false; |
| 104 | std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs); |
| 105 | if (packet) { |
| 106 | res = (packet->buf.size() == size && |
| 107 | memcmp(packet->buf.data(), buf, size) == 0 && |
| 108 | CheckTimestamp(packet->packet_time)); |
| 109 | if (addr) |
| 110 | *addr = packet->addr; |
| 111 | } |
| 112 | return res; |
| 113 | } |
| 114 |
| 115 | bool TestClient::CheckTimestamp( |
| 116 | absl::optional<webrtc::Timestamp> packet_timestamp) { |
| 117 | bool res = true; |
| 118 | if (!packet_timestamp) { |
| 119 | res = false; |
| 120 | } |
| 121 | if (prev_packet_timestamp_) { |
| 122 | if (packet_timestamp < prev_packet_timestamp_) { |
| 123 | res = false; |
| 124 | } |
| 125 | } |
| 126 | prev_packet_timestamp_ = packet_timestamp; |
| 127 | return res; |
| 128 | } |
| 129 |
| 130 | void TestClient::AdvanceTime(int ms) { |
| 131 | // If the test is using a fake clock, we must advance the fake clock to |
| 132 | // advance time. Otherwise, ProcessMessages will work. |
| 133 | if (fake_clock_) { |
| 134 | SIMULATED_WAIT(false, ms, *fake_clock_); |
| 135 | } else { |
| 136 | Thread::Current()->ProcessMessages(1); |
| 137 | } |
| 138 | } |
| 139 |
| 140 | bool TestClient::CheckNoPacket() { |
| 141 | return NextPacket(kNoPacketTimeoutMs) == nullptr; |
| 142 | } |
| 143 |
| 144 | int TestClient::GetError() { |
| 145 | return socket_->GetError(); |
| 146 | } |
| 147 |
| 148 | int TestClient::SetOption(Socket::Option opt, int value) { |
| 149 | return socket_->SetOption(opt, value); |
| 150 | } |
| 151 |
| 152 | void TestClient::OnPacket(AsyncPacketSocket* socket, |
| 153 | const rtc::ReceivedPacket& received_packet) { |
| 154 | webrtc::MutexLock lock(&mutex_); |
| 155 | packets_.push_back(std::make_unique<Packet>(received_packet)); |
| 156 | } |
| 157 |
| 158 | void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { |
| 159 | ++ready_to_send_count_; |
| 160 | } |
| 161 |
| 162 | TestClient::Packet::Packet(const rtc::ReceivedPacket& received_packet) |
| 163 | : addr(received_packet.source_address()), |
| 164 | // Copy received_packet payload to a buffer owned by Packet. |
| 165 | buf(received_packet.payload().data(), received_packet.payload().size()), |
| 166 | packet_time(received_packet.arrival_time()) {} |
| 167 |
| 168 | TestClient::Packet::Packet(const Packet& p) |
| 169 | : addr(p.addr), |
| 170 | buf(p.buf.data(), p.buf.size()), |
| 171 | packet_time(p.packet_time) {} |
| 172 |
| 173 | } // namespace rtc |
| 174 |