OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/testclient.h" | 11 #include "webrtc/base/testclient.h" |
| 12 #include "webrtc/base/ptr_util.h" |
12 #include "webrtc/base/thread.h" | 13 #include "webrtc/base/thread.h" |
13 #include "webrtc/base/timeutils.h" | 14 #include "webrtc/base/timeutils.h" |
14 | 15 |
15 namespace rtc { | 16 namespace rtc { |
16 | 17 |
17 // DESIGN: Each packet received is put it into a list of packets. | 18 // DESIGN: Each packet received is put it into a list of packets. |
18 // Callers can retrieve received packets from any thread by calling | 19 // Callers can retrieve received packets from any thread by calling |
19 // NextPacket. | 20 // NextPacket. |
20 | 21 |
21 TestClient::TestClient(AsyncPacketSocket* socket) | 22 TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket) |
22 : socket_(socket), prev_packet_timestamp_(-1) { | 23 : socket_(std::move(socket)), prev_packet_timestamp_(-1) { |
23 packets_ = new std::vector<Packet*>(); | |
24 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); | 24 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); |
25 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); | 25 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); |
26 } | 26 } |
27 | 27 |
28 TestClient::~TestClient() { | 28 TestClient::~TestClient() {} |
29 delete socket_; | |
30 for (unsigned i = 0; i < packets_->size(); i++) | |
31 delete (*packets_)[i]; | |
32 delete packets_; | |
33 } | |
34 | 29 |
35 bool TestClient::CheckConnState(AsyncPacketSocket::State state) { | 30 bool TestClient::CheckConnState(AsyncPacketSocket::State state) { |
36 // Wait for our timeout value until the socket reaches the desired state. | 31 // Wait for our timeout value until the socket reaches the desired state. |
37 int64_t end = TimeAfter(kTimeoutMs); | 32 int64_t end = TimeAfter(kTimeoutMs); |
38 while (socket_->GetState() != state && TimeUntil(end) > 0) { | 33 while (socket_->GetState() != state && TimeUntil(end) > 0) { |
39 Thread::Current()->ProcessMessages(1); | 34 Thread::Current()->ProcessMessages(1); |
40 } | 35 } |
41 return (socket_->GetState() == state); | 36 return (socket_->GetState() == state); |
42 } | 37 } |
43 | 38 |
44 int TestClient::Send(const char* buf, size_t size) { | 39 int TestClient::Send(const char* buf, size_t size) { |
45 rtc::PacketOptions options; | 40 rtc::PacketOptions options; |
46 return socket_->Send(buf, size, options); | 41 return socket_->Send(buf, size, options); |
47 } | 42 } |
48 | 43 |
49 int TestClient::SendTo(const char* buf, size_t size, | 44 int TestClient::SendTo(const char* buf, size_t size, |
50 const SocketAddress& dest) { | 45 const SocketAddress& dest) { |
51 rtc::PacketOptions options; | 46 rtc::PacketOptions options; |
52 return socket_->SendTo(buf, size, dest, options); | 47 return socket_->SendTo(buf, size, dest, options); |
53 } | 48 } |
54 | 49 |
55 TestClient::Packet* TestClient::NextPacket(int timeout_ms) { | 50 std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) { |
56 // If no packets are currently available, we go into a get/dispatch loop for | 51 // If no packets are currently available, we go into a get/dispatch loop for |
57 // at most timeout_ms. If, during the loop, a packet arrives, then we can | 52 // at most timeout_ms. If, during the loop, a packet arrives, then we can |
58 // stop early and return it. | 53 // stop early and return it. |
59 | 54 |
60 // Note that the case where no packet arrives is important. We often want to | 55 // Note that the case where no packet arrives is important. We often want to |
61 // test that a packet does not arrive. | 56 // test that a packet does not arrive. |
62 | 57 |
63 // Note also that we only try to pump our current thread's message queue. | 58 // 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 | 59 // Pumping another thread's queue could lead to messages being dispatched from |
65 // the wrong thread to non-thread-safe objects. | 60 // the wrong thread to non-thread-safe objects. |
66 | 61 |
67 int64_t end = TimeAfter(timeout_ms); | 62 int64_t end = TimeAfter(timeout_ms); |
68 while (TimeUntil(end) > 0) { | 63 while (TimeUntil(end) > 0) { |
69 { | 64 { |
70 CritScope cs(&crit_); | 65 CritScope cs(&crit_); |
71 if (packets_->size() != 0) { | 66 if (packets_.size() != 0) { |
72 break; | 67 break; |
73 } | 68 } |
74 } | 69 } |
75 Thread::Current()->ProcessMessages(1); | 70 Thread::Current()->ProcessMessages(1); |
76 } | 71 } |
77 | 72 |
78 // Return the first packet placed in the queue. | 73 // Return the first packet placed in the queue. |
79 Packet* packet = nullptr; | 74 std::unique_ptr<Packet> packet; |
80 CritScope cs(&crit_); | 75 CritScope cs(&crit_); |
81 if (packets_->size() > 0) { | 76 if (packets_.size() > 0) { |
82 packet = packets_->front(); | 77 packet = std::move(packets_.front()); |
83 packets_->erase(packets_->begin()); | 78 packets_.erase(packets_.begin()); |
84 } | 79 } |
85 | 80 |
86 return packet; | 81 return packet; |
87 } | 82 } |
88 | 83 |
89 bool TestClient::CheckNextPacket(const char* buf, size_t size, | 84 bool TestClient::CheckNextPacket(const char* buf, size_t size, |
90 SocketAddress* addr) { | 85 SocketAddress* addr) { |
91 bool res = false; | 86 bool res = false; |
92 Packet* packet = NextPacket(kTimeoutMs); | 87 std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs); |
93 if (packet) { | 88 if (packet) { |
94 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 && | 89 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 && |
95 CheckTimestamp(packet->packet_time.timestamp)); | 90 CheckTimestamp(packet->packet_time.timestamp)); |
96 if (addr) | 91 if (addr) |
97 *addr = packet->addr; | 92 *addr = packet->addr; |
98 delete packet; | |
99 } | 93 } |
100 return res; | 94 return res; |
101 } | 95 } |
102 | 96 |
103 bool TestClient::CheckTimestamp(int64_t packet_timestamp) { | 97 bool TestClient::CheckTimestamp(int64_t packet_timestamp) { |
104 bool res = true; | 98 bool res = true; |
105 if (packet_timestamp == -1) { | 99 if (packet_timestamp == -1) { |
106 res = false; | 100 res = false; |
107 } | 101 } |
108 if (prev_packet_timestamp_ != -1) { | 102 if (prev_packet_timestamp_ != -1) { |
109 if (packet_timestamp < prev_packet_timestamp_) { | 103 if (packet_timestamp < prev_packet_timestamp_) { |
110 res = false; | 104 res = false; |
111 } | 105 } |
112 } | 106 } |
113 prev_packet_timestamp_ = packet_timestamp; | 107 prev_packet_timestamp_ = packet_timestamp; |
114 return res; | 108 return res; |
115 } | 109 } |
116 | 110 |
117 bool TestClient::CheckNoPacket() { | 111 bool TestClient::CheckNoPacket() { |
118 bool res; | 112 return NextPacket(kNoPacketTimeoutMs) == nullptr; |
119 Packet* packet = NextPacket(kNoPacketTimeoutMs); | |
120 res = (packet == nullptr); | |
121 delete packet; | |
122 return res; | |
123 } | 113 } |
124 | 114 |
125 int TestClient::GetError() { | 115 int TestClient::GetError() { |
126 return socket_->GetError(); | 116 return socket_->GetError(); |
127 } | 117 } |
128 | 118 |
129 int TestClient::SetOption(Socket::Option opt, int value) { | 119 int TestClient::SetOption(Socket::Option opt, int value) { |
130 return socket_->SetOption(opt, value); | 120 return socket_->SetOption(opt, value); |
131 } | 121 } |
132 | 122 |
133 void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf, | 123 void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf, |
134 size_t size, const SocketAddress& remote_addr, | 124 size_t size, const SocketAddress& remote_addr, |
135 const PacketTime& packet_time) { | 125 const PacketTime& packet_time) { |
136 CritScope cs(&crit_); | 126 CritScope cs(&crit_); |
137 packets_->push_back(new Packet(remote_addr, buf, size, packet_time)); | 127 packets_.push_back(MakeUnique<Packet>(remote_addr, buf, size, packet_time)); |
138 } | 128 } |
139 | 129 |
140 void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { | 130 void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { |
141 ++ready_to_send_count_; | 131 ++ready_to_send_count_; |
142 } | 132 } |
143 | 133 |
144 TestClient::Packet::Packet(const SocketAddress& a, | 134 TestClient::Packet::Packet(const SocketAddress& a, |
145 const char* b, | 135 const char* b, |
146 size_t s, | 136 size_t s, |
147 const PacketTime& packet_time) | 137 const PacketTime& packet_time) |
148 : addr(a), buf(0), size(s), packet_time(packet_time) { | 138 : addr(a), buf(0), size(s), packet_time(packet_time) { |
149 buf = new char[size]; | 139 buf = new char[size]; |
150 memcpy(buf, b, size); | 140 memcpy(buf, b, size); |
151 } | 141 } |
152 | 142 |
153 TestClient::Packet::Packet(const Packet& p) | 143 TestClient::Packet::Packet(const Packet& p) |
154 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) { | 144 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) { |
155 buf = new char[size]; | 145 buf = new char[size]; |
156 memcpy(buf, p.buf, size); | 146 memcpy(buf, p.buf, size); |
157 } | 147 } |
158 | 148 |
159 TestClient::Packet::~Packet() { | 149 TestClient::Packet::~Packet() { |
160 delete[] buf; | 150 delete[] buf; |
161 } | 151 } |
162 | 152 |
163 } // namespace rtc | 153 } // namespace rtc |
OLD | NEW |