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