Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: tools/android/forwarder2/host_controller.cc

Issue 19478003: Remove Thread wrapper class in forwarder2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 } 63 }
64 // When doing dynamically allocation of port, we get the port from the 64 // When doing dynamically allocation of port, we get the port from the
65 // BIND_SUCCESS command we received above. 65 // BIND_SUCCESS command we received above.
66 device_port_ = device_port_allocated; 66 device_port_ = device_port_allocated;
67 ready_ = true; 67 ready_ = true;
68 return true; 68 return true;
69 } 69 }
70 70
71 void HostController::Start() { 71 void HostController::Start() {
72 thread_.Start(); 72 thread_.Start();
73 ReadNextCommandSoon();
74 }
75
76 void HostController::ReadNextCommandSoon() {
73 thread_.message_loop_proxy()->PostTask( 77 thread_.message_loop_proxy()->PostTask(
74 FROM_HERE, 78 FROM_HERE,
75 base::Bind(&HostController::ThreadHandler, base::Unretained(this))); 79 base::Bind(&HostController::ReadCommandOnInternalThread,
80 base::Unretained(this)));
76 } 81 }
77 82
78 void HostController::ThreadHandler() { 83 void HostController::ReadCommandOnInternalThread() {
79 CHECK(ready_) << "HostController not ready. Must call Connect() first."; 84 CHECK(ready_) << "HostController not ready. Must call Connect() first.";
80 while (true) { 85 if (!ReceivedCommand(command::ACCEPT_SUCCESS, &adb_control_socket_)) {
81 if (!ReceivedCommand(command::ACCEPT_SUCCESS, &adb_control_socket_)) { 86 SelfDelete();
82 SelfDelete(); 87 return;
88 }
89 // Try to connect to host server.
90 scoped_ptr<Socket> host_server_data_socket(CreateSocket());
91 if (!host_server_data_socket->ConnectTcp(
92 forward_to_host_, forward_to_host_port_)) {
93 LOG(ERROR) << "Could not Connect HostServerData socket on port: "
94 << forward_to_host_port_;
95 SendCommand(command::HOST_SERVER_ERROR, device_port_, &adb_control_socket_);
96 if (ReceivedCommand(command::ACK, &adb_control_socket_)) {
97 // It can continue if the host forwarder could not connect to the host
98 // server but the device acknowledged that, so that the device could
99 // re-try later.
100 ReadNextCommandSoon();
83 return; 101 return;
84 } 102 }
85 // Try to connect to host server. 103 SelfDelete();
86 scoped_ptr<Socket> host_server_data_socket(CreateSocket()); 104 return;
87 if (!host_server_data_socket->ConnectTcp(
88 forward_to_host_, forward_to_host_port_)) {
89 LOG(ERROR) << "Could not Connect HostServerData socket on port: "
90 << forward_to_host_port_;
91 SendCommand(command::HOST_SERVER_ERROR,
92 device_port_,
93 &adb_control_socket_);
94 if (ReceivedCommand(command::ACK, &adb_control_socket_)) {
95 // It can continue if the host forwarder could not connect to the host
96 // server but the device acknowledged that, so that the device could
97 // re-try later.
98 continue;
99 }
100 SelfDelete();
101 return;
102 }
103 SendCommand(command::HOST_SERVER_SUCCESS,
104 device_port_,
105 &adb_control_socket_);
106 StartForwarder(host_server_data_socket.Pass());
107 } 105 }
106 SendCommand(command::HOST_SERVER_SUCCESS, device_port_, &adb_control_socket_);
107 StartForwarder(host_server_data_socket.Pass());
108 ReadNextCommandSoon();
108 } 109 }
109 110
110 void HostController::StartForwarder( 111 void HostController::StartForwarder(
111 scoped_ptr<Socket> host_server_data_socket) { 112 scoped_ptr<Socket> host_server_data_socket) {
112 scoped_ptr<Socket> adb_data_socket(CreateSocket()); 113 scoped_ptr<Socket> adb_data_socket(CreateSocket());
113 if (!adb_data_socket->ConnectTcp("", adb_port_)) { 114 if (!adb_data_socket->ConnectTcp("", adb_port_)) {
114 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; 115 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_;
115 SelfDelete(); 116 SelfDelete();
116 return; 117 return;
117 } 118 }
118 // Open the Adb data connection, and send a command with the 119 // Open the Adb data connection, and send a command with the
119 // |device_forward_port| as a way for the device to identify the connection. 120 // |device_forward_port| as a way for the device to identify the connection.
120 SendCommand(command::DATA_CONNECTION, 121 SendCommand(command::DATA_CONNECTION,
121 device_port_, 122 device_port_,
122 adb_data_socket.get()); 123 adb_data_socket.get());
123 124
124 // Check that the device received the new Adb Data Connection. Note that this 125 // Check that the device received the new Adb Data Connection. Note that this
125 // check is done through the |adb_control_socket_| that is handled in the 126 // check is done through the |adb_control_socket_| that is handled in the
126 // DeviceListener thread just after the call to WaitForAdbDataSocket(). 127 // DeviceListener thread just after the call to WaitForAdbDataSocket().
127 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, 128 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS,
128 &adb_control_socket_)) { 129 &adb_control_socket_)) {
129 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; 130 LOG(ERROR) << "Device could not handle the new Adb Data Connection.";
130 SelfDelete(); 131 SelfDelete();
131 return; 132 return;
132 } 133 }
133 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), 134 forwarder2::StartForwarder(
134 adb_data_socket.Pass()); 135 host_server_data_socket.Pass(), adb_data_socket.Pass());
135 // Forwarder object will self delete after returning.
136 forwarder->Start();
137 } 136 }
138 137
139 scoped_ptr<Socket> HostController::CreateSocket() { 138 scoped_ptr<Socket> HostController::CreateSocket() {
140 scoped_ptr<Socket> socket(new Socket()); 139 scoped_ptr<Socket> socket(new Socket());
141 socket->AddEventFd(global_exit_notifier_fd_); 140 socket->AddEventFd(global_exit_notifier_fd_);
142 socket->AddEventFd(delete_controller_notifier_.receiver_fd()); 141 socket->AddEventFd(delete_controller_notifier_.receiver_fd());
143 return socket.Pass(); 142 return socket.Pass();
144 } 143 }
145 144
146 void HostController::SelfDelete() { 145 void HostController::SelfDelete() {
(...skipping 12 matching lines...) Expand all
159 LOG(ERROR) << "Could not send unmap command for port " << device_port_; 158 LOG(ERROR) << "Could not send unmap command for port " << device_port_;
160 return; 159 return;
161 } 160 }
162 if (!ReceivedCommand(command::UNMAP_PORT_SUCCESS, &socket)) { 161 if (!ReceivedCommand(command::UNMAP_PORT_SUCCESS, &socket)) {
163 LOG(ERROR) << "Unamp command failed for port " << device_port_; 162 LOG(ERROR) << "Unamp command failed for port " << device_port_;
164 return; 163 return;
165 } 164 }
166 } 165 }
167 166
168 } // namespace forwarder2 167 } // namespace forwarder2
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698