#include <glib.h>
-#include <thread>
+#include <atomic>
#include <mutex>
+#include <thread>
#include "log-private.hh"
#include "port-internal.h"
class ProxyExt : public Proxy, public Proxy::IEventListener {
public:
- explicit ProxyExt(bool mock = false) : Proxy(mock) {}
+ explicit ProxyExt(bool mock = false) : Proxy(mock), destroying_(false) {}
virtual ~ProxyExt() = default;
void AddConnectedEventListener(rpc_port_proxy_connected_event_cb cb,
}
void OnConnected(const std::string& endpoint, Port* port) override {
+ if (IsDestroying())
+ return;
+
for (auto& ev : connected_events_) {
ev->cb_(endpoint.c_str(), GetPortName().c_str(), port,
ev->user_data_);
}
void OnDisconnected(const std::string& endpoint) override {
+ if (IsDestroying())
+ return;
+
for (auto& ev : disconnected_events_) {
ev->cb_(endpoint.c_str(), GetPortName().c_str(), ev->user_data_);
}
}
void OnRejected(const std::string& endpoint, int err_code) override {
+ if (IsDestroying())
+ return;
+
for (auto& ev : rejected_events_) {
set_last_result(err_code);
ev->cb_(endpoint.c_str(), GetPortName().c_str(), ev->user_data_);
}
void OnReceived(const std::string& endpoint) override {
+ if (IsDestroying())
+ return;
+
for (auto& ev : received_events_) {
ev->cb_(endpoint.c_str(), GetPortName().c_str(), ev->user_data_);
}
return mutex_;
}
+ void SetDestroying(bool destroying) {
+ destroying_ = destroying;
+ }
+
+ bool IsDestroying() {
+ return destroying_;
+ }
+
private:
+ std::atomic<bool> destroying_;
std::list<std::unique_ptr<Event<rpc_port_proxy_connected_event_cb>>>
connected_events_;
std::list<std::unique_ptr<Event<rpc_port_proxy_disconnected_event_cb>>>
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- _W("rpc_port_proxy_destroy(%p)", h);
auto p = static_cast<::ProxyExt*>(h);
- delete p;
+ p->SetDestroying(true);
+
+ g_idle_add_full(G_PRIORITY_HIGH,
+ [](gpointer data) -> gboolean {
+ auto p = static_cast<::ProxyExt*>(data);
+ _W("rpc_port_proxy_destroy(%p)", p);
+ delete p;
+ return G_SOURCE_REMOVE;
+ }, h, nullptr);
return RPC_PORT_ERROR_NONE;
}