blob: 9be768566053059854e66f14700db69754ae6366 [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>
Yves Gerey3e707812018-11-28 16:47:49 +010014#if defined(WEBRTC_POSIX)
15#include <sys/select.h>
16#endif
17#include <time.h>
Jonas Olsson5b2eda42019-06-11 14:29:40 +020018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include <string>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020#include <vector>
21
Mirko Bonadei04cffe32019-06-25 15:43:23 +020022#include "absl/flags/flag.h"
23#include "absl/flags/parse.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "examples/peerconnection/server/data_socket.h"
25#include "examples/peerconnection/server/peer_channel.h"
Bjorn Terelius3e676762018-10-29 15:26:27 +010026#include "system_wrappers/include/field_trial.h"
27#include "test/field_trial.h"
28
Mirko Bonadei04cffe32019-06-25 15:43:23 +020029ABSL_FLAG(
30 std::string,
Bjorn Terelius3e676762018-10-29 15:26:27 +010031 force_fieldtrials,
32 "",
33 "Field trials control experimental features. This flag specifies the field "
34 "trials in effect. E.g. running with "
35 "--force_fieldtrials=WebRTC-FooFeature/Enabled/ "
36 "will assign the group Enabled to field trial WebRTC-FooFeature. Multiple "
37 "trials are separated by \"/\"");
Mirko Bonadei04cffe32019-06-25 15:43:23 +020038ABSL_FLAG(int, port, 8888, "default: 8888");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40static const size_t kMaxConnections = (FD_SETSIZE - 2);
41
42void HandleBrowserRequest(DataSocket* ds, bool* quit) {
43 assert(ds && ds->valid());
44 assert(quit);
45
46 const std::string& path = ds->request_path();
47
48 *quit = (path.compare("/quit") == 0);
49
50 if (*quit) {
51 ds->Send("200 OK", true, "text/html", "",
52 "<html><body>Quitting...</body></html>");
53 } else if (ds->method() == DataSocket::OPTIONS) {
54 // We'll get this when a browsers do cross-resource-sharing requests.
55 // The headers to allow cross-origin script support will be set inside
56 // Send.
57 ds->Send("200 OK", true, "", "", "");
58 } else {
59 // Here we could write some useful output back to the browser depending on
60 // the path.
61 printf("Received an invalid request: %s\n", ds->request_path().c_str());
62 ds->Send("500 Sorry", true, "text/html", "",
63 "<html><body>Sorry, not yet implemented</body></html>");
64 }
65}
66
Robin Raymond1c62ffa2017-12-03 16:45:56 -050067int main(int argc, char* argv[]) {
Mirko Bonadei04cffe32019-06-25 15:43:23 +020068 absl::ParseCommandLine(argc, argv);
69 // TODO(bugs.webrtc.org/10616): Add program usage message when Abseil
70 // flags supports it.
71 // std::string usage = "Example usage: " + program_name + " --port=8888";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
Bjorn Terelius3e676762018-10-29 15:26:27 +010073 // InitFieldTrialsFromString stores the char*, so the char array must outlive
74 // the application.
Mirko Bonadei04cffe32019-06-25 15:43:23 +020075 webrtc::field_trial::InitFieldTrialsFromString(
76 absl::GetFlag(FLAGS_force_fieldtrials).c_str());
Bjorn Terelius3e676762018-10-29 15:26:27 +010077
Mirko Bonadei04cffe32019-06-25 15:43:23 +020078 int port = absl::GetFlag(FLAGS_port);
tkchinbad7b092016-03-11 20:45:16 -080079
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 // Abort if the user specifies a port that is outside the allowed
81 // range [1, 65535].
tkchinbad7b092016-03-11 20:45:16 -080082 if ((port < 1) || (port > 65535)) {
83 printf("Error: %i is not a valid port.\n", port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 return -1;
85 }
86
87 ListeningSocket listener;
88 if (!listener.Create()) {
89 printf("Failed to create server socket\n");
90 return -1;
tkchinbad7b092016-03-11 20:45:16 -080091 } else if (!listener.Listen(port)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 printf("Failed to listen on server socket\n");
93 return -1;
94 }
95
tkchinbad7b092016-03-11 20:45:16 -080096 printf("Server listening on port %i\n", port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
98 PeerChannel clients;
99 typedef std::vector<DataSocket*> SocketArray;
100 SocketArray sockets;
101 bool quit = false;
102 while (!quit) {
103 fd_set socket_set;
104 FD_ZERO(&socket_set);
105 if (listener.valid())
106 FD_SET(listener.socket(), &socket_set);
107
108 for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i)
109 FD_SET((*i)->socket(), &socket_set);
110
Yves Gerey665174f2018-06-19 15:03:05 +0200111 struct timeval timeout = {10, 0};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 if (select(FD_SETSIZE, &socket_set, NULL, NULL, &timeout) == SOCKET_ERROR) {
113 printf("select failed\n");
114 break;
115 }
116
117 for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) {
118 DataSocket* s = *i;
119 bool socket_done = true;
120 if (FD_ISSET(s->socket(), &socket_set)) {
121 if (s->OnDataAvailable(&socket_done) && s->request_received()) {
122 ChannelMember* member = clients.Lookup(s);
123 if (member || PeerChannel::IsPeerConnection(s)) {
124 if (!member) {
125 if (s->PathEquals("/sign_in")) {
126 clients.AddMember(s);
127 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200128 printf("No member found for: %s\n", s->request_path().c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 s->Send("500 Error", true, "text/plain", "",
130 "Peer most likely gone.");
131 }
132 } else if (member->is_wait_request(s)) {
133 // no need to do anything.
134 socket_done = false;
135 } else {
136 ChannelMember* target = clients.IsTargetedRequest(s);
137 if (target) {
138 member->ForwardRequestToPeer(s, target);
139 } else if (s->PathEquals("/sign_out")) {
140 s->Send("200 OK", true, "text/plain", "", "");
141 } else {
142 printf("Couldn't find target for request: %s\n",
Yves Gerey665174f2018-06-19 15:03:05 +0200143 s->request_path().c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 s->Send("500 Error", true, "text/plain", "",
145 "Peer most likely gone.");
146 }
147 }
148 } else {
149 HandleBrowserRequest(s, &quit);
150 if (quit) {
151 printf("Quitting...\n");
152 FD_CLR(listener.socket(), &socket_set);
153 listener.Close();
154 clients.CloseAll();
155 }
156 }
157 }
158 } else {
159 socket_done = false;
160 }
161
162 if (socket_done) {
163 printf("Disconnecting socket\n");
164 clients.OnClosing(s);
165 assert(s->valid()); // Close must not have been called yet.
166 FD_CLR(s->socket(), &socket_set);
167 delete (*i);
168 i = sockets.erase(i);
169 if (i == sockets.end())
170 break;
171 }
172 }
173
174 clients.CheckForTimeout();
175
176 if (FD_ISSET(listener.socket(), &socket_set)) {
177 DataSocket* s = listener.Accept();
178 if (sockets.size() >= kMaxConnections) {
179 delete s; // sorry, that's all we can take.
180 printf("Connection limit reached\n");
181 } else {
182 sockets.push_back(s);
183 printf("New connection...\n");
184 }
185 }
186 }
187
188 for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i)
189 delete (*i);
190 sockets.clear();
191
192 return 0;
193}