blob: fbb4f0cdf33b138a59f34ebe95898c8a74e05b89 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
15namespace 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
21TestClient::TestClient(AsyncPacketSocket* socket)
Stefan Holmer9131efd2016-05-23 18:19:26 +020022 : socket_(socket), ready_to_send_(false), prev_packet_timestamp_(-1) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023 packets_ = new std::vector<Packet*>();
24 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket);
25 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend);
26}
27
28TestClient::~TestClient() {
29 delete socket_;
30 for (unsigned i = 0; i < packets_->size(); i++)
31 delete (*packets_)[i];
32 delete packets_;
33}
34
35bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
36 // Wait for our timeout value until the socket reaches the desired state.
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070037 int64_t end = TimeAfter(kTimeoutMs);
38 while (socket_->GetState() != state && TimeUntil(end) > 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 Thread::Current()->ProcessMessages(1);
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070040 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 return (socket_->GetState() == state);
42}
43
44int TestClient::Send(const char* buf, size_t size) {
45 rtc::PacketOptions options;
46 return socket_->Send(buf, size, options);
47}
48
49int 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.orgec499be2015-02-07 22:37:59 +000055TestClient::Packet* TestClient::NextPacket(int timeout_ms) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 // If no packets are currently available, we go into a get/dispatch loop for
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000057 // at most timeout_ms. If, during the loop, a packet arrives, then we can
58 // stop early and return it.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
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 Brandstetter2b3bf6b2016-05-19 14:57:31 -070067 int64_t end = TimeAfter(timeout_ms);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 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
89bool TestClient::CheckNextPacket(const char* buf, size_t size,
90 SocketAddress* addr) {
91 bool res = false;
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000092 Packet* packet = NextPacket(kTimeoutMs);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 if (packet) {
Stefan Holmer9131efd2016-05-23 18:19:26 +020094 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 &&
95 CheckTimestamp(packet->packet_time.timestamp));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 if (addr)
97 *addr = packet->addr;
98 delete packet;
99 }
100 return res;
101}
102
Stefan Holmer9131efd2016-05-23 18:19:26 +0200103bool 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.orgf0488722014-05-13 18:00:26 +0000126bool TestClient::CheckNoPacket() {
127 bool res;
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000128 Packet* packet = NextPacket(kNoPacketTimeoutMs);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 res = (packet == NULL);
130 delete packet;
131 return res;
132}
133
134int TestClient::GetError() {
135 return socket_->GetError();
136}
137
138int TestClient::SetOption(Socket::Option opt, int value) {
139 return socket_->SetOption(opt, value);
140}
141
142bool TestClient::ready_to_send() const {
143 return ready_to_send_;
144}
145
146void 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 Holmer9131efd2016-05-23 18:19:26 +0200150 packets_->push_back(new Packet(remote_addr, buf, size, packet_time));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151}
152
153void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
154 ready_to_send_ = true;
155}
156
Stefan Holmer9131efd2016-05-23 18:19:26 +0200157TestClient::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.orgf0488722014-05-13 18:00:26 +0000162 buf = new char[size];
163 memcpy(buf, b, size);
164}
165
166TestClient::Packet::Packet(const Packet& p)
Stefan Holmer9131efd2016-05-23 18:19:26 +0200167 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 buf = new char[size];
169 memcpy(buf, p.buf, size);
170}
171
172TestClient::Packet::~Packet() {
173 delete[] buf;
174}
175
176} // namespace rtc