diff options
author | android-build-prod (mdb) | 2018-04-27 18:16:37 -0500 |
---|---|---|
committer | Gerrit Code Review | 2018-04-27 18:16:37 -0500 |
commit | 2666955d3d6aab35d033d41e3f63d568179f3663 (patch) | |
tree | 8bbfa6bf7b8af5250c3038842d71039dc81bc721 | |
parent | 65027d9d8038c9c1e57b2f5b7016634d00e71173 (diff) | |
parent | 9a388d53872f51daac16454cb7218e32ca368d3f (diff) | |
download | platform-system-core-2666955d3d6aab35d033d41e3f63d568179f3663.tar.gz platform-system-core-2666955d3d6aab35d033d41e3f63d568179f3663.tar.xz platform-system-core-2666955d3d6aab35d033d41e3f63d568179f3663.zip |
Merge "adb: Make the Connection object a std::shared_ptr"
-rw-r--r-- | adb/transport.cpp | 24 | ||||
-rw-r--r-- | adb/transport.h | 16 | ||||
-rw-r--r-- | adb/transport_local.cpp | 5 | ||||
-rw-r--r-- | adb/transport_usb.cpp | 2 |
4 files changed, 32 insertions, 15 deletions
diff --git a/adb/transport.cpp b/adb/transport.cpp index 0ab428e0d..706aee604 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp | |||
@@ -517,8 +517,8 @@ static void transport_registration_func(int _fd, unsigned ev, void*) { | |||
517 | if (t->GetConnectionState() != kCsNoPerm) { | 517 | if (t->GetConnectionState() != kCsNoPerm) { |
518 | /* initial references are the two threads */ | 518 | /* initial references are the two threads */ |
519 | t->ref_count = 1; | 519 | t->ref_count = 1; |
520 | t->connection->SetTransportName(t->serial_name()); | 520 | t->connection()->SetTransportName(t->serial_name()); |
521 | t->connection->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) { | 521 | t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) { |
522 | if (!check_header(p.get(), t)) { | 522 | if (!check_header(p.get(), t)) { |
523 | D("%s: remote read: bad header", t->serial); | 523 | D("%s: remote read: bad header", t->serial); |
524 | return false; | 524 | return false; |
@@ -531,7 +531,7 @@ static void transport_registration_func(int _fd, unsigned ev, void*) { | |||
531 | fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); }); | 531 | fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); }); |
532 | return true; | 532 | return true; |
533 | }); | 533 | }); |
534 | t->connection->SetErrorCallback([t](Connection*, const std::string& error) { | 534 | t->connection()->SetErrorCallback([t](Connection*, const std::string& error) { |
535 | D("%s: connection terminated: %s", t->serial, error.c_str()); | 535 | D("%s: connection terminated: %s", t->serial, error.c_str()); |
536 | fdevent_run_on_main_thread([t]() { | 536 | fdevent_run_on_main_thread([t]() { |
537 | handle_offline(t); | 537 | handle_offline(t); |
@@ -539,7 +539,7 @@ static void transport_registration_func(int _fd, unsigned ev, void*) { | |||
539 | }); | 539 | }); |
540 | }); | 540 | }); |
541 | 541 | ||
542 | t->connection->Start(); | 542 | t->connection()->Start(); |
543 | #if ADB_HOST | 543 | #if ADB_HOST |
544 | send_connect(t); | 544 | send_connect(t); |
545 | #endif | 545 | #endif |
@@ -608,7 +608,7 @@ static void transport_unref(atransport* t) { | |||
608 | t->ref_count--; | 608 | t->ref_count--; |
609 | if (t->ref_count == 0) { | 609 | if (t->ref_count == 0) { |
610 | D("transport: %s unref (kicking and closing)", t->serial); | 610 | D("transport: %s unref (kicking and closing)", t->serial); |
611 | t->connection->Stop(); | 611 | t->connection()->Stop(); |
612 | remove_transport(t); | 612 | remove_transport(t); |
613 | } else { | 613 | } else { |
614 | D("transport: %s unref (count=%zu)", t->serial, t->ref_count); | 614 | D("transport: %s unref (count=%zu)", t->serial, t->ref_count); |
@@ -758,14 +758,14 @@ atransport::~atransport() { | |||
758 | } | 758 | } |
759 | 759 | ||
760 | int atransport::Write(apacket* p) { | 760 | int atransport::Write(apacket* p) { |
761 | return this->connection->Write(std::unique_ptr<apacket>(p)) ? 0 : -1; | 761 | return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1; |
762 | } | 762 | } |
763 | 763 | ||
764 | void atransport::Kick() { | 764 | void atransport::Kick() { |
765 | if (!kicked_) { | 765 | if (!kicked_) { |
766 | D("kicking transport %s", this->serial); | 766 | D("kicking transport %s", this->serial); |
767 | kicked_ = true; | 767 | kicked_ = true; |
768 | this->connection->Stop(); | 768 | this->connection()->Stop(); |
769 | } | 769 | } |
770 | } | 770 | } |
771 | 771 | ||
@@ -778,6 +778,11 @@ void atransport::SetConnectionState(ConnectionState state) { | |||
778 | connection_state_ = state; | 778 | connection_state_ = state; |
779 | } | 779 | } |
780 | 780 | ||
781 | void atransport::SetConnection(std::unique_ptr<Connection> connection) { | ||
782 | std::lock_guard<std::mutex> lock(mutex_); | ||
783 | connection_ = std::shared_ptr<Connection>(std::move(connection)); | ||
784 | } | ||
785 | |||
781 | std::string atransport::connection_state_name() const { | 786 | std::string atransport::connection_state_name() const { |
782 | ConnectionState state = GetConnectionState(); | 787 | ConnectionState state = GetConnectionState(); |
783 | switch (state) { | 788 | switch (state) { |
@@ -1094,8 +1099,9 @@ void register_usb_transport(usb_handle* usb, const char* serial, const char* dev | |||
1094 | void unregister_usb_transport(usb_handle* usb) { | 1099 | void unregister_usb_transport(usb_handle* usb) { |
1095 | std::lock_guard<std::recursive_mutex> lock(transport_lock); | 1100 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
1096 | transport_list.remove_if([usb](atransport* t) { | 1101 | transport_list.remove_if([usb](atransport* t) { |
1097 | if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) { | 1102 | auto connection = t->connection(); |
1098 | return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm; | 1103 | if (auto usb_connection = dynamic_cast<UsbConnection*>(connection.get())) { |
1104 | return usb_connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm; | ||
1099 | } | 1105 | } |
1100 | return false; | 1106 | return false; |
1101 | }); | 1107 | }); |
diff --git a/adb/transport.h b/adb/transport.h index 4e0220f2d..ebc186bf9 100644 --- a/adb/transport.h +++ b/adb/transport.h | |||
@@ -201,7 +201,8 @@ class atransport { | |||
201 | atransport(ConnectionState state = kCsOffline) | 201 | atransport(ConnectionState state = kCsOffline) |
202 | : id(NextTransportId()), | 202 | : id(NextTransportId()), |
203 | connection_state_(state), | 203 | connection_state_(state), |
204 | connection_waitable_(std::make_shared<ConnectionWaitable>()) { | 204 | connection_waitable_(std::make_shared<ConnectionWaitable>()), |
205 | connection_(nullptr) { | ||
205 | // Initialize protocol to min version for compatibility with older versions. | 206 | // Initialize protocol to min version for compatibility with older versions. |
206 | // Version will be updated post-connect. | 207 | // Version will be updated post-connect. |
207 | protocol_version = A_VERSION_MIN; | 208 | protocol_version = A_VERSION_MIN; |
@@ -216,13 +217,17 @@ class atransport { | |||
216 | ConnectionState GetConnectionState() const; | 217 | ConnectionState GetConnectionState() const; |
217 | void SetConnectionState(ConnectionState state); | 218 | void SetConnectionState(ConnectionState state); |
218 | 219 | ||
220 | void SetConnection(std::unique_ptr<Connection> connection); | ||
221 | std::shared_ptr<Connection> connection() { | ||
222 | std::lock_guard<std::mutex> lock(mutex_); | ||
223 | return connection_; | ||
224 | } | ||
225 | |||
219 | const TransportId id; | 226 | const TransportId id; |
220 | size_t ref_count = 0; | 227 | size_t ref_count = 0; |
221 | bool online = false; | 228 | bool online = false; |
222 | TransportType type = kTransportAny; | 229 | TransportType type = kTransportAny; |
223 | 230 | ||
224 | std::unique_ptr<Connection> connection; | ||
225 | |||
226 | // Used to identify transports for clients. | 231 | // Used to identify transports for clients. |
227 | char* serial = nullptr; | 232 | char* serial = nullptr; |
228 | char* product = nullptr; | 233 | char* product = nullptr; |
@@ -302,6 +307,11 @@ class atransport { | |||
302 | // connection to be established. | 307 | // connection to be established. |
303 | std::shared_ptr<ConnectionWaitable> connection_waitable_; | 308 | std::shared_ptr<ConnectionWaitable> connection_waitable_; |
304 | 309 | ||
310 | // The underlying connection object. | ||
311 | std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_); | ||
312 | |||
313 | std::mutex mutex_; | ||
314 | |||
305 | DISALLOW_COPY_AND_ASSIGN(atransport); | 315 | DISALLOW_COPY_AND_ASSIGN(atransport); |
306 | }; | 316 | }; |
307 | 317 | ||
diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp index 8032421b1..e81f27c95 100644 --- a/adb/transport_local.cpp +++ b/adb/transport_local.cpp | |||
@@ -456,7 +456,8 @@ int init_socket_transport(atransport* t, int s, int adb_port, int local) { | |||
456 | // Emulator connection. | 456 | // Emulator connection. |
457 | if (local) { | 457 | if (local) { |
458 | auto emulator_connection = std::make_unique<EmulatorConnection>(std::move(fd), adb_port); | 458 | auto emulator_connection = std::make_unique<EmulatorConnection>(std::move(fd), adb_port); |
459 | t->connection = std::make_unique<BlockingConnectionAdapter>(std::move(emulator_connection)); | 459 | t->SetConnection( |
460 | std::make_unique<BlockingConnectionAdapter>(std::move(emulator_connection))); | ||
460 | std::lock_guard<std::mutex> lock(local_transports_lock); | 461 | std::lock_guard<std::mutex> lock(local_transports_lock); |
461 | atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port); | 462 | atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port); |
462 | if (existing_transport != NULL) { | 463 | if (existing_transport != NULL) { |
@@ -476,6 +477,6 @@ int init_socket_transport(atransport* t, int s, int adb_port, int local) { | |||
476 | 477 | ||
477 | // Regular tcp connection. | 478 | // Regular tcp connection. |
478 | auto fd_connection = std::make_unique<FdConnection>(std::move(fd)); | 479 | auto fd_connection = std::make_unique<FdConnection>(std::move(fd)); |
479 | t->connection = std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)); | 480 | t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection))); |
480 | return fail; | 481 | return fail; |
481 | } | 482 | } |
diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp index e9a75cd71..94b2e377c 100644 --- a/adb/transport_usb.cpp +++ b/adb/transport_usb.cpp | |||
@@ -176,7 +176,7 @@ void UsbConnection::Close() { | |||
176 | void init_usb_transport(atransport* t, usb_handle* h) { | 176 | void init_usb_transport(atransport* t, usb_handle* h) { |
177 | D("transport: usb"); | 177 | D("transport: usb"); |
178 | auto connection = std::make_unique<UsbConnection>(h); | 178 | auto connection = std::make_unique<UsbConnection>(h); |
179 | t->connection = std::make_unique<BlockingConnectionAdapter>(std::move(connection)); | 179 | t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(connection))); |
180 | t->type = kTransportUsb; | 180 | t->type = kTransportUsb; |
181 | } | 181 | } |
182 | 182 | ||