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 <memory> |
7 #include <string> | 8 #include <string> |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "tools/android/forwarder2/command.h" | 15 #include "tools/android/forwarder2/command.h" |
16 #include "tools/android/forwarder2/forwarder.h" | 16 #include "tools/android/forwarder2/forwarder.h" |
17 #include "tools/android/forwarder2/socket.h" | 17 #include "tools/android/forwarder2/socket.h" |
18 | 18 |
19 namespace forwarder2 { | 19 namespace forwarder2 { |
20 | 20 |
21 // static | 21 // static |
22 scoped_ptr<HostController> HostController::Create( | 22 std::unique_ptr<HostController> HostController::Create( |
23 int device_port, | 23 int device_port, |
24 int host_port, | 24 int host_port, |
25 int adb_port, | 25 int adb_port, |
26 int exit_notifier_fd, | 26 int exit_notifier_fd, |
27 const ErrorCallback& error_callback) { | 27 const ErrorCallback& error_callback) { |
28 scoped_ptr<HostController> host_controller; | 28 std::unique_ptr<HostController> host_controller; |
29 scoped_ptr<PipeNotifier> delete_controller_notifier(new PipeNotifier()); | 29 std::unique_ptr<PipeNotifier> delete_controller_notifier(new PipeNotifier()); |
30 scoped_ptr<Socket> adb_control_socket(new Socket()); | 30 std::unique_ptr<Socket> adb_control_socket(new Socket()); |
31 adb_control_socket->AddEventFd(exit_notifier_fd); | 31 adb_control_socket->AddEventFd(exit_notifier_fd); |
32 adb_control_socket->AddEventFd(delete_controller_notifier->receiver_fd()); | 32 adb_control_socket->AddEventFd(delete_controller_notifier->receiver_fd()); |
33 if (!adb_control_socket->ConnectTcp(std::string(), adb_port)) { | 33 if (!adb_control_socket->ConnectTcp(std::string(), adb_port)) { |
34 LOG(ERROR) << "Could not connect HostController socket on port: " | 34 LOG(ERROR) << "Could not connect HostController socket on port: " |
35 << adb_port; | 35 << adb_port; |
36 return host_controller; | 36 return host_controller; |
37 } | 37 } |
38 // Send the command to the device start listening to the "device_forward_port" | 38 // Send the command to the device start listening to the "device_forward_port" |
39 bool send_command_success = SendCommand( | 39 bool send_command_success = SendCommand( |
40 command::LISTEN, device_port, adb_control_socket.get()); | 40 command::LISTEN, device_port, adb_control_socket.get()); |
(...skipping 22 matching lines...) Expand all Loading... |
63 thread_.Start(); | 63 thread_.Start(); |
64 ReadNextCommandSoon(); | 64 ReadNextCommandSoon(); |
65 } | 65 } |
66 | 66 |
67 HostController::HostController( | 67 HostController::HostController( |
68 int device_port, | 68 int device_port, |
69 int host_port, | 69 int host_port, |
70 int adb_port, | 70 int adb_port, |
71 int exit_notifier_fd, | 71 int exit_notifier_fd, |
72 const ErrorCallback& error_callback, | 72 const ErrorCallback& error_callback, |
73 scoped_ptr<Socket> adb_control_socket, | 73 std::unique_ptr<Socket> adb_control_socket, |
74 scoped_ptr<PipeNotifier> delete_controller_notifier) | 74 std::unique_ptr<PipeNotifier> delete_controller_notifier) |
75 : self_deleter_helper_(this, error_callback), | 75 : self_deleter_helper_(this, error_callback), |
76 device_port_(device_port), | 76 device_port_(device_port), |
77 host_port_(host_port), | 77 host_port_(host_port), |
78 adb_port_(adb_port), | 78 adb_port_(adb_port), |
79 global_exit_notifier_fd_(exit_notifier_fd), | 79 global_exit_notifier_fd_(exit_notifier_fd), |
80 adb_control_socket_(std::move(adb_control_socket)), | 80 adb_control_socket_(std::move(adb_control_socket)), |
81 delete_controller_notifier_(std::move(delete_controller_notifier)), | 81 delete_controller_notifier_(std::move(delete_controller_notifier)), |
82 deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 82 deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
83 thread_("HostControllerThread") {} | 83 thread_("HostControllerThread") {} |
84 | 84 |
85 void HostController::ReadNextCommandSoon() { | 85 void HostController::ReadNextCommandSoon() { |
86 thread_.task_runner()->PostTask( | 86 thread_.task_runner()->PostTask( |
87 FROM_HERE, | 87 FROM_HERE, |
88 base::Bind(&HostController::ReadCommandOnInternalThread, | 88 base::Bind(&HostController::ReadCommandOnInternalThread, |
89 base::Unretained(this))); | 89 base::Unretained(this))); |
90 } | 90 } |
91 | 91 |
92 void HostController::ReadCommandOnInternalThread() { | 92 void HostController::ReadCommandOnInternalThread() { |
93 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) { | 93 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) { |
94 LOG(ERROR) << "Did not receive ACCEPT_SUCCESS for port: " | 94 LOG(ERROR) << "Did not receive ACCEPT_SUCCESS for port: " |
95 << host_port_; | 95 << host_port_; |
96 OnInternalThreadError(); | 96 OnInternalThreadError(); |
97 return; | 97 return; |
98 } | 98 } |
99 // Try to connect to host server. | 99 // Try to connect to host server. |
100 scoped_ptr<Socket> host_server_data_socket(new Socket()); | 100 std::unique_ptr<Socket> host_server_data_socket(new Socket()); |
101 if (!host_server_data_socket->ConnectTcp(std::string(), host_port_)) { | 101 if (!host_server_data_socket->ConnectTcp(std::string(), host_port_)) { |
102 LOG(ERROR) << "Could not Connect HostServerData socket on port: " | 102 LOG(ERROR) << "Could not Connect HostServerData socket on port: " |
103 << host_port_; | 103 << host_port_; |
104 SendCommand( | 104 SendCommand( |
105 command::HOST_SERVER_ERROR, device_port_, adb_control_socket_.get()); | 105 command::HOST_SERVER_ERROR, device_port_, adb_control_socket_.get()); |
106 if (ReceivedCommand(command::ACK, adb_control_socket_.get())) { | 106 if (ReceivedCommand(command::ACK, adb_control_socket_.get())) { |
107 // It can continue if the host forwarder could not connect to the host | 107 // It can continue if the host forwarder could not connect to the host |
108 // server but the device acknowledged that, so that the device could | 108 // server but the device acknowledged that, so that the device could |
109 // re-try later. | 109 // re-try later. |
110 ReadNextCommandSoon(); | 110 ReadNextCommandSoon(); |
111 return; | 111 return; |
112 } | 112 } |
113 OnInternalThreadError(); | 113 OnInternalThreadError(); |
114 return; | 114 return; |
115 } | 115 } |
116 LOG(INFO) << "Will send HOST_SERVER_SUCCESS: " << host_port_; | 116 LOG(INFO) << "Will send HOST_SERVER_SUCCESS: " << host_port_; |
117 SendCommand( | 117 SendCommand( |
118 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get()); | 118 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get()); |
119 StartForwarder(std::move(host_server_data_socket)); | 119 StartForwarder(std::move(host_server_data_socket)); |
120 ReadNextCommandSoon(); | 120 ReadNextCommandSoon(); |
121 } | 121 } |
122 | 122 |
123 void HostController::StartForwarder( | 123 void HostController::StartForwarder( |
124 scoped_ptr<Socket> host_server_data_socket) { | 124 std::unique_ptr<Socket> host_server_data_socket) { |
125 scoped_ptr<Socket> adb_data_socket(new Socket()); | 125 std::unique_ptr<Socket> adb_data_socket(new Socket()); |
126 if (!adb_data_socket->ConnectTcp("", adb_port_)) { | 126 if (!adb_data_socket->ConnectTcp("", adb_port_)) { |
127 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; | 127 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; |
128 OnInternalThreadError(); | 128 OnInternalThreadError(); |
129 return; | 129 return; |
130 } | 130 } |
131 // Open the Adb data connection, and send a command with the | 131 // Open the Adb data connection, and send a command with the |
132 // |device_forward_port| as a way for the device to identify the connection. | 132 // |device_forward_port| as a way for the device to identify the connection. |
133 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get()); | 133 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get()); |
134 | 134 |
135 // Check that the device received the new Adb Data Connection. Note that this | 135 // Check that the device received the new Adb Data Connection. Note that this |
(...skipping 24 matching lines...) Expand all Loading... |
160 LOG(ERROR) << "Could not send unmap command for port " << device_port_; | 160 LOG(ERROR) << "Could not send unmap command for port " << device_port_; |
161 return; | 161 return; |
162 } | 162 } |
163 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) { | 163 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) { |
164 LOG(ERROR) << "Unamp command failed for port " << device_port_; | 164 LOG(ERROR) << "Unamp command failed for port " << device_port_; |
165 return; | 165 return; |
166 } | 166 } |
167 } | 167 } |
168 | 168 |
169 } // namespace forwarder2 | 169 } // namespace forwarder2 |
OLD | NEW |