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 "tools/android/forwarder2/host_controller.h" | 5 #include "tools/android/forwarder2/host_controller.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 adb_control_socket_.set_exit_notifier_fd(exit_notifier_fd); | 28 adb_control_socket_.set_exit_notifier_fd(exit_notifier_fd); |
29 } | 29 } |
30 | 30 |
31 HostController::~HostController() { | 31 HostController::~HostController() { |
32 } | 32 } |
33 | 33 |
34 void HostController::StartForwarder( | 34 void HostController::StartForwarder( |
35 scoped_ptr<Socket> host_server_data_socket) { | 35 scoped_ptr<Socket> host_server_data_socket) { |
36 scoped_ptr<Socket> adb_data_socket(new Socket); | 36 scoped_ptr<Socket> adb_data_socket(new Socket); |
37 if (!adb_data_socket->ConnectTcp("", adb_port_)) { | 37 if (!adb_data_socket->ConnectTcp("", adb_port_)) { |
38 LOG(ERROR) << "Could not Connect AdbDataSocket on port: " | 38 LOG(ERROR) << "Could not connect AdbDataSocket on port: " |
39 << adb_port_; | 39 << adb_port_; |
40 return; | 40 return; |
41 } | 41 } |
42 // Open the Adb data connection, and send a command with the | 42 // Open the Adb data connection, and send a command with the |
43 // |device_forward_port| as a way for the device to identify the connection. | 43 // |device_forward_port| as a way for the device to identify the connection. |
44 SendCommand(command::DATA_CONNECTION, | 44 SendCommand(command::DATA_CONNECTION, |
45 device_port_, | 45 device_port_, |
46 adb_data_socket.get()); | 46 adb_data_socket.get()); |
47 | 47 |
48 // Check that the device received the new Adb Data Connection. Observe that | 48 // Check that the device received the new Adb Data Connection. Observe that |
49 // this check is done through the |adb_control_socket_| that is handled in the | 49 // this check is done through the |adb_control_socket_| that is handled in the |
50 // DeviceListener thread just after the call to WaitForAdbDataSocket(). | 50 // DeviceListener thread just after the call to WaitForAdbDataSocket(). |
51 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, | 51 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, |
52 &adb_control_socket_)) { | 52 &adb_control_socket_)) { |
53 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; | 53 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; |
54 host_server_data_socket->Close(); | 54 host_server_data_socket->Close(); |
55 adb_data_socket->Close(); | 55 adb_data_socket->Close(); |
56 return; | 56 return; |
57 } | 57 } |
58 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), | 58 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), |
59 adb_data_socket.Pass()); | 59 adb_data_socket.Pass()); |
60 // Forwarder object will self delete after returning. | 60 // Forwarder object will self delete after returning. |
61 forwarder->Start(); | 61 forwarder->Start(); |
62 } | 62 } |
63 | 63 |
64 bool HostController::Connect() { | 64 bool HostController::Connect() { |
65 if (!adb_control_socket_.ConnectTcp("", adb_port_)) { | 65 if (!adb_control_socket_.ConnectTcp("", adb_port_)) { |
66 LOG(ERROR) << "Could not Connect HostController socket on port: " | 66 LOG(ERROR) << "Could not connect HostController socket on port: " |
67 << adb_port_; | 67 << adb_port_; |
68 return false; | 68 return false; |
69 } | 69 } |
70 // Send the command to the device start listening to the "device_forward_port" | 70 // Send the command to the device start listening to the "device_forward_port" |
71 bool send_command_success = SendCommand( | 71 bool send_command_success = SendCommand( |
72 command::LISTEN, device_port_, &adb_control_socket_); | 72 command::LISTEN, device_port_, &adb_control_socket_); |
73 CHECK(send_command_success); | 73 CHECK(send_command_success); |
74 int device_port_allocated; | 74 int device_port_allocated; |
75 command::Type command; | 75 command::Type command; |
76 if (!ReadCommand(&adb_control_socket_, &device_port_allocated, &command) || | 76 if (!ReadCommand(&adb_control_socket_, &device_port_allocated, &command) || |
77 command != command::BIND_SUCCESS) { | 77 command != command::BIND_SUCCESS) { |
78 LOG(ERROR) << "Device binding error using port " << device_port_; | 78 LOG(ERROR) << "Device binding error using port " << device_port_; |
79 adb_control_socket_.Close(); | 79 adb_control_socket_.Close(); |
80 return false; | 80 return false; |
81 } | 81 } |
82 // When doing dynamically allocation of port, we get the port from the | 82 // When doing dynamically allocation of port, we get the port from the |
83 // BIND_SUCCESS command we received above. | 83 // BIND_SUCCESS command we received above. |
84 device_port_ = device_port_allocated; | 84 device_port_ = device_port_allocated; |
85 ready_ = true; | 85 ready_ = true; |
86 return true; | 86 return true; |
87 } | 87 } |
88 | 88 |
89 void HostController::Run() { | 89 void HostController::Run() { |
90 CHECK(ready_) << "HostController not ready. Must call Connect() first."; | 90 CHECK(ready_) << "HostController not ready. Must call Connect() first."; |
91 while (true) { | 91 while (true) { |
92 if (!ReceivedCommand(command::ACCEPT_SUCCESS, | 92 if (!ReceivedCommand(command::ACCEPT_SUCCESS, |
93 &adb_control_socket_)) { | 93 &adb_control_socket_)) { |
| 94 // TODO(pliard): This can also happen if device_forwarder was |
| 95 // intentionally killed before host_forwarder. In that case, |
| 96 // device_forwarder should send a notification to the host. Currently the |
| 97 // error message below is always emitted to the log file although this is |
| 98 // not necessarily an error. |
94 LOG(ERROR) << "Device socket error on accepting using port " | 99 LOG(ERROR) << "Device socket error on accepting using port " |
95 << device_port_; | 100 << device_port_; |
96 break; | 101 break; |
97 } | 102 } |
98 // Try to connect to host server. | 103 // Try to connect to host server. |
99 scoped_ptr<Socket> host_server_data_socket(new Socket); | 104 scoped_ptr<Socket> host_server_data_socket(new Socket); |
100 host_server_data_socket->set_exit_notifier_fd(exit_notifier_fd_); | 105 host_server_data_socket->set_exit_notifier_fd(exit_notifier_fd_); |
101 if (!host_server_data_socket->ConnectTcp( | 106 if (!host_server_data_socket->ConnectTcp( |
102 forward_to_host_, forward_to_host_port_)) { | 107 forward_to_host_, forward_to_host_port_)) { |
103 LOG(ERROR) << "Could not Connect HostServerData socket on port: " | 108 LOG(ERROR) << "Could not Connect HostServerData socket on port: " |
(...skipping 12 matching lines...) Expand all Loading... |
116 } | 121 } |
117 SendCommand(command::HOST_SERVER_SUCCESS, | 122 SendCommand(command::HOST_SERVER_SUCCESS, |
118 device_port_, | 123 device_port_, |
119 &adb_control_socket_); | 124 &adb_control_socket_); |
120 StartForwarder(host_server_data_socket.Pass()); | 125 StartForwarder(host_server_data_socket.Pass()); |
121 } | 126 } |
122 adb_control_socket_.Close(); | 127 adb_control_socket_.Close(); |
123 } | 128 } |
124 | 129 |
125 } // namespace forwarder2 | 130 } // namespace forwarder2 |
OLD | NEW |