blob: be4de82ad6006b445434e47d9c46fdef74105a43 [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)
22 : socket_(socket), ready_to_send_(false) {
23 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) {
94 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0);
95 if (addr)
96 *addr = packet->addr;
97 delete packet;
98 }
99 return res;
100}
101
102bool TestClient::CheckNoPacket() {
103 bool res;
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000104 Packet* packet = NextPacket(kNoPacketTimeoutMs);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 res = (packet == NULL);
106 delete packet;
107 return res;
108}
109
110int TestClient::GetError() {
111 return socket_->GetError();
112}
113
114int TestClient::SetOption(Socket::Option opt, int value) {
115 return socket_->SetOption(opt, value);
116}
117
118bool TestClient::ready_to_send() const {
119 return ready_to_send_;
120}
121
122void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf,
123 size_t size, const SocketAddress& remote_addr,
124 const PacketTime& packet_time) {
125 CritScope cs(&crit_);
126 packets_->push_back(new Packet(remote_addr, buf, size));
127}
128
129void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
130 ready_to_send_ = true;
131}
132
133TestClient::Packet::Packet(const SocketAddress& a, const char* b, size_t s)
134 : addr(a), buf(0), size(s) {
135 buf = new char[size];
136 memcpy(buf, b, size);
137}
138
139TestClient::Packet::Packet(const Packet& p)
140 : addr(p.addr), buf(0), size(p.size) {
141 buf = new char[size];
142 memcpy(buf, p.buf, size);
143}
144
145TestClient::Packet::~Packet() {
146 delete[] buf;
147}
148
149} // namespace rtc