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.

193 lines
6.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2011 The WebRTC Project Authors. All rights reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #if defined(WEBRTC_POSIX)
  14. #include <sys/select.h>
  15. #endif
  16. #include <time.h>
  17. #include <string>
  18. #include <vector>
  19. #include "absl/flags/flag.h"
  20. #include "absl/flags/parse.h"
  21. #include "absl/flags/usage.h"
  22. #include "examples/peerconnection/server/data_socket.h"
  23. #include "examples/peerconnection/server/peer_channel.h"
  24. #include "system_wrappers/include/field_trial.h"
  25. #include "test/field_trial.h"
  26. ABSL_FLAG(
  27. std::string,
  28. force_fieldtrials,
  29. "",
  30. "Field trials control experimental features. This flag specifies the field "
  31. "trials in effect. E.g. running with "
  32. "--force_fieldtrials=WebRTC-FooFeature/Enabled/ "
  33. "will assign the group Enabled to field trial WebRTC-FooFeature. Multiple "
  34. "trials are separated by \"/\"");
  35. ABSL_FLAG(int, port, 8888, "default: 8888");
  36. static const size_t kMaxConnections = (FD_SETSIZE - 2);
  37. void HandleBrowserRequest(DataSocket* ds, bool* quit) {
  38. assert(ds && ds->valid());
  39. assert(quit);
  40. const std::string& path = ds->request_path();
  41. *quit = (path.compare("/quit") == 0);
  42. if (*quit) {
  43. ds->Send("200 OK", true, "text/html", "",
  44. "<html><body>Quitting...</body></html>");
  45. } else if (ds->method() == DataSocket::OPTIONS) {
  46. // We'll get this when a browsers do cross-resource-sharing requests.
  47. // The headers to allow cross-origin script support will be set inside
  48. // Send.
  49. ds->Send("200 OK", true, "", "", "");
  50. } else {
  51. // Here we could write some useful output back to the browser depending on
  52. // the path.
  53. printf("Received an invalid request: %s\n", ds->request_path().c_str());
  54. ds->Send("500 Sorry", true, "text/html", "",
  55. "<html><body>Sorry, not yet implemented</body></html>");
  56. }
  57. }
  58. int main(int argc, char* argv[]) {
  59. absl::SetProgramUsageMessage(
  60. "Example usage: ./peerconnection_server --port=8888\n");
  61. absl::ParseCommandLine(argc, argv);
  62. // InitFieldTrialsFromString stores the char*, so the char array must outlive
  63. // the application.
  64. const std::string force_field_trials = absl::GetFlag(FLAGS_force_fieldtrials);
  65. webrtc::field_trial::InitFieldTrialsFromString(force_field_trials.c_str());
  66. int port = absl::GetFlag(FLAGS_port);
  67. // Abort if the user specifies a port that is outside the allowed
  68. // range [1, 65535].
  69. if ((port < 1) || (port > 65535)) {
  70. printf("Error: %i is not a valid port.\n", port);
  71. return -1;
  72. }
  73. ListeningSocket listener;
  74. if (!listener.Create()) {
  75. printf("Failed to create server socket\n");
  76. return -1;
  77. } else if (!listener.Listen(port)) {
  78. printf("Failed to listen on server socket\n");
  79. return -1;
  80. }
  81. printf("Server listening on port %i\n", port);
  82. PeerChannel clients;
  83. typedef std::vector<DataSocket*> SocketArray;
  84. SocketArray sockets;
  85. bool quit = false;
  86. while (!quit) {
  87. fd_set socket_set;
  88. FD_ZERO(&socket_set);
  89. if (listener.valid())
  90. FD_SET(listener.socket(), &socket_set);
  91. for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i)
  92. FD_SET((*i)->socket(), &socket_set);
  93. struct timeval timeout = {10, 0};
  94. if (select(FD_SETSIZE, &socket_set, NULL, NULL, &timeout) == SOCKET_ERROR) {
  95. printf("select failed\n");
  96. break;
  97. }
  98. for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) {
  99. DataSocket* s = *i;
  100. bool socket_done = true;
  101. if (FD_ISSET(s->socket(), &socket_set)) {
  102. if (s->OnDataAvailable(&socket_done) && s->request_received()) {
  103. ChannelMember* member = clients.Lookup(s);
  104. if (member || PeerChannel::IsPeerConnection(s)) {
  105. if (!member) {
  106. if (s->PathEquals("/sign_in")) {
  107. clients.AddMember(s);
  108. } else {
  109. printf("No member found for: %s\n", s->request_path().c_str());
  110. s->Send("500 Error", true, "text/plain", "",
  111. "Peer most likely gone.");
  112. }
  113. } else if (member->is_wait_request(s)) {
  114. // no need to do anything.
  115. socket_done = false;
  116. } else {
  117. ChannelMember* target = clients.IsTargetedRequest(s);
  118. if (target) {
  119. member->ForwardRequestToPeer(s, target);
  120. } else if (s->PathEquals("/sign_out")) {
  121. s->Send("200 OK", true, "text/plain", "", "");
  122. } else {
  123. printf("Couldn't find target for request: %s\n",
  124. s->request_path().c_str());
  125. s->Send("500 Error", true, "text/plain", "",
  126. "Peer most likely gone.");
  127. }
  128. }
  129. } else {
  130. HandleBrowserRequest(s, &quit);
  131. if (quit) {
  132. printf("Quitting...\n");
  133. FD_CLR(listener.socket(), &socket_set);
  134. listener.Close();
  135. clients.CloseAll();
  136. }
  137. }
  138. }
  139. } else {
  140. socket_done = false;
  141. }
  142. if (socket_done) {
  143. printf("Disconnecting socket\n");
  144. clients.OnClosing(s);
  145. assert(s->valid()); // Close must not have been called yet.
  146. FD_CLR(s->socket(), &socket_set);
  147. delete (*i);
  148. i = sockets.erase(i);
  149. if (i == sockets.end())
  150. break;
  151. }
  152. }
  153. clients.CheckForTimeout();
  154. if (FD_ISSET(listener.socket(), &socket_set)) {
  155. DataSocket* s = listener.Accept();
  156. if (sockets.size() >= kMaxConnections) {
  157. delete s; // sorry, that's all we can take.
  158. printf("Connection limit reached\n");
  159. } else {
  160. sockets.push_back(s);
  161. printf("New connection...\n");
  162. }
  163. }
  164. }
  165. for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i)
  166. delete (*i);
  167. sockets.clear();
  168. return 0;
  169. }