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