| 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 <sys/types.h> | 7 #include <sys/types.h> |
| 8 #include <sys/wait.h> | 8 #include <sys/wait.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/command_line.h" | 21 #include "base/command_line.h" |
| 22 #include "base/compiler_specific.h" | 22 #include "base/compiler_specific.h" |
| 23 #include "base/containers/hash_tables.h" | 23 #include "base/containers/hash_tables.h" |
| 24 #include "base/files/file_path.h" | 24 #include "base/files/file_path.h" |
| 25 #include "base/files/file_util.h" | 25 #include "base/files/file_util.h" |
| 26 #include "base/logging.h" | 26 #include "base/logging.h" |
| 27 #include "base/memory/linked_ptr.h" | 27 #include "base/memory/linked_ptr.h" |
| 28 #include "base/memory/scoped_vector.h" | 28 #include "base/memory/scoped_vector.h" |
| 29 #include "base/memory/weak_ptr.h" | 29 #include "base/memory/weak_ptr.h" |
| 30 #include "base/pickle.h" | 30 #include "base/pickle.h" |
| 31 #include "base/safe_strerror_posix.h" | |
| 32 #include "base/strings/string_number_conversions.h" | 31 #include "base/strings/string_number_conversions.h" |
| 33 #include "base/strings/string_piece.h" | 32 #include "base/strings/string_piece.h" |
| 34 #include "base/strings/string_split.h" | 33 #include "base/strings/string_split.h" |
| 35 #include "base/strings/string_util.h" | 34 #include "base/strings/string_util.h" |
| 36 #include "base/strings/stringprintf.h" | 35 #include "base/strings/stringprintf.h" |
| 37 #include "base/task_runner.h" | 36 #include "base/task_runner.h" |
| 38 #include "base/threading/thread.h" | 37 #include "base/threading/thread.h" |
| 39 #include "tools/android/forwarder2/common.h" | 38 #include "tools/android/forwarder2/common.h" |
| 40 #include "tools/android/forwarder2/daemon.h" | 39 #include "tools/android/forwarder2/daemon.h" |
| 41 #include "tools/android/forwarder2/host_controller.h" | 40 #include "tools/android/forwarder2/host_controller.h" |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 void OnClientConnected(scoped_ptr<Socket> client_socket) override { | 324 void OnClientConnected(scoped_ptr<Socket> client_socket) override { |
| 326 char buf[kBufSize]; | 325 char buf[kBufSize]; |
| 327 const int bytes_read = client_socket->Read(buf, sizeof(buf)); | 326 const int bytes_read = client_socket->Read(buf, sizeof(buf)); |
| 328 if (bytes_read <= 0) { | 327 if (bytes_read <= 0) { |
| 329 if (client_socket->DidReceiveEvent()) | 328 if (client_socket->DidReceiveEvent()) |
| 330 return; | 329 return; |
| 331 PError("Read()"); | 330 PError("Read()"); |
| 332 has_failed_ = true; | 331 has_failed_ = true; |
| 333 return; | 332 return; |
| 334 } | 333 } |
| 335 const Pickle command_pickle(buf, bytes_read); | 334 const base::Pickle command_pickle(buf, bytes_read); |
| 336 PickleIterator pickle_it(command_pickle); | 335 base::PickleIterator pickle_it(command_pickle); |
| 337 std::string device_serial; | 336 std::string device_serial; |
| 338 CHECK(pickle_it.ReadString(&device_serial)); | 337 CHECK(pickle_it.ReadString(&device_serial)); |
| 339 int device_port; | 338 int device_port; |
| 340 if (!pickle_it.ReadInt(&device_port)) { | 339 if (!pickle_it.ReadInt(&device_port)) { |
| 341 client_socket->WriteString("ERROR: missing device port"); | 340 client_socket->WriteString("ERROR: missing device port"); |
| 342 return; | 341 return; |
| 343 } | 342 } |
| 344 int host_port; | 343 int host_port; |
| 345 if (!pickle_it.ReadInt(&host_port)) | 344 if (!pickle_it.ReadInt(&host_port)) |
| 346 host_port = -1; | 345 host_port = -1; |
| 347 controllers_manager_.HandleRequest(adb_path_, device_serial, device_port, | 346 controllers_manager_.HandleRequest(adb_path_, device_serial, device_port, |
| 348 host_port, client_socket.Pass()); | 347 host_port, client_socket.Pass()); |
| 349 } | 348 } |
| 350 | 349 |
| 351 private: | 350 private: |
| 352 std::string adb_path_; | 351 std::string adb_path_; |
| 353 bool has_failed_; | 352 bool has_failed_; |
| 354 HostControllersManager controllers_manager_; | 353 HostControllersManager controllers_manager_; |
| 355 | 354 |
| 356 DISALLOW_COPY_AND_ASSIGN(ServerDelegate); | 355 DISALLOW_COPY_AND_ASSIGN(ServerDelegate); |
| 357 }; | 356 }; |
| 358 | 357 |
| 359 class ClientDelegate : public Daemon::ClientDelegate { | 358 class ClientDelegate : public Daemon::ClientDelegate { |
| 360 public: | 359 public: |
| 361 ClientDelegate(const Pickle& command_pickle) | 360 ClientDelegate(const base::Pickle& command_pickle) |
| 362 : command_pickle_(command_pickle), | 361 : command_pickle_(command_pickle), has_failed_(false) {} |
| 363 has_failed_(false) { | |
| 364 } | |
| 365 | 362 |
| 366 bool has_failed() const { return has_failed_; } | 363 bool has_failed() const { return has_failed_; } |
| 367 | 364 |
| 368 // Daemon::ClientDelegate: | 365 // Daemon::ClientDelegate: |
| 369 void OnDaemonReady(Socket* daemon_socket) override { | 366 void OnDaemonReady(Socket* daemon_socket) override { |
| 370 // Send the forward command to the daemon. | 367 // Send the forward command to the daemon. |
| 371 CHECK_EQ(static_cast<long>(command_pickle_.size()), | 368 CHECK_EQ(static_cast<long>(command_pickle_.size()), |
| 372 daemon_socket->WriteNumBytes(command_pickle_.data(), | 369 daemon_socket->WriteNumBytes(command_pickle_.data(), |
| 373 command_pickle_.size())); | 370 command_pickle_.size())); |
| 374 char buf[kBufSize]; | 371 char buf[kBufSize]; |
| 375 const int bytes_read = daemon_socket->Read( | 372 const int bytes_read = daemon_socket->Read( |
| 376 buf, sizeof(buf) - 1 /* leave space for null terminator */); | 373 buf, sizeof(buf) - 1 /* leave space for null terminator */); |
| 377 CHECK_GT(bytes_read, 0); | 374 CHECK_GT(bytes_read, 0); |
| 378 DCHECK(static_cast<size_t>(bytes_read) < sizeof(buf)); | 375 DCHECK(static_cast<size_t>(bytes_read) < sizeof(buf)); |
| 379 buf[bytes_read] = 0; | 376 buf[bytes_read] = 0; |
| 380 base::StringPiece msg(buf, bytes_read); | 377 base::StringPiece msg(buf, bytes_read); |
| 381 if (msg.starts_with("ERROR")) { | 378 if (msg.starts_with("ERROR")) { |
| 382 LOG(ERROR) << msg; | 379 LOG(ERROR) << msg; |
| 383 has_failed_ = true; | 380 has_failed_ = true; |
| 384 return; | 381 return; |
| 385 } | 382 } |
| 386 printf("%s\n", buf); | 383 printf("%s\n", buf); |
| 387 } | 384 } |
| 388 | 385 |
| 389 private: | 386 private: |
| 390 const Pickle command_pickle_; | 387 const base::Pickle command_pickle_; |
| 391 bool has_failed_; | 388 bool has_failed_; |
| 392 }; | 389 }; |
| 393 | 390 |
| 394 void ExitWithUsage() { | 391 void ExitWithUsage() { |
| 395 std::cerr << "Usage: host_forwarder [options]\n\n" | 392 std::cerr << "Usage: host_forwarder [options]\n\n" |
| 396 "Options:\n" | 393 "Options:\n" |
| 397 " --serial-id=[0-9A-Z]{16}]\n" | 394 " --serial-id=[0-9A-Z]{16}]\n" |
| 398 " --map DEVICE_PORT HOST_PORT\n" | 395 " --map DEVICE_PORT HOST_PORT\n" |
| 399 " --unmap DEVICE_PORT\n" | 396 " --unmap DEVICE_PORT\n" |
| 400 " --adb PATH_TO_ADB\n" | 397 " --adb PATH_TO_ADB\n" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 412 } | 409 } |
| 413 return value; | 410 return value; |
| 414 } | 411 } |
| 415 | 412 |
| 416 int RunHostForwarder(int argc, char** argv) { | 413 int RunHostForwarder(int argc, char** argv) { |
| 417 base::CommandLine::Init(argc, argv); | 414 base::CommandLine::Init(argc, argv); |
| 418 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess(); | 415 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess(); |
| 419 std::string adb_path = "adb"; | 416 std::string adb_path = "adb"; |
| 420 bool kill_server = false; | 417 bool kill_server = false; |
| 421 | 418 |
| 422 Pickle pickle; | 419 base::Pickle pickle; |
| 423 pickle.WriteString( | 420 pickle.WriteString( |
| 424 cmd_line.HasSwitch("serial-id") ? | 421 cmd_line.HasSwitch("serial-id") ? |
| 425 cmd_line.GetSwitchValueASCII("serial-id") : std::string()); | 422 cmd_line.GetSwitchValueASCII("serial-id") : std::string()); |
| 426 | 423 |
| 427 const std::vector<std::string> args = cmd_line.GetArgs(); | 424 const std::vector<std::string> args = cmd_line.GetArgs(); |
| 428 if (cmd_line.HasSwitch("kill-server")) { | 425 if (cmd_line.HasSwitch("kill-server")) { |
| 429 kill_server = true; | 426 kill_server = true; |
| 430 } else if (cmd_line.HasSwitch("unmap")) { | 427 } else if (cmd_line.HasSwitch("unmap")) { |
| 431 if (args.size() != 1) | 428 if (args.size() != 1) |
| 432 ExitWithUsage(); | 429 ExitWithUsage(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 461 | 458 |
| 462 return client_delegate.has_failed() || daemon_delegate.has_failed(); | 459 return client_delegate.has_failed() || daemon_delegate.has_failed(); |
| 463 } | 460 } |
| 464 | 461 |
| 465 } // namespace | 462 } // namespace |
| 466 } // namespace forwarder2 | 463 } // namespace forwarder2 |
| 467 | 464 |
| 468 int main(int argc, char** argv) { | 465 int main(int argc, char** argv) { |
| 469 return forwarder2::RunHostForwarder(argc, argv); | 466 return forwarder2::RunHostForwarder(argc, argv); |
| 470 } | 467 } |
| OLD | NEW |