henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Niels Möller | 433eafe | 2018-10-04 14:33:20 +0200 | [diff] [blame] | 11 | #include <assert.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "examples/peerconnection/server/data_socket.h" |
| 19 | #include "examples/peerconnection/server/peer_channel.h" |
| 20 | #include "examples/peerconnection/server/utils.h" |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame^] | 21 | #include "rtc_base/flags.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_tools/simple_command_line_parser.h" |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame^] | 23 | #include "system_wrappers/include/field_trial.h" |
| 24 | #include "test/field_trial.h" |
| 25 | |
| 26 | WEBRTC_DEFINE_string( |
| 27 | force_fieldtrials, |
| 28 | "", |
| 29 | "Field trials control experimental features. This flag specifies the field " |
| 30 | "trials in effect. E.g. running with " |
| 31 | "--force_fieldtrials=WebRTC-FooFeature/Enabled/ " |
| 32 | "will assign the group Enabled to field trial WebRTC-FooFeature. Multiple " |
| 33 | "trials are separated by \"/\""); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
| 35 | static const size_t kMaxConnections = (FD_SETSIZE - 2); |
| 36 | |
| 37 | void HandleBrowserRequest(DataSocket* ds, bool* quit) { |
| 38 | assert(ds && ds->valid()); |
| 39 | assert(quit); |
| 40 | |
| 41 | const std::string& path = ds->request_path(); |
| 42 | |
| 43 | *quit = (path.compare("/quit") == 0); |
| 44 | |
| 45 | if (*quit) { |
| 46 | ds->Send("200 OK", true, "text/html", "", |
| 47 | "<html><body>Quitting...</body></html>"); |
| 48 | } else if (ds->method() == DataSocket::OPTIONS) { |
| 49 | // We'll get this when a browsers do cross-resource-sharing requests. |
| 50 | // The headers to allow cross-origin script support will be set inside |
| 51 | // Send. |
| 52 | ds->Send("200 OK", true, "", "", ""); |
| 53 | } else { |
| 54 | // Here we could write some useful output back to the browser depending on |
| 55 | // the path. |
| 56 | printf("Received an invalid request: %s\n", ds->request_path().c_str()); |
| 57 | ds->Send("500 Sorry", true, "text/html", "", |
| 58 | "<html><body>Sorry, not yet implemented</body></html>"); |
| 59 | } |
| 60 | } |
| 61 | |
Robin Raymond | 1c62ffa | 2017-12-03 16:45:56 -0500 | [diff] [blame] | 62 | int main(int argc, char* argv[]) { |
tkchin | bad7b09 | 2016-03-11 20:45:16 -0800 | [diff] [blame] | 63 | std::string program_name = argv[0]; |
| 64 | std::string usage = "Example usage: " + program_name + " --port=8888"; |
| 65 | webrtc::test::CommandLineParser parser; |
| 66 | parser.Init(argc, argv); |
| 67 | parser.SetUsageMessage(usage); |
| 68 | parser.SetFlag("port", "8888"); |
| 69 | parser.SetFlag("help", "false"); |
| 70 | parser.ProcessFlags(); |
| 71 | |
| 72 | if (parser.GetFlag("help") == "true") { |
| 73 | parser.PrintUsageMessage(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame^] | 77 | webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials); |
| 78 | // InitFieldTrialsFromString stores the char*, so the char array must outlive |
| 79 | // the application. |
| 80 | webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials); |
| 81 | |
tkchin | bad7b09 | 2016-03-11 20:45:16 -0800 | [diff] [blame] | 82 | int port = strtol((parser.GetFlag("port")).c_str(), NULL, 10); |
| 83 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | // Abort if the user specifies a port that is outside the allowed |
| 85 | // range [1, 65535]. |
tkchin | bad7b09 | 2016-03-11 20:45:16 -0800 | [diff] [blame] | 86 | if ((port < 1) || (port > 65535)) { |
| 87 | printf("Error: %i is not a valid port.\n", port); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | ListeningSocket listener; |
| 92 | if (!listener.Create()) { |
| 93 | printf("Failed to create server socket\n"); |
| 94 | return -1; |
tkchin | bad7b09 | 2016-03-11 20:45:16 -0800 | [diff] [blame] | 95 | } else if (!listener.Listen(port)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | printf("Failed to listen on server socket\n"); |
| 97 | return -1; |
| 98 | } |
| 99 | |
tkchin | bad7b09 | 2016-03-11 20:45:16 -0800 | [diff] [blame] | 100 | printf("Server listening on port %i\n", port); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | |
| 102 | PeerChannel clients; |
| 103 | typedef std::vector<DataSocket*> SocketArray; |
| 104 | SocketArray sockets; |
| 105 | bool quit = false; |
| 106 | while (!quit) { |
| 107 | fd_set socket_set; |
| 108 | FD_ZERO(&socket_set); |
| 109 | if (listener.valid()) |
| 110 | FD_SET(listener.socket(), &socket_set); |
| 111 | |
| 112 | for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) |
| 113 | FD_SET((*i)->socket(), &socket_set); |
| 114 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 115 | struct timeval timeout = {10, 0}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | if (select(FD_SETSIZE, &socket_set, NULL, NULL, &timeout) == SOCKET_ERROR) { |
| 117 | printf("select failed\n"); |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) { |
| 122 | DataSocket* s = *i; |
| 123 | bool socket_done = true; |
| 124 | if (FD_ISSET(s->socket(), &socket_set)) { |
| 125 | if (s->OnDataAvailable(&socket_done) && s->request_received()) { |
| 126 | ChannelMember* member = clients.Lookup(s); |
| 127 | if (member || PeerChannel::IsPeerConnection(s)) { |
| 128 | if (!member) { |
| 129 | if (s->PathEquals("/sign_in")) { |
| 130 | clients.AddMember(s); |
| 131 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 132 | printf("No member found for: %s\n", s->request_path().c_str()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | s->Send("500 Error", true, "text/plain", "", |
| 134 | "Peer most likely gone."); |
| 135 | } |
| 136 | } else if (member->is_wait_request(s)) { |
| 137 | // no need to do anything. |
| 138 | socket_done = false; |
| 139 | } else { |
| 140 | ChannelMember* target = clients.IsTargetedRequest(s); |
| 141 | if (target) { |
| 142 | member->ForwardRequestToPeer(s, target); |
| 143 | } else if (s->PathEquals("/sign_out")) { |
| 144 | s->Send("200 OK", true, "text/plain", "", ""); |
| 145 | } else { |
| 146 | printf("Couldn't find target for request: %s\n", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 147 | s->request_path().c_str()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | s->Send("500 Error", true, "text/plain", "", |
| 149 | "Peer most likely gone."); |
| 150 | } |
| 151 | } |
| 152 | } else { |
| 153 | HandleBrowserRequest(s, &quit); |
| 154 | if (quit) { |
| 155 | printf("Quitting...\n"); |
| 156 | FD_CLR(listener.socket(), &socket_set); |
| 157 | listener.Close(); |
| 158 | clients.CloseAll(); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } else { |
| 163 | socket_done = false; |
| 164 | } |
| 165 | |
| 166 | if (socket_done) { |
| 167 | printf("Disconnecting socket\n"); |
| 168 | clients.OnClosing(s); |
| 169 | assert(s->valid()); // Close must not have been called yet. |
| 170 | FD_CLR(s->socket(), &socket_set); |
| 171 | delete (*i); |
| 172 | i = sockets.erase(i); |
| 173 | if (i == sockets.end()) |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | clients.CheckForTimeout(); |
| 179 | |
| 180 | if (FD_ISSET(listener.socket(), &socket_set)) { |
| 181 | DataSocket* s = listener.Accept(); |
| 182 | if (sockets.size() >= kMaxConnections) { |
| 183 | delete s; // sorry, that's all we can take. |
| 184 | printf("Connection limit reached\n"); |
| 185 | } else { |
| 186 | sockets.push_back(s); |
| 187 | printf("New connection...\n"); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) |
| 193 | delete (*i); |
| 194 | sockets.clear(); |
| 195 | |
| 196 | return 0; |
| 197 | } |