blob: 5214ca5ca551154cf92a1955fece100d1a417189 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Niels Möller433eafe2018-10-04 14:33:20 +020011#include <assert.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "examples/peerconnection/server/data_socket.h"
19#include "examples/peerconnection/server/peer_channel.h"
20#include "examples/peerconnection/server/utils.h"
Bjorn Terelius3e676762018-10-29 15:26:27 +010021#include "rtc_base/flags.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_tools/simple_command_line_parser.h"
Bjorn Terelius3e676762018-10-29 15:26:27 +010023#include "system_wrappers/include/field_trial.h"
24#include "test/field_trial.h"
25
26WEBRTC_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.org28e20752013-07-10 00:45:36 +000034
35static const size_t kMaxConnections = (FD_SETSIZE - 2);
36
37void 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 Raymond1c62ffa2017-12-03 16:45:56 -050062int main(int argc, char* argv[]) {
tkchinbad7b092016-03-11 20:45:16 -080063 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.org28e20752013-07-10 00:45:36 +000074 return 0;
75 }
76
Bjorn Terelius3e676762018-10-29 15:26:27 +010077 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
tkchinbad7b092016-03-11 20:45:16 -080082 int port = strtol((parser.GetFlag("port")).c_str(), NULL, 10);
83
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 // Abort if the user specifies a port that is outside the allowed
85 // range [1, 65535].
tkchinbad7b092016-03-11 20:45:16 -080086 if ((port < 1) || (port > 65535)) {
87 printf("Error: %i is not a valid port.\n", port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 return -1;
89 }
90
91 ListeningSocket listener;
92 if (!listener.Create()) {
93 printf("Failed to create server socket\n");
94 return -1;
tkchinbad7b092016-03-11 20:45:16 -080095 } else if (!listener.Listen(port)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 printf("Failed to listen on server socket\n");
97 return -1;
98 }
99
tkchinbad7b092016-03-11 20:45:16 -0800100 printf("Server listening on port %i\n", port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
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 Gerey665174f2018-06-19 15:03:05 +0200115 struct timeval timeout = {10, 0};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 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 Gerey665174f2018-06-19 15:03:05 +0200132 printf("No member found for: %s\n", s->request_path().c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 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 Gerey665174f2018-06-19 15:03:05 +0200147 s->request_path().c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 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}