From 5c7ac4f8854ab27cb5756a19746bf3ad59f65c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20S=C3=A1nchez-Beato?= Date: Tue, 22 Aug 2017 11:53:42 +0200 Subject: [PATCH] Release resources on disconnection --- src/anbox/qemu/adb_message_processor.cpp | 31 +++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/anbox/qemu/adb_message_processor.cpp b/src/anbox/qemu/adb_message_processor.cpp index 97135cb8..263d3155 100644 --- a/src/anbox/qemu/adb_message_processor.cpp +++ b/src/anbox/qemu/adb_message_processor.cpp @@ -20,13 +20,21 @@ #include "anbox/network/delegate_message_processor.h" #include "anbox/network/tcp_socket_messenger.h" #include "anbox/utils.h" +#include "anbox/logger.h" #include #include +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include + namespace { const unsigned short default_adb_client_port{5037}; -const unsigned short default_host_listen_port{6664}; +const unsigned short default_host_listen_port{5559}; constexpr const char *loopback_address{"127.0.0.1"}; const std::string accept_command{"accept"}; const std::string ok_command{"ok"}; @@ -52,9 +60,12 @@ AdbMessageProcessor::AdbMessageProcessor( expected_command_(accept_command), messenger_(messenger), host_notify_timer_(rt->service()), - lock_(active_instance_, std::defer_lock) {} + lock_(active_instance_, std::defer_lock) { + INFO("%p constructor", this); +} AdbMessageProcessor::~AdbMessageProcessor() { + INFO("%p destructor", this); state_ = closed_by_host; host_notify_timer_.cancel(); @@ -62,6 +73,7 @@ AdbMessageProcessor::~AdbMessageProcessor() { } void AdbMessageProcessor::advance_state() { + INFO("%p [%d] state %d", this, syscall(SYS_gettid), state_); switch (state_) { case waiting_for_guest_accept_command: // Try to get a lock here as if we already have another processor @@ -108,7 +120,9 @@ void AdbMessageProcessor::advance_state() { } void AdbMessageProcessor::wait_for_host_connection() { - if (state_ == closed_by_host || state_ == closed_by_container) + INFO("%p state %d", this, state_); + + if (state_ != waiting_for_guest_accept_command) return; if (!host_connector_) { @@ -141,6 +155,8 @@ void AdbMessageProcessor::wait_for_host_connection() { } void AdbMessageProcessor::on_host_connection(std::shared_ptr> const &socket) { + INFO("%p state %d", this, state_); + host_messenger_ = std::make_shared(socket); // set_no_delay() reduces the latency of sending data, at the cost @@ -159,13 +175,19 @@ void AdbMessageProcessor::on_host_connection(std::shared_ptrasync_receive_msg(callback, boost::asio::buffer(host_buffer_)); } void AdbMessageProcessor::on_host_read_size(const boost::system::error_code &error, std::size_t bytes_read) { if (error) { + INFO("%p [%d] closed error %d", this, syscall(SYS_gettid), error.value()); state_ = closed_by_host; + host_connector_.reset(); + //messenger_.reset(); + lock_.unlock(); return; } @@ -174,6 +196,8 @@ void AdbMessageProcessor::on_host_read_size(const boost::system::error_code &err } bool AdbMessageProcessor::process_data(const std::vector &data) { + INFO("%p state %d bytes rx %d", this, state_, int(data.size())); + if (state_ == proxying_data) { host_messenger_->send(reinterpret_cast(data.data()), data.size()); @@ -186,6 +210,7 @@ bool AdbMessageProcessor::process_data(const std::vector &data) { buffer_.size() >= expected_command_.size()) { if (::memcmp(buffer_.data(), expected_command_.data(), data.size()) != 0) { // We got not the command we expected and will terminate here + INFO("%p not the expected command", this); return false; } -- GitLab