henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/base/testclient.h" |
| 29 | #include "talk/base/thread.h" |
| 30 | #include "talk/base/timeutils.h" |
| 31 | |
| 32 | namespace talk_base { |
| 33 | |
| 34 | // DESIGN: Each packet received is put it into a list of packets. |
| 35 | // Callers can retrieve received packets from any thread by calling |
| 36 | // NextPacket. |
| 37 | |
| 38 | TestClient::TestClient(AsyncPacketSocket* socket) |
| 39 | : socket_(socket), ready_to_send_(false) { |
| 40 | packets_ = new std::vector<Packet*>(); |
| 41 | socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); |
| 42 | socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); |
| 43 | } |
| 44 | |
| 45 | TestClient::~TestClient() { |
| 46 | delete socket_; |
| 47 | for (unsigned i = 0; i < packets_->size(); i++) |
| 48 | delete (*packets_)[i]; |
| 49 | delete packets_; |
| 50 | } |
| 51 | |
| 52 | bool TestClient::CheckConnState(AsyncPacketSocket::State state) { |
| 53 | // Wait for our timeout value until the socket reaches the desired state. |
| 54 | uint32 end = TimeAfter(kTimeout); |
| 55 | while (socket_->GetState() != state && TimeUntil(end) > 0) |
| 56 | Thread::Current()->ProcessMessages(1); |
| 57 | return (socket_->GetState() == state); |
| 58 | } |
| 59 | |
| 60 | int TestClient::Send(const char* buf, size_t size) { |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 61 | talk_base::PacketOptions options; |
| 62 | return socket_->Send(buf, size, options); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int TestClient::SendTo(const char* buf, size_t size, |
| 66 | const SocketAddress& dest) { |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 67 | talk_base::PacketOptions options; |
| 68 | return socket_->SendTo(buf, size, dest, options); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TestClient::Packet* TestClient::NextPacket() { |
| 72 | // If no packets are currently available, we go into a get/dispatch loop for |
| 73 | // at most 1 second. If, during the loop, a packet arrives, then we can stop |
| 74 | // early and return it. |
| 75 | |
| 76 | // Note that the case where no packet arrives is important. We often want to |
| 77 | // test that a packet does not arrive. |
| 78 | |
| 79 | // Note also that we only try to pump our current thread's message queue. |
| 80 | // Pumping another thread's queue could lead to messages being dispatched from |
| 81 | // the wrong thread to non-thread-safe objects. |
| 82 | |
| 83 | uint32 end = TimeAfter(kTimeout); |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 84 | while (TimeUntil(end) > 0) { |
| 85 | { |
| 86 | CritScope cs(&crit_); |
| 87 | if (packets_->size() != 0) { |
| 88 | break; |
| 89 | } |
| 90 | } |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | Thread::Current()->ProcessMessages(1); |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 92 | } |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | |
| 94 | // Return the first packet placed in the queue. |
| 95 | Packet* packet = NULL; |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 96 | CritScope cs(&crit_); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | if (packets_->size() > 0) { |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | packet = packets_->front(); |
| 99 | packets_->erase(packets_->begin()); |
| 100 | } |
| 101 | |
| 102 | return packet; |
| 103 | } |
| 104 | |
| 105 | bool TestClient::CheckNextPacket(const char* buf, size_t size, |
| 106 | SocketAddress* addr) { |
| 107 | bool res = false; |
| 108 | Packet* packet = NextPacket(); |
| 109 | if (packet) { |
pbos@webrtc.org | b951827 | 2014-03-07 15:22:04 +0000 | [diff] [blame] | 110 | res = (packet->size == size && memcmp(packet->buf, buf, size) == 0); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | if (addr) |
| 112 | *addr = packet->addr; |
| 113 | delete packet; |
| 114 | } |
| 115 | return res; |
| 116 | } |
| 117 | |
| 118 | bool TestClient::CheckNoPacket() { |
| 119 | bool res; |
| 120 | Packet* packet = NextPacket(); |
| 121 | res = (packet == NULL); |
| 122 | delete packet; |
| 123 | return res; |
| 124 | } |
| 125 | |
| 126 | int TestClient::GetError() { |
| 127 | return socket_->GetError(); |
| 128 | } |
| 129 | |
| 130 | int TestClient::SetOption(Socket::Option opt, int value) { |
| 131 | return socket_->SetOption(opt, value); |
| 132 | } |
| 133 | |
| 134 | bool TestClient::ready_to_send() const { |
| 135 | return ready_to_send_; |
| 136 | } |
| 137 | |
| 138 | void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf, |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 139 | size_t size, const SocketAddress& remote_addr, |
| 140 | const PacketTime& packet_time) { |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | CritScope cs(&crit_); |
| 142 | packets_->push_back(new Packet(remote_addr, buf, size)); |
| 143 | } |
| 144 | |
| 145 | void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { |
| 146 | ready_to_send_ = true; |
| 147 | } |
| 148 | |
| 149 | TestClient::Packet::Packet(const SocketAddress& a, const char* b, size_t s) |
| 150 | : addr(a), buf(0), size(s) { |
| 151 | buf = new char[size]; |
| 152 | memcpy(buf, b, size); |
| 153 | } |
| 154 | |
| 155 | TestClient::Packet::Packet(const Packet& p) |
| 156 | : addr(p.addr), buf(0), size(p.size) { |
| 157 | buf = new char[size]; |
| 158 | memcpy(buf, p.buf, size); |
| 159 | } |
| 160 | |
| 161 | TestClient::Packet::~Packet() { |
| 162 | delete[] buf; |
| 163 | } |
| 164 | |
| 165 | } // namespace talk_base |