adb: poll for emulator connection.
Bug: 19974213
Change-Id: I336f3ad6f428277c54479e5b8c45d5343c64f472
diff --git a/transport_local.cpp b/transport_local.cpp
index db9bedb..650e5ea 100644
--- a/transport_local.cpp
+++ b/transport_local.cpp
@@ -94,6 +94,10 @@
int fd = -1;
#if ADB_HOST
+ if (find_emulator_transport_by_adb_port(adb_port) != nullptr) {
+ return -1;
+ }
+
const char *host = getenv("ADBHOST");
if (host) {
fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
@@ -108,8 +112,10 @@
close_on_exec(fd);
disable_tcp_nagle(fd);
std::string serial = android::base::StringPrintf("emulator-%d", console_port);
- register_socket_transport(fd, serial.c_str(), adb_port, 1);
- return 0;
+ if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
+ return 0;
+ }
+ adb_close(fd);
}
return -1;
}
@@ -118,16 +124,16 @@
static void *client_socket_thread(void *x)
{
#if ADB_HOST
- int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
- int count = ADB_LOCAL_TRANSPORT_MAX;
-
D("transport: client_socket_thread() starting\n");
+ while (true) {
+ int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
+ int count = ADB_LOCAL_TRANSPORT_MAX;
- /* try to connect to any number of running emulator instances */
- /* this is only done when ADB starts up. later, each new emulator */
- /* will send a message to ADB to indicate that is is starting up */
- for ( ; count > 0; count--, port += 2 ) {
- local_connect(port);
+ // Try to connect to any number of running emulator instances.
+ for ( ; count > 0; count--, port += 2 ) {
+ local_connect(port);
+ }
+ sleep(1);
}
#endif
return 0;