henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | #include "webrtc/base/testclient.h" |
| 12 | #include "webrtc/base/thread.h" |
| 13 | #include "webrtc/base/timeutils.h" |
| 14 | |
| 15 | namespace rtc { |
| 16 | |
| 17 | // DESIGN: Each packet received is put it into a list of packets. |
| 18 | // Callers can retrieve received packets from any thread by calling |
| 19 | // NextPacket. |
| 20 | |
| 21 | TestClient::TestClient(AsyncPacketSocket* socket) |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 22 | : socket_(socket), ready_to_send_(false), prev_packet_timestamp_(-1) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | packets_ = new std::vector<Packet*>(); |
| 24 | socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); |
| 25 | socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); |
| 26 | } |
| 27 | |
| 28 | TestClient::~TestClient() { |
| 29 | delete socket_; |
| 30 | for (unsigned i = 0; i < packets_->size(); i++) |
| 31 | delete (*packets_)[i]; |
| 32 | delete packets_; |
| 33 | } |
| 34 | |
| 35 | bool TestClient::CheckConnState(AsyncPacketSocket::State state) { |
| 36 | // Wait for our timeout value until the socket reaches the desired state. |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 37 | int64_t end = TimeAfter(kTimeoutMs); |
| 38 | while (socket_->GetState() != state && TimeUntil(end) > 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | Thread::Current()->ProcessMessages(1); |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 40 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | return (socket_->GetState() == state); |
| 42 | } |
| 43 | |
| 44 | int TestClient::Send(const char* buf, size_t size) { |
| 45 | rtc::PacketOptions options; |
| 46 | return socket_->Send(buf, size, options); |
| 47 | } |
| 48 | |
| 49 | int TestClient::SendTo(const char* buf, size_t size, |
| 50 | const SocketAddress& dest) { |
| 51 | rtc::PacketOptions options; |
| 52 | return socket_->SendTo(buf, size, dest, options); |
| 53 | } |
| 54 | |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 55 | TestClient::Packet* TestClient::NextPacket(int timeout_ms) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | // If no packets are currently available, we go into a get/dispatch loop for |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 57 | // at most timeout_ms. If, during the loop, a packet arrives, then we can |
| 58 | // stop early and return it. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | |
| 60 | // Note that the case where no packet arrives is important. We often want to |
| 61 | // test that a packet does not arrive. |
| 62 | |
| 63 | // Note also that we only try to pump our current thread's message queue. |
| 64 | // Pumping another thread's queue could lead to messages being dispatched from |
| 65 | // the wrong thread to non-thread-safe objects. |
| 66 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 67 | int64_t end = TimeAfter(timeout_ms); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | while (TimeUntil(end) > 0) { |
| 69 | { |
| 70 | CritScope cs(&crit_); |
| 71 | if (packets_->size() != 0) { |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | Thread::Current()->ProcessMessages(1); |
| 76 | } |
| 77 | |
| 78 | // Return the first packet placed in the queue. |
| 79 | Packet* packet = NULL; |
| 80 | CritScope cs(&crit_); |
| 81 | if (packets_->size() > 0) { |
| 82 | packet = packets_->front(); |
| 83 | packets_->erase(packets_->begin()); |
| 84 | } |
| 85 | |
| 86 | return packet; |
| 87 | } |
| 88 | |
| 89 | bool TestClient::CheckNextPacket(const char* buf, size_t size, |
| 90 | SocketAddress* addr) { |
| 91 | bool res = false; |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 92 | Packet* packet = NextPacket(kTimeoutMs); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | if (packet) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 94 | res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 && |
| 95 | CheckTimestamp(packet->packet_time.timestamp)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | if (addr) |
| 97 | *addr = packet->addr; |
| 98 | delete packet; |
| 99 | } |
| 100 | return res; |
| 101 | } |
| 102 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 103 | bool TestClient::CheckTimestamp(int64_t packet_timestamp) { |
| 104 | bool res = true; |
| 105 | if (packet_timestamp == -1) { |
| 106 | res = false; |
| 107 | } |
| 108 | int64_t time_us = rtc::TimeMicros(); |
| 109 | if (prev_packet_timestamp_ != -1) { |
| 110 | if (packet_timestamp < prev_packet_timestamp_) { |
| 111 | res = false; |
| 112 | } |
| 113 | const int64_t kErrorMarginUs = 20000; |
| 114 | if (packet_timestamp - prev_packet_timestamp_ < |
| 115 | time_us - prev_time_us_ - kErrorMarginUs || |
| 116 | packet_timestamp - prev_packet_timestamp_ > |
| 117 | time_us - prev_time_us_ + kErrorMarginUs) { |
| 118 | res = false; |
| 119 | } |
| 120 | } |
| 121 | prev_packet_timestamp_ = packet_timestamp; |
| 122 | prev_time_us_ = time_us; |
| 123 | return res; |
| 124 | } |
| 125 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | bool TestClient::CheckNoPacket() { |
| 127 | bool res; |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 128 | Packet* packet = NextPacket(kNoPacketTimeoutMs); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | res = (packet == NULL); |
| 130 | delete packet; |
| 131 | return res; |
| 132 | } |
| 133 | |
| 134 | int TestClient::GetError() { |
| 135 | return socket_->GetError(); |
| 136 | } |
| 137 | |
| 138 | int TestClient::SetOption(Socket::Option opt, int value) { |
| 139 | return socket_->SetOption(opt, value); |
| 140 | } |
| 141 | |
| 142 | bool TestClient::ready_to_send() const { |
| 143 | return ready_to_send_; |
| 144 | } |
| 145 | |
| 146 | void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf, |
| 147 | size_t size, const SocketAddress& remote_addr, |
| 148 | const PacketTime& packet_time) { |
| 149 | CritScope cs(&crit_); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 150 | packets_->push_back(new Packet(remote_addr, buf, size, packet_time)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { |
| 154 | ready_to_send_ = true; |
| 155 | } |
| 156 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 157 | TestClient::Packet::Packet(const SocketAddress& a, |
| 158 | const char* b, |
| 159 | size_t s, |
| 160 | const PacketTime& packet_time) |
| 161 | : addr(a), buf(0), size(s), packet_time(packet_time) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | buf = new char[size]; |
| 163 | memcpy(buf, b, size); |
| 164 | } |
| 165 | |
| 166 | TestClient::Packet::Packet(const Packet& p) |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 167 | : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 168 | buf = new char[size]; |
| 169 | memcpy(buf, p.buf, size); |
| 170 | } |
| 171 | |
| 172 | TestClient::Packet::~Packet() { |
| 173 | delete[] buf; |
| 174 | } |
| 175 | |
| 176 | } // namespace rtc |