You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/base/testclient.h"
  28. #include "talk/base/thread.h"
  29. #include "talk/base/timeutils.h"
  30. namespace talk_base {
  31. // DESIGN: Each packet received is put it into a list of packets.
  32. // Callers can retrieve received packets from any thread by calling
  33. // NextPacket.
  34. TestClient::TestClient(AsyncPacketSocket* socket) : socket_(socket) {
  35. packets_ = new std::vector<Packet*>();
  36. socket_->SignalReadPacket.connect(this, &TestClient::OnPacket);
  37. }
  38. TestClient::~TestClient() {
  39. delete socket_;
  40. for (unsigned i = 0; i < packets_->size(); i++)
  41. delete (*packets_)[i];
  42. delete packets_;
  43. }
  44. bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
  45. // Wait for our timeout value until the socket reaches the desired state.
  46. uint32 end = TimeAfter(kTimeout);
  47. while (socket_->GetState() != state && TimeUntil(end) > 0)
  48. Thread::Current()->ProcessMessages(1);
  49. return (socket_->GetState() == state);
  50. }
  51. int TestClient::Send(const char* buf, size_t size) {
  52. return socket_->Send(buf, size);
  53. }
  54. int TestClient::SendTo(const char* buf, size_t size,
  55. const SocketAddress& dest) {
  56. return socket_->SendTo(buf, size, dest);
  57. }
  58. TestClient::Packet* TestClient::NextPacket() {
  59. // If no packets are currently available, we go into a get/dispatch loop for
  60. // at most 1 second. If, during the loop, a packet arrives, then we can stop
  61. // early and return it.
  62. // Note that the case where no packet arrives is important. We often want to
  63. // test that a packet does not arrive.
  64. // Note also that we only try to pump our current thread's message queue.
  65. // Pumping another thread's queue could lead to messages being dispatched from
  66. // the wrong thread to non-thread-safe objects.
  67. uint32 end = TimeAfter(kTimeout);
  68. while (packets_->size() == 0 && TimeUntil(end) > 0)
  69. Thread::Current()->ProcessMessages(1);
  70. // Return the first packet placed in the queue.
  71. Packet* packet = NULL;
  72. if (packets_->size() > 0) {
  73. CritScope cs(&crit_);
  74. packet = packets_->front();
  75. packets_->erase(packets_->begin());
  76. }
  77. return packet;
  78. }
  79. bool TestClient::CheckNextPacket(const char* buf, size_t size,
  80. SocketAddress* addr) {
  81. bool res = false;
  82. Packet* packet = NextPacket();
  83. if (packet) {
  84. res = (packet->size == size && std::memcmp(packet->buf, buf, size) == 0);
  85. if (addr)
  86. *addr = packet->addr;
  87. delete packet;
  88. }
  89. return res;
  90. }
  91. bool TestClient::CheckNoPacket() {
  92. bool res;
  93. Packet* packet = NextPacket();
  94. res = (packet == NULL);
  95. delete packet;
  96. return res;
  97. }
  98. void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf,
  99. size_t size, const SocketAddress& remote_addr) {
  100. CritScope cs(&crit_);
  101. packets_->push_back(new Packet(remote_addr, buf, size));
  102. }
  103. TestClient::Packet::Packet(const SocketAddress& a, const char* b, size_t s)
  104. : addr(a), buf(0), size(s) {
  105. buf = new char[size];
  106. memcpy(buf, b, size);
  107. }
  108. TestClient::Packet::Packet(const Packet& p)
  109. : addr(p.addr), buf(0), size(p.size) {
  110. buf = new char[size];
  111. memcpy(buf, p.buf, size);
  112. }
  113. TestClient::Packet::~Packet() {
  114. delete[] buf;
  115. }
  116. } // namespace talk_base