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.

190 lines
6.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, 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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <vector>
  31. #include "talk/base/flags.h"
  32. #include "talk/examples/peerconnection/server/data_socket.h"
  33. #include "talk/examples/peerconnection/server/peer_channel.h"
  34. #include "talk/examples/peerconnection/server/utils.h"
  35. DEFINE_bool(help, false, "Prints this message");
  36. DEFINE_int(port, 8888, "The port on which to listen.");
  37. static const size_t kMaxConnections = (FD_SETSIZE - 2);
  38. void HandleBrowserRequest(DataSocket* ds, bool* quit) {
  39. assert(ds && ds->valid());
  40. assert(quit);
  41. const std::string& path = ds->request_path();
  42. *quit = (path.compare("/quit") == 0);
  43. if (*quit) {
  44. ds->Send("200 OK", true, "text/html", "",
  45. "<html><body>Quitting...</body></html>");
  46. } else if (ds->method() == DataSocket::OPTIONS) {
  47. // We'll get this when a browsers do cross-resource-sharing requests.
  48. // The headers to allow cross-origin script support will be set inside
  49. // Send.
  50. ds->Send("200 OK", true, "", "", "");
  51. } else {
  52. // Here we could write some useful output back to the browser depending on
  53. // the path.
  54. printf("Received an invalid request: %s\n", ds->request_path().c_str());
  55. ds->Send("500 Sorry", true, "text/html", "",
  56. "<html><body>Sorry, not yet implemented</body></html>");
  57. }
  58. }
  59. int main(int argc, char** argv) {
  60. FlagList::SetFlagsFromCommandLine(&argc, argv, true);
  61. if (FLAG_help) {
  62. FlagList::Print(NULL, false);
  63. return 0;
  64. }
  65. // Abort if the user specifies a port that is outside the allowed
  66. // range [1, 65535].
  67. if ((FLAG_port < 1) || (FLAG_port > 65535)) {
  68. printf("Error: %i is not a valid port.\n", FLAG_port);
  69. return -1;
  70. }
  71. ListeningSocket listener;
  72. if (!listener.Create()) {
  73. printf("Failed to create server socket\n");
  74. return -1;
  75. } else if (!listener.Listen(FLAG_port)) {
  76. printf("Failed to listen on server socket\n");
  77. return -1;
  78. }
  79. printf("Server listening on port %i\n", FLAG_port);
  80. PeerChannel clients;
  81. typedef std::vector<DataSocket*> SocketArray;
  82. SocketArray sockets;
  83. bool quit = false;
  84. while (!quit) {
  85. fd_set socket_set;
  86. FD_ZERO(&socket_set);
  87. if (listener.valid())
  88. FD_SET(listener.socket(), &socket_set);
  89. for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i)
  90. FD_SET((*i)->socket(), &socket_set);
  91. struct timeval timeout = { 10, 0 };
  92. if (select(FD_SETSIZE, &socket_set, NULL, NULL, &timeout) == SOCKET_ERROR) {
  93. printf("select failed\n");
  94. break;
  95. }
  96. for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) {
  97. DataSocket* s = *i;
  98. bool socket_done = true;
  99. if (FD_ISSET(s->socket(), &socket_set)) {
  100. if (s->OnDataAvailable(&socket_done) && s->request_received()) {
  101. ChannelMember* member = clients.Lookup(s);
  102. if (member || PeerChannel::IsPeerConnection(s)) {
  103. if (!member) {
  104. if (s->PathEquals("/sign_in")) {
  105. clients.AddMember(s);
  106. } else {
  107. printf("No member found for: %s\n",
  108. s->request_path().c_str());
  109. s->Send("500 Error", true, "text/plain", "",
  110. "Peer most likely gone.");
  111. }
  112. } else if (member->is_wait_request(s)) {
  113. // no need to do anything.
  114. socket_done = false;
  115. } else {
  116. ChannelMember* target = clients.IsTargetedRequest(s);
  117. if (target) {
  118. member->ForwardRequestToPeer(s, target);
  119. } else if (s->PathEquals("/sign_out")) {
  120. s->Send("200 OK", true, "text/plain", "", "");
  121. } else {
  122. printf("Couldn't find target for request: %s\n",
  123. s->request_path().c_str());
  124. s->Send("500 Error", true, "text/plain", "",
  125. "Peer most likely gone.");
  126. }
  127. }
  128. } else {
  129. HandleBrowserRequest(s, &quit);
  130. if (quit) {
  131. printf("Quitting...\n");
  132. FD_CLR(listener.socket(), &socket_set);
  133. listener.Close();
  134. clients.CloseAll();
  135. }
  136. }
  137. }
  138. } else {
  139. socket_done = false;
  140. }
  141. if (socket_done) {
  142. printf("Disconnecting socket\n");
  143. clients.OnClosing(s);
  144. assert(s->valid()); // Close must not have been called yet.
  145. FD_CLR(s->socket(), &socket_set);
  146. delete (*i);
  147. i = sockets.erase(i);
  148. if (i == sockets.end())
  149. break;
  150. }
  151. }
  152. clients.CheckForTimeout();
  153. if (FD_ISSET(listener.socket(), &socket_set)) {
  154. DataSocket* s = listener.Accept();
  155. if (sockets.size() >= kMaxConnections) {
  156. delete s; // sorry, that's all we can take.
  157. printf("Connection limit reached\n");
  158. } else {
  159. sockets.push_back(s);
  160. printf("New connection...\n");
  161. }
  162. }
  163. }
  164. for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i)
  165. delete (*i);
  166. sockets.clear();
  167. return 0;
  168. }