Index: tools/android/forwarder2/host_controller.cc |
diff --git a/tools/android/forwarder2/host_controller.cc b/tools/android/forwarder2/host_controller.cc |
index d98a4ad2e7c7cdc0a8c8ffc0177f6a449f37f163..fcf0e41917bfc974bc9e9b407c6abf26ff899e96 100644 |
--- a/tools/android/forwarder2/host_controller.cc |
+++ b/tools/android/forwarder2/host_controller.cc |
@@ -70,41 +70,42 @@ bool HostController::Connect() { |
void HostController::Start() { |
thread_.Start(); |
+ ReadNextCommandSoon(); |
+} |
+ |
+void HostController::ReadNextCommandSoon() { |
thread_.message_loop_proxy()->PostTask( |
FROM_HERE, |
- base::Bind(&HostController::ThreadHandler, base::Unretained(this))); |
+ base::Bind(&HostController::ReadCommandOnInternalThread, |
+ base::Unretained(this))); |
} |
-void HostController::ThreadHandler() { |
+void HostController::ReadCommandOnInternalThread() { |
CHECK(ready_) << "HostController not ready. Must call Connect() first."; |
- while (true) { |
- if (!ReceivedCommand(command::ACCEPT_SUCCESS, &adb_control_socket_)) { |
- SelfDelete(); |
- return; |
- } |
- // Try to connect to host server. |
- scoped_ptr<Socket> host_server_data_socket(CreateSocket()); |
- if (!host_server_data_socket->ConnectTcp( |
- forward_to_host_, forward_to_host_port_)) { |
- LOG(ERROR) << "Could not Connect HostServerData socket on port: " |
- << forward_to_host_port_; |
- SendCommand(command::HOST_SERVER_ERROR, |
- device_port_, |
- &adb_control_socket_); |
- if (ReceivedCommand(command::ACK, &adb_control_socket_)) { |
- // It can continue if the host forwarder could not connect to the host |
- // server but the device acknowledged that, so that the device could |
- // re-try later. |
- continue; |
- } |
- SelfDelete(); |
+ if (!ReceivedCommand(command::ACCEPT_SUCCESS, &adb_control_socket_)) { |
+ SelfDelete(); |
+ return; |
+ } |
+ // Try to connect to host server. |
+ scoped_ptr<Socket> host_server_data_socket(CreateSocket()); |
+ if (!host_server_data_socket->ConnectTcp( |
+ forward_to_host_, forward_to_host_port_)) { |
+ LOG(ERROR) << "Could not Connect HostServerData socket on port: " |
+ << forward_to_host_port_; |
+ SendCommand(command::HOST_SERVER_ERROR, device_port_, &adb_control_socket_); |
+ if (ReceivedCommand(command::ACK, &adb_control_socket_)) { |
+ // It can continue if the host forwarder could not connect to the host |
+ // server but the device acknowledged that, so that the device could |
+ // re-try later. |
+ ReadNextCommandSoon(); |
return; |
} |
- SendCommand(command::HOST_SERVER_SUCCESS, |
- device_port_, |
- &adb_control_socket_); |
- StartForwarder(host_server_data_socket.Pass()); |
+ SelfDelete(); |
+ return; |
} |
+ SendCommand(command::HOST_SERVER_SUCCESS, device_port_, &adb_control_socket_); |
+ StartForwarder(host_server_data_socket.Pass()); |
+ ReadNextCommandSoon(); |
} |
void HostController::StartForwarder( |
@@ -130,10 +131,8 @@ void HostController::StartForwarder( |
SelfDelete(); |
return; |
} |
- Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), |
- adb_data_socket.Pass()); |
- // Forwarder object will self delete after returning. |
- forwarder->Start(); |
+ forwarder2::StartForwarder( |
+ host_server_data_socket.Pass(), adb_data_socket.Pass()); |
} |
scoped_ptr<Socket> HostController::CreateSocket() { |