| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 154 |
| 155 void HandleRequestOnInternalThread(const std::string& adb_path, | 155 void HandleRequestOnInternalThread(const std::string& adb_path, |
| 156 const std::string& device_serial, | 156 const std::string& device_serial, |
| 157 int device_port, | 157 int device_port, |
| 158 int host_port, | 158 int host_port, |
| 159 std::unique_ptr<Socket> client_socket) { | 159 std::unique_ptr<Socket> client_socket) { |
| 160 const int adb_port = GetAdbPortForDevice(adb_path, device_serial); | 160 const int adb_port = GetAdbPortForDevice(adb_path, device_serial); |
| 161 if (adb_port < 0) { | 161 if (adb_port < 0) { |
| 162 SendMessage( | 162 SendMessage( |
| 163 "ERROR: could not get adb port for device. You might need to add " | 163 "ERROR: could not get adb port for device. You might need to add " |
| 164 "'adb' to your PATH or provide the device serial id.", | 164 "'adb' to your PATH or provide the device serial id.\n", |
| 165 client_socket.get()); | 165 client_socket.get()); |
| 166 return; | 166 return; |
| 167 } | 167 } |
| 168 if (device_port < 0) { | 168 if (device_port < 0) { |
| 169 // Remove the previously created host controller. | 169 // Remove the previously created host controller. |
| 170 const std::string controller_key = MakeHostControllerMapKey( | 170 const std::string controller_key = MakeHostControllerMapKey( |
| 171 adb_port, -device_port); | 171 adb_port, -device_port); |
| 172 const bool controller_did_exist = DeleteRefCountedValueInMap( | 172 const bool controller_did_exist = DeleteRefCountedValueInMap( |
| 173 controller_key, controllers_.get()); | 173 controller_key, controllers_.get()); |
| 174 if (!controller_did_exist) { | 174 if (!controller_did_exist) { |
| 175 SendMessage("ERROR: could not unmap port.", client_socket.get()); | 175 SendMessage("ERROR: could not unmap port.\n", client_socket.get()); |
| 176 LogExistingControllers(client_socket); | 176 LogExistingControllers(client_socket); |
| 177 } else { | 177 } else { |
| 178 SendMessage("OK", client_socket.get()); | 178 SendMessage("OK", client_socket.get()); |
| 179 } | 179 } |
| 180 | 180 |
| 181 RemoveAdbPortForDeviceIfNeeded(adb_path, device_serial); | 181 RemoveAdbPortForDeviceIfNeeded(adb_path, device_serial); |
| 182 return; | 182 return; |
| 183 } | 183 } |
| 184 if (host_port < 0) { | 184 if (host_port < 0) { |
| 185 SendMessage("ERROR: missing host port", client_socket.get()); | 185 SendMessage("ERROR: missing host port\n", client_socket.get()); |
| 186 return; | 186 return; |
| 187 } | 187 } |
| 188 const bool use_dynamic_port_allocation = device_port == 0; | 188 const bool use_dynamic_port_allocation = device_port == 0; |
| 189 if (!use_dynamic_port_allocation) { | 189 if (!use_dynamic_port_allocation) { |
| 190 const std::string controller_key = MakeHostControllerMapKey( | 190 const std::string controller_key = MakeHostControllerMapKey( |
| 191 adb_port, device_port); | 191 adb_port, device_port); |
| 192 if (controllers_->find(controller_key) != controllers_->end()) { | 192 if (controllers_->find(controller_key) != controllers_->end()) { |
| 193 LOG(INFO) << "Already forwarding device port " << device_port | 193 LOG(INFO) << "Already forwarding device port " << device_port |
| 194 << " to host port " << host_port; | 194 << " to host port " << host_port; |
| 195 SendMessage(base::StringPrintf("%d:%d", device_port, host_port), | 195 SendMessage(base::StringPrintf("%d:%d", device_port, host_port), |
| 196 client_socket.get()); | 196 client_socket.get()); |
| 197 return; | 197 return; |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 // Create a new host controller. | 200 // Create a new host controller. |
| 201 std::unique_ptr<HostController> host_controller(HostController::Create( | 201 std::unique_ptr<HostController> host_controller(HostController::Create( |
| 202 device_port, host_port, adb_port, GetExitNotifierFD(), | 202 device_port, host_port, adb_port, GetExitNotifierFD(), |
| 203 base::Bind(&HostControllersManager::DeleteHostController, | 203 base::Bind(&HostControllersManager::DeleteHostController, |
| 204 weak_ptr_factory_.GetWeakPtr()))); | 204 weak_ptr_factory_.GetWeakPtr()))); |
| 205 if (!host_controller.get()) { | 205 if (!host_controller.get()) { |
| 206 has_failed_ = true; | 206 has_failed_ = true; |
| 207 SendMessage("ERROR: Connection to device failed.", client_socket.get()); | 207 SendMessage("ERROR: Connection to device failed.\n", client_socket.get()); |
| 208 LogExistingControllers(client_socket); | 208 LogExistingControllers(client_socket); |
| 209 return; | 209 return; |
| 210 } | 210 } |
| 211 // Get the current allocated port. | 211 // Get the current allocated port. |
| 212 device_port = host_controller->device_port(); | 212 device_port = host_controller->device_port(); |
| 213 LOG(INFO) << "Forwarding device port " << device_port << " to host port " | 213 LOG(INFO) << "Forwarding device port " << device_port << " to host port " |
| 214 << host_port; | 214 << host_port; |
| 215 const std::string msg = base::StringPrintf("%d:%d", device_port, host_port); | 215 const std::string msg = base::StringPrintf("%d:%d", device_port, host_port); |
| 216 if (!SendMessage(msg, client_socket.get())) | 216 if (!SendMessage(msg, client_socket.get())) |
| 217 return; | 217 return; |
| 218 host_controller->Start(); | 218 host_controller->Start(); |
| 219 controllers_->insert( | 219 controllers_->insert( |
| 220 std::make_pair(MakeHostControllerMapKey(adb_port, device_port), | 220 std::make_pair(MakeHostControllerMapKey(adb_port, device_port), |
| 221 linked_ptr<HostController>(host_controller.release()))); | 221 linked_ptr<HostController>(host_controller.release()))); |
| 222 } | 222 } |
| 223 | 223 |
| 224 void LogExistingControllers(const std::unique_ptr<Socket>& client_socket) { | 224 void LogExistingControllers(const std::unique_ptr<Socket>& client_socket) { |
| 225 SendMessage("ERROR: Existing controllers:", client_socket.get()); | 225 SendMessage("ERROR: Existing controllers:\n", client_socket.get()); |
| 226 for (const auto& controller : *controllers_) { | 226 for (const auto& controller : *controllers_) { |
| 227 SendMessage(base::StringPrintf("ERROR: %s", controller.first.c_str()), | 227 SendMessage(base::StringPrintf("ERROR: %s\n", controller.first.c_str()), |
| 228 client_socket.get()); | 228 client_socket.get()); |
| 229 } | 229 } |
| 230 } | 230 } |
| 231 | 231 |
| 232 void RemoveAdbPortForDeviceIfNeeded(const std::string& adb_path, | 232 void RemoveAdbPortForDeviceIfNeeded(const std::string& adb_path, |
| 233 const std::string& device_serial) { | 233 const std::string& device_serial) { |
| 234 base::hash_map<std::string, int>::const_iterator it = | 234 base::hash_map<std::string, int>::const_iterator it = |
| 235 device_serial_to_adb_port_map_.find(device_serial); | 235 device_serial_to_adb_port_map_.find(device_serial); |
| 236 if (it == device_serial_to_adb_port_map_.end()) | 236 if (it == device_serial_to_adb_port_map_.end()) |
| 237 return; | 237 return; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 PError("Read()"); | 343 PError("Read()"); |
| 344 has_failed_ = true; | 344 has_failed_ = true; |
| 345 return; | 345 return; |
| 346 } | 346 } |
| 347 const base::Pickle command_pickle(buf, bytes_read); | 347 const base::Pickle command_pickle(buf, bytes_read); |
| 348 base::PickleIterator pickle_it(command_pickle); | 348 base::PickleIterator pickle_it(command_pickle); |
| 349 std::string device_serial; | 349 std::string device_serial; |
| 350 CHECK(pickle_it.ReadString(&device_serial)); | 350 CHECK(pickle_it.ReadString(&device_serial)); |
| 351 int device_port; | 351 int device_port; |
| 352 if (!pickle_it.ReadInt(&device_port)) { | 352 if (!pickle_it.ReadInt(&device_port)) { |
| 353 client_socket->WriteString("ERROR: missing device port"); | 353 client_socket->WriteString("ERROR: missing device port\n"); |
| 354 return; | 354 return; |
| 355 } | 355 } |
| 356 int host_port; | 356 int host_port; |
| 357 if (!pickle_it.ReadInt(&host_port)) | 357 if (!pickle_it.ReadInt(&host_port)) |
| 358 host_port = -1; | 358 host_port = -1; |
| 359 controllers_manager_.HandleRequest(adb_path_, device_serial, device_port, | 359 controllers_manager_.HandleRequest(adb_path_, device_serial, device_port, |
| 360 host_port, std::move(client_socket)); | 360 host_port, std::move(client_socket)); |
| 361 } | 361 } |
| 362 | 362 |
| 363 private: | 363 private: |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 | 471 |
| 472 return client_delegate.has_failed() || daemon_delegate.has_failed(); | 472 return client_delegate.has_failed() || daemon_delegate.has_failed(); |
| 473 } | 473 } |
| 474 | 474 |
| 475 } // namespace | 475 } // namespace |
| 476 } // namespace forwarder2 | 476 } // namespace forwarder2 |
| 477 | 477 |
| 478 int main(int argc, char** argv) { | 478 int main(int argc, char** argv) { |
| 479 return forwarder2::RunHostForwarder(argc, argv); | 479 return forwarder2::RunHostForwarder(argc, argv); |
| 480 } | 480 } |
| OLD | NEW |