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.

118 lines
4.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008 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. #ifndef TALK_P2P_BASE_TESTRELAYSERVER_H_
  28. #define TALK_P2P_BASE_TESTRELAYSERVER_H_
  29. #include "talk/base/asynctcpsocket.h"
  30. #include "talk/base/scoped_ptr.h"
  31. #include "talk/base/socketadapters.h"
  32. #include "talk/base/sigslot.h"
  33. #include "talk/base/thread.h"
  34. #include "talk/p2p/base/relayserver.h"
  35. namespace cricket {
  36. // A test relay server. Useful for unit tests.
  37. class TestRelayServer : public sigslot::has_slots<> {
  38. public:
  39. TestRelayServer(talk_base::Thread* thread,
  40. const talk_base::SocketAddress& udp_int_addr,
  41. const talk_base::SocketAddress& udp_ext_addr,
  42. const talk_base::SocketAddress& tcp_int_addr,
  43. const talk_base::SocketAddress& tcp_ext_addr,
  44. const talk_base::SocketAddress& ssl_int_addr,
  45. const talk_base::SocketAddress& ssl_ext_addr)
  46. : server_(thread) {
  47. server_.AddInternalSocket(talk_base::AsyncUDPSocket::Create(
  48. thread->socketserver(), udp_int_addr));
  49. server_.AddExternalSocket(talk_base::AsyncUDPSocket::Create(
  50. thread->socketserver(), udp_ext_addr));
  51. tcp_int_socket_.reset(CreateListenSocket(thread, tcp_int_addr));
  52. tcp_ext_socket_.reset(CreateListenSocket(thread, tcp_ext_addr));
  53. ssl_int_socket_.reset(CreateListenSocket(thread, ssl_int_addr));
  54. ssl_ext_socket_.reset(CreateListenSocket(thread, ssl_ext_addr));
  55. }
  56. int GetConnectionCount() const {
  57. return server_.GetConnectionCount();
  58. }
  59. talk_base::SocketAddressPair GetConnection(int connection) const {
  60. return server_.GetConnection(connection);
  61. }
  62. bool HasConnection(const talk_base::SocketAddress& address) const {
  63. return server_.HasConnection(address);
  64. }
  65. private:
  66. talk_base::AsyncSocket* CreateListenSocket(talk_base::Thread* thread,
  67. const talk_base::SocketAddress& addr) {
  68. talk_base::AsyncSocket* socket =
  69. thread->socketserver()->CreateAsyncSocket(addr.family(), SOCK_STREAM);
  70. socket->Bind(addr);
  71. socket->Listen(5);
  72. socket->SignalReadEvent.connect(this, &TestRelayServer::OnAccept);
  73. return socket;
  74. }
  75. void OnAccept(talk_base::AsyncSocket* socket) {
  76. bool external = (socket == tcp_ext_socket_.get() ||
  77. socket == ssl_ext_socket_.get());
  78. bool ssl = (socket == ssl_int_socket_.get() ||
  79. socket == ssl_ext_socket_.get());
  80. talk_base::AsyncSocket* raw_socket = socket->Accept(NULL);
  81. if (raw_socket) {
  82. talk_base::AsyncTCPSocket* packet_socket = new talk_base::AsyncTCPSocket(
  83. (!ssl) ? raw_socket :
  84. new talk_base::AsyncSSLServerSocket(raw_socket), false);
  85. if (!external) {
  86. packet_socket->SignalClose.connect(this,
  87. &TestRelayServer::OnInternalClose);
  88. server_.AddInternalSocket(packet_socket);
  89. } else {
  90. packet_socket->SignalClose.connect(this,
  91. &TestRelayServer::OnExternalClose);
  92. server_.AddExternalSocket(packet_socket);
  93. }
  94. }
  95. }
  96. void OnInternalClose(talk_base::AsyncPacketSocket* socket, int error) {
  97. server_.RemoveInternalSocket(socket);
  98. }
  99. void OnExternalClose(talk_base::AsyncPacketSocket* socket, int error) {
  100. server_.RemoveExternalSocket(socket);
  101. }
  102. private:
  103. cricket::RelayServer server_;
  104. talk_base::scoped_ptr<talk_base::AsyncSocket> tcp_int_socket_;
  105. talk_base::scoped_ptr<talk_base::AsyncSocket> tcp_ext_socket_;
  106. talk_base::scoped_ptr<talk_base::AsyncSocket> ssl_int_socket_;
  107. talk_base::scoped_ptr<talk_base::AsyncSocket> ssl_ext_socket_;
  108. };
  109. } // namespace cricket
  110. #endif // TALK_P2P_BASE_TESTRELAYSERVER_H_