| 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/device_controller.h" | 5 #include "tools/android/forwarder2/device_controller.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 bool DeviceController::Init(const std::string& adb_unix_socket) { | 48 bool DeviceController::Init(const std::string& adb_unix_socket) { |
| 49 if (!kickstart_adb_socket_.BindUnix(adb_unix_socket, | 49 if (!kickstart_adb_socket_.BindUnix(adb_unix_socket, |
| 50 true /* abstract */)) { | 50 true /* abstract */)) { |
| 51 LOG(ERROR) << "Could not BindAndListen DeviceController socket on port " | 51 LOG(ERROR) << "Could not BindAndListen DeviceController socket on port " |
| 52 << adb_unix_socket << ": " << safe_strerror(errno); | 52 << adb_unix_socket << ": " << safe_strerror(errno); |
| 53 return false; | 53 return false; |
| 54 } | 54 } |
| 55 LOG(INFO) << "Listening on Unix Domain Socket " << adb_unix_socket; |
| 55 return true; | 56 return true; |
| 56 } | 57 } |
| 57 | 58 |
| 58 void DeviceController::Start() { | 59 void DeviceController::Start() { |
| 59 while (true) { | 60 while (true) { |
| 60 CleanUpDeadListeners(); | 61 CleanUpDeadListeners(); |
| 61 scoped_ptr<Socket> socket(new Socket); | 62 scoped_ptr<Socket> socket(new Socket); |
| 62 if (!kickstart_adb_socket_.Accept(socket.get())) { | 63 if (!kickstart_adb_socket_.Accept(socket.get())) { |
| 63 LOG(ERROR) << "Could not Accept DeviceController socket: " | 64 if (!kickstart_adb_socket_.exited()) { |
| 64 << safe_strerror(errno); | 65 LOG(ERROR) << "Could not Accept DeviceController socket: " |
| 66 << safe_strerror(errno); |
| 67 } |
| 65 break; | 68 break; |
| 66 } | 69 } |
| 67 // So that |socket| doesn't block on read if it has notifications. | 70 // So that |socket| doesn't block on read if it has notifications. |
| 68 socket->set_exit_notifier_fd(exit_notifier_fd_); | 71 socket->set_exit_notifier_fd(exit_notifier_fd_); |
| 69 int port; | 72 int port; |
| 70 command::Type command; | 73 command::Type command; |
| 71 if (!ReadCommand(socket.get(), &port, &command)) { | 74 if (!ReadCommand(socket.get(), &port, &command)) { |
| 72 LOG(ERROR) << "Invalid command received."; | 75 LOG(ERROR) << "Invalid command received."; |
| 73 socket->Close(); | 76 socket->Close(); |
| 74 continue; | 77 continue; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 90 if (!new_listener->BindListenerSocket()) | 93 if (!new_listener->BindListenerSocket()) |
| 91 continue; | 94 continue; |
| 92 new_listener->Start(); | 95 new_listener->Start(); |
| 93 // |port| can be zero, to allow dynamically allocated port, so instead, | 96 // |port| can be zero, to allow dynamically allocated port, so instead, |
| 94 // we call DeviceListener::listener_port() to retrieve the currently | 97 // we call DeviceListener::listener_port() to retrieve the currently |
| 95 // allocated port to this new listener, which has been set by the | 98 // allocated port to this new listener, which has been set by the |
| 96 // BindListenerSocket() method in case of success. | 99 // BindListenerSocket() method in case of success. |
| 97 const int listener_port = new_listener->listener_port(); | 100 const int listener_port = new_listener->listener_port(); |
| 98 // |new_listener| is now owned by listeners_ map. | 101 // |new_listener| is now owned by listeners_ map. |
| 99 listeners_.AddWithID(new_listener.release(), listener_port); | 102 listeners_.AddWithID(new_listener.release(), listener_port); |
| 100 printf("Forwarding device port %d to host.\n", listener_port); | 103 LOG(INFO) << "Forwarding device port " << listener_port << " to host."; |
| 101 break; | 104 break; |
| 102 } | 105 } |
| 103 case command::DATA_CONNECTION: | 106 case command::DATA_CONNECTION: |
| 104 if (listener == NULL) { | 107 if (listener == NULL) { |
| 105 LOG(ERROR) << "Data Connection command received, but " | 108 LOG(ERROR) << "Data Connection command received, but " |
| 106 << "listener has not been set up yet for port " << port; | 109 << "listener has not been set up yet for port " << port; |
| 107 // After this point it is assumed that, once we close our Adb Data | 110 // After this point it is assumed that, once we close our Adb Data |
| 108 // socket, the Adb forwarder command will propagate the closing of | 111 // socket, the Adb forwarder command will propagate the closing of |
| 109 // sockets all the way to the host side. | 112 // sockets all the way to the host side. |
| 110 socket->Close(); | 113 socket->Close(); |
| 111 continue; | 114 continue; |
| 112 } else if (!listener->SetAdbDataSocket(socket.Pass())) { | 115 } else if (!listener->SetAdbDataSocket(socket.Pass())) { |
| 113 LOG(ERROR) << "Could not set Adb Data Socket for port: " << port; | 116 LOG(ERROR) << "Could not set Adb Data Socket for port: " << port; |
| 114 // Same assumption as above, but in this case the socket is closed | 117 // Same assumption as above, but in this case the socket is closed |
| 115 // inside SetAdbDataSocket. | 118 // inside SetAdbDataSocket. |
| 116 continue; | 119 continue; |
| 117 } | 120 } |
| 118 break; | 121 break; |
| 119 default: | 122 default: |
| 120 // TODO(felipeg): add a KillAllListeners command. | 123 // TODO(felipeg): add a KillAllListeners command. |
| 121 LOG(ERROR) << "Invalid command received. Port: " << port | 124 LOG(ERROR) << "Invalid command received. Port: " << port |
| 122 << " Command: " << command; | 125 << " Command: " << command; |
| 123 socket->Close(); | 126 socket->Close(); |
| 124 continue; | |
| 125 break; | |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 KillAllListeners(); | 129 KillAllListeners(); |
| 129 CleanUpDeadListeners(); | 130 CleanUpDeadListeners(); |
| 130 kickstart_adb_socket_.Close(); | 131 kickstart_adb_socket_.Close(); |
| 131 } | 132 } |
| 132 | 133 |
| 133 } // namespace forwarder | 134 } // namespace forwarder |
| OLD | NEW |