| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <signal.h> | 6 #include <signal.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 forwarder2::PipeNotifier* g_notifier; | 32 forwarder2::PipeNotifier* g_notifier; |
| 33 | 33 |
| 34 void KillHandler(int /* unused */) { | 34 void KillHandler(int /* unused */) { |
| 35 static int s_kill_handler_count = 0; | 35 static int s_kill_handler_count = 0; |
| 36 CHECK(g_notifier); | 36 CHECK(g_notifier); |
| 37 // If for some reason the forwarder get stuck in any socket waiting forever, | 37 // If for some reason the forwarder get stuck in any socket waiting forever, |
| 38 // we can send a SIGKILL or SIGINT three times to force it die | 38 // we can send a SIGKILL or SIGINT three times to force it die |
| 39 // (non-nicely). This is useful when debugging. | 39 // (non-nicely). This is useful when debugging. |
| 40 ++s_kill_handler_count; | 40 ++s_kill_handler_count; |
| 41 if (!g_notifier->Notify() || s_kill_handler_count > 2) | 41 if (!g_notifier->Notify() || s_kill_handler_count > 2) |
| 42 exit(-1); | 42 exit(1); |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Format of arg: <Device port>[:<Forward to port>:<Forward to address>] | 45 // Format of arg: <Device port>[:<Forward to port>:<Forward to address>] |
| 46 bool ParseForwardArg(const std::string& arg, | 46 bool ParseForwardArg(const std::string& arg, |
| 47 int* device_port, | 47 int* device_port, |
| 48 std::string* forward_to_host, | 48 std::string* forward_to_host, |
| 49 int* forward_to_port) { | 49 int* forward_to_port) { |
| 50 std::vector<std::string> arg_pieces; | 50 std::vector<std::string> arg_pieces; |
| 51 base::SplitString(arg, ':', &arg_pieces); | 51 base::SplitString(arg, ':', &arg_pieces); |
| 52 if (arg_pieces.size() == 0 || !StringToInt(arg_pieces[0], device_port)) | 52 if (arg_pieces.size() == 0 || !StringToInt(arg_pieces[0], device_port)) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 70 printf("Like 'adb forward' but in the reverse direction\n"); | 70 printf("Like 'adb forward' but in the reverse direction\n"); |
| 71 | 71 |
| 72 CommandLine command_line(argc, argv); | 72 CommandLine command_line(argc, argv); |
| 73 bool show_help = tools::HasHelpSwitch(command_line); | 73 bool show_help = tools::HasHelpSwitch(command_line); |
| 74 std::string adb_port_str = command_line.GetSwitchValueASCII("adb_port"); | 74 std::string adb_port_str = command_line.GetSwitchValueASCII("adb_port"); |
| 75 int adb_port = kDefaultAdbPort; | 75 int adb_port = kDefaultAdbPort; |
| 76 if (!adb_port_str.empty() && !StringToInt(adb_port_str, &adb_port)) { | 76 if (!adb_port_str.empty() && !StringToInt(adb_port_str, &adb_port)) { |
| 77 printf("Could not parse adb port number: %s\n", adb_port_str.c_str()); | 77 printf("Could not parse adb port number: %s\n", adb_port_str.c_str()); |
| 78 show_help = true; | 78 show_help = true; |
| 79 } | 79 } |
| 80 if (adb_port <= 0) { |
| 81 printf("Invalid adb port number: %s. Adb port must be a " |
| 82 "postivie integer.\n", adb_port_str.c_str()); |
| 83 show_help = true; |
| 84 } |
| 80 CommandLine::StringVector forward_args = command_line.GetArgs(); | 85 CommandLine::StringVector forward_args = command_line.GetArgs(); |
| 81 if (show_help || forward_args.empty()) { | 86 if (show_help || forward_args.empty()) { |
| 82 tools::ShowHelp( | 87 tools::ShowHelp( |
| 83 argv[0], | 88 argv[0], |
| 84 "[--adb_port=<adb port>] " | 89 "[--adb_port=<adb port>] " |
| 85 "<Device port>[:<Forward to port>:<Forward to address>] ...", | 90 "<Device port>[:<Forward to port>:<Forward to address>] ...", |
| 86 base::StringPrintf( | 91 base::StringPrintf( |
| 87 " <adb port> is the TCP port Adb is configured to forward to." | 92 " <adb port> is the TCP port Adb is configured to forward to." |
| 88 " Default is %d\n" | 93 " Default is %d\n" |
| 89 " <Forward to port> default is <Device port>\n" | 94 " <Forward to port> default is <Device port>\n" |
| 90 " <Forward to address> default is 127.0.0.1.", | 95 " <Forward to address> default is 127.0.0.1.", |
| 91 kDefaultAdbPort).c_str()); | 96 kDefaultAdbPort).c_str()); |
| 92 return 0; | 97 return 1; |
| 93 } | 98 } |
| 94 | 99 |
| 95 g_notifier = new forwarder2::PipeNotifier(); | 100 g_notifier = new forwarder2::PipeNotifier(); |
| 96 ScopedVector<HostController> controllers; | 101 ScopedVector<HostController> controllers; |
| 97 int failed_count = 0; | 102 int failed_count = 0; |
| 98 for (size_t i = 0; i < forward_args.size(); ++i) { | 103 for (size_t i = 0; i < forward_args.size(); ++i) { |
| 99 int device_port = 0; | 104 int device_port = 0; |
| 100 std::string forward_to_host; | 105 std::string forward_to_host; |
| 101 int forward_to_port = 0; | 106 int forward_to_port = 0; |
| 102 if (ParseForwardArg(forward_args[i], | 107 if (ParseForwardArg(forward_args[i], |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 } | 142 } |
| 138 | 143 |
| 139 // TODO(felipeg): We should check if the controllers are really alive before | 144 // TODO(felipeg): We should check if the controllers are really alive before |
| 140 // printing Ready. | 145 // printing Ready. |
| 141 printf("Host Forwarder Ready.\n"); | 146 printf("Host Forwarder Ready.\n"); |
| 142 for (int i = 0; i < controllers.size(); ++i) | 147 for (int i = 0; i < controllers.size(); ++i) |
| 143 controllers[i]->Join(); | 148 controllers[i]->Join(); |
| 144 | 149 |
| 145 return 0; | 150 return 0; |
| 146 } | 151 } |
| OLD | NEW |