| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 57 } |
| 58 | 58 |
| 59 void DeviceController::Start() { | 59 void DeviceController::Start() { |
| 60 while (true) { | 60 while (true) { |
| 61 CleanUpDeadListeners(); | 61 CleanUpDeadListeners(); |
| 62 scoped_ptr<Socket> socket(new Socket); | 62 scoped_ptr<Socket> socket(new Socket); |
| 63 if (!kickstart_adb_socket_.Accept(socket.get())) { | 63 if (!kickstart_adb_socket_.Accept(socket.get())) { |
| 64 if (!kickstart_adb_socket_.exited()) { | 64 if (!kickstart_adb_socket_.exited()) { |
| 65 LOG(ERROR) << "Could not Accept DeviceController socket: " | 65 LOG(ERROR) << "Could not Accept DeviceController socket: " |
| 66 << safe_strerror(errno); | 66 << safe_strerror(errno); |
| 67 } else { |
| 68 LOG(INFO) << "Received exit notification"; |
| 67 } | 69 } |
| 68 break; | 70 break; |
| 69 } | 71 } |
| 70 // So that |socket| doesn't block on read if it has notifications. | 72 // So that |socket| doesn't block on read if it has notifications. |
| 71 socket->set_exit_notifier_fd(exit_notifier_fd_); | 73 socket->set_exit_notifier_fd(exit_notifier_fd_); |
| 72 int port; | 74 int port; |
| 73 command::Type command; | 75 command::Type command; |
| 74 if (!ReadCommand(socket.get(), &port, &command)) { | 76 if (!ReadCommand(socket.get(), &port, &command)) { |
| 75 LOG(ERROR) << "Invalid command received."; | 77 LOG(ERROR) << "Invalid command received."; |
| 76 socket->Close(); | |
| 77 continue; | 78 continue; |
| 78 } | 79 } |
| 79 DeviceListener* listener = listeners_.Lookup(port); | 80 DeviceListener* listener = listeners_.Lookup(port); |
| 80 switch (command) { | 81 switch (command) { |
| 81 case command::LISTEN: { | 82 case command::LISTEN: { |
| 82 if (listener != NULL) { | 83 if (listener != NULL) { |
| 83 LOG(WARNING) << "Already forwarding port " << port | 84 LOG(WARNING) << "Already forwarding port " << port |
| 84 << ". Attempting to restart the listener.\n"; | 85 << ". Attempting to restart the listener.\n"; |
| 85 listener->ForceExit(); | 86 listener->ForceExit(); |
| 86 listener->Join(); | 87 listener->Join(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 103 LOG(INFO) << "Forwarding device port " << listener_port << " to host."; | 104 LOG(INFO) << "Forwarding device port " << listener_port << " to host."; |
| 104 break; | 105 break; |
| 105 } | 106 } |
| 106 case command::DATA_CONNECTION: | 107 case command::DATA_CONNECTION: |
| 107 if (listener == NULL) { | 108 if (listener == NULL) { |
| 108 LOG(ERROR) << "Data Connection command received, but " | 109 LOG(ERROR) << "Data Connection command received, but " |
| 109 << "listener has not been set up yet for port " << port; | 110 << "listener has not been set up yet for port " << port; |
| 110 // After this point it is assumed that, once we close our Adb Data | 111 // After this point it is assumed that, once we close our Adb Data |
| 111 // socket, the Adb forwarder command will propagate the closing of | 112 // socket, the Adb forwarder command will propagate the closing of |
| 112 // sockets all the way to the host side. | 113 // sockets all the way to the host side. |
| 113 socket->Close(); | |
| 114 continue; | 114 continue; |
| 115 } else if (!listener->SetAdbDataSocket(socket.Pass())) { | 115 } else if (!listener->SetAdbDataSocket(socket.Pass())) { |
| 116 LOG(ERROR) << "Could not set Adb Data Socket for port: " << port; | 116 LOG(ERROR) << "Could not set Adb Data Socket for port: " << port; |
| 117 // 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 |
| 118 // inside SetAdbDataSocket. | 118 // inside SetAdbDataSocket. |
| 119 continue; | 119 continue; |
| 120 } | 120 } |
| 121 break; | 121 break; |
| 122 default: | 122 default: |
| 123 // TODO(felipeg): add a KillAllListeners command. | 123 // TODO(felipeg): add a KillAllListeners command. |
| 124 LOG(ERROR) << "Invalid command received. Port: " << port | 124 LOG(ERROR) << "Invalid command received. Port: " << port |
| 125 << " Command: " << command; | 125 << " Command: " << command; |
| 126 socket->Close(); | |
| 127 } | 126 } |
| 128 } | 127 } |
| 129 KillAllListeners(); | 128 KillAllListeners(); |
| 130 CleanUpDeadListeners(); | 129 CleanUpDeadListeners(); |
| 131 kickstart_adb_socket_.Close(); | |
| 132 } | 130 } |
| 133 | 131 |
| 134 } // namespace forwarder | 132 } // namespace forwarder |
| OLD | NEW |