OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include <string.h> | 13 #include <string.h> |
14 | 14 |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/examples/peerconnection/server/data_socket.h" | 17 #include "webrtc/examples/peerconnection/server/data_socket.h" |
18 #include "webrtc/examples/peerconnection/server/peer_channel.h" | 18 #include "webrtc/examples/peerconnection/server/peer_channel.h" |
19 #include "webrtc/examples/peerconnection/server/utils.h" | 19 #include "webrtc/examples/peerconnection/server/utils.h" |
20 #include "webrtc/base/flags.h" | 20 #include "webrtc/tools/simple_command_line_parser.h" |
21 | |
22 DEFINE_bool(help, false, "Prints this message"); | |
23 DEFINE_int(port, 8888, "The port on which to listen."); | |
24 | 21 |
25 static const size_t kMaxConnections = (FD_SETSIZE - 2); | 22 static const size_t kMaxConnections = (FD_SETSIZE - 2); |
26 | 23 |
27 void HandleBrowserRequest(DataSocket* ds, bool* quit) { | 24 void HandleBrowserRequest(DataSocket* ds, bool* quit) { |
28 assert(ds && ds->valid()); | 25 assert(ds && ds->valid()); |
29 assert(quit); | 26 assert(quit); |
30 | 27 |
31 const std::string& path = ds->request_path(); | 28 const std::string& path = ds->request_path(); |
32 | 29 |
33 *quit = (path.compare("/quit") == 0); | 30 *quit = (path.compare("/quit") == 0); |
34 | 31 |
35 if (*quit) { | 32 if (*quit) { |
36 ds->Send("200 OK", true, "text/html", "", | 33 ds->Send("200 OK", true, "text/html", "", |
37 "<html><body>Quitting...</body></html>"); | 34 "<html><body>Quitting...</body></html>"); |
38 } else if (ds->method() == DataSocket::OPTIONS) { | 35 } else if (ds->method() == DataSocket::OPTIONS) { |
39 // We'll get this when a browsers do cross-resource-sharing requests. | 36 // We'll get this when a browsers do cross-resource-sharing requests. |
40 // The headers to allow cross-origin script support will be set inside | 37 // The headers to allow cross-origin script support will be set inside |
41 // Send. | 38 // Send. |
42 ds->Send("200 OK", true, "", "", ""); | 39 ds->Send("200 OK", true, "", "", ""); |
43 } else { | 40 } else { |
44 // Here we could write some useful output back to the browser depending on | 41 // Here we could write some useful output back to the browser depending on |
45 // the path. | 42 // the path. |
46 printf("Received an invalid request: %s\n", ds->request_path().c_str()); | 43 printf("Received an invalid request: %s\n", ds->request_path().c_str()); |
47 ds->Send("500 Sorry", true, "text/html", "", | 44 ds->Send("500 Sorry", true, "text/html", "", |
48 "<html><body>Sorry, not yet implemented</body></html>"); | 45 "<html><body>Sorry, not yet implemented</body></html>"); |
49 } | 46 } |
50 } | 47 } |
51 | 48 |
52 int main(int argc, char** argv) { | 49 int main(int argc, char** argv) { |
53 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true); | 50 std::string program_name = argv[0]; |
54 if (FLAG_help) { | 51 std::string usage = "Example usage: " + program_name + " --port=8888"; |
55 rtc::FlagList::Print(NULL, false); | 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(); |
56 return 0; | 61 return 0; |
57 } | 62 } |
58 | 63 |
| 64 int port = strtol((parser.GetFlag("port")).c_str(), NULL, 10); |
| 65 |
59 // Abort if the user specifies a port that is outside the allowed | 66 // Abort if the user specifies a port that is outside the allowed |
60 // range [1, 65535]. | 67 // range [1, 65535]. |
61 if ((FLAG_port < 1) || (FLAG_port > 65535)) { | 68 if ((port < 1) || (port > 65535)) { |
62 printf("Error: %i is not a valid port.\n", FLAG_port); | 69 printf("Error: %i is not a valid port.\n", port); |
63 return -1; | 70 return -1; |
64 } | 71 } |
65 | 72 |
66 ListeningSocket listener; | 73 ListeningSocket listener; |
67 if (!listener.Create()) { | 74 if (!listener.Create()) { |
68 printf("Failed to create server socket\n"); | 75 printf("Failed to create server socket\n"); |
69 return -1; | 76 return -1; |
70 } else if (!listener.Listen(FLAG_port)) { | 77 } else if (!listener.Listen(port)) { |
71 printf("Failed to listen on server socket\n"); | 78 printf("Failed to listen on server socket\n"); |
72 return -1; | 79 return -1; |
73 } | 80 } |
74 | 81 |
75 printf("Server listening on port %i\n", FLAG_port); | 82 printf("Server listening on port %i\n", port); |
76 | 83 |
77 PeerChannel clients; | 84 PeerChannel clients; |
78 typedef std::vector<DataSocket*> SocketArray; | 85 typedef std::vector<DataSocket*> SocketArray; |
79 SocketArray sockets; | 86 SocketArray sockets; |
80 bool quit = false; | 87 bool quit = false; |
81 while (!quit) { | 88 while (!quit) { |
82 fd_set socket_set; | 89 fd_set socket_set; |
83 FD_ZERO(&socket_set); | 90 FD_ZERO(&socket_set); |
84 if (listener.valid()) | 91 if (listener.valid()) |
85 FD_SET(listener.socket(), &socket_set); | 92 FD_SET(listener.socket(), &socket_set); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 171 } |
165 } | 172 } |
166 } | 173 } |
167 | 174 |
168 for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) | 175 for (SocketArray::iterator i = sockets.begin(); i != sockets.end(); ++i) |
169 delete (*i); | 176 delete (*i); |
170 sockets.clear(); | 177 sockets.clear(); |
171 | 178 |
172 return 0; | 179 return 0; |
173 } | 180 } |
OLD | NEW |