From d85e48c9d0fc24fceb0f77ed13d496fa4f24eca4 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 29 Jun 2020 14:39:31 -0600 Subject: [PATCH] Use llarp::Context::CallSafe() for vpn AsyncClose() --- include/llarp.h | 7 ++++++- llarp/context.cpp | 6 +++--- llarp/ev/vpnio.cpp | 5 ++++- llarp/ev/vpnio.hpp | 5 +++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/llarp.h b/include/llarp.h index 2080fb7ad..0a2b4dd39 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -3,6 +3,11 @@ #include #include +namespace llarp +{ + struct Context; +} + #ifdef __cplusplus extern "C" { @@ -48,7 +53,7 @@ extern "C" /// initialize llarp_vpn_io private implementation /// returns false if either parameter is nullptr bool - llarp_vpn_io_init(struct llarp_main* m, struct llarp_vpn_io* io); + llarp_vpn_io_init(llarp::Context* ctx, struct llarp_vpn_io* io); /// get the packet pipe for writing IP packets to lokinet internals /// returns nullptr if llarp_vpn_io is nullptr or not initialized diff --git a/llarp/context.cpp b/llarp/context.cpp index 76524b212..8fcbca2e6 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -297,11 +297,11 @@ extern "C" } bool - llarp_vpn_io_init(struct llarp_main* ptr, struct llarp_vpn_io* io) + llarp_vpn_io_init(llarp::Context* ctx, struct llarp_vpn_io* io) { - if (io == nullptr || ptr == nullptr) + if (io == nullptr || ctx == nullptr) return false; - llarp_vpn_io_impl* impl = new llarp_vpn_io_impl(ptr, io); + llarp_vpn_io_impl* impl = new llarp_vpn_io_impl(ctx, io); io->impl = impl; return true; } diff --git a/llarp/ev/vpnio.cpp b/llarp/ev/vpnio.cpp index af12b4c59..b7e05d3dc 100644 --- a/llarp/ev/vpnio.cpp +++ b/llarp/ev/vpnio.cpp @@ -10,7 +10,10 @@ llarp_vpn_io_impl::AsyncClose() writer.queue.disable(); // TODO: call asynchronously - Expunge(); + if (ctx) + ctx->CallSafe([this]() { Expunge(); }); + else + Expunge(); } void diff --git a/llarp/ev/vpnio.hpp b/llarp/ev/vpnio.hpp index af20eb055..5aaa764ae 100644 --- a/llarp/ev/vpnio.hpp +++ b/llarp/ev/vpnio.hpp @@ -2,6 +2,7 @@ #define LLARP_EV_VPNIO_HPP #include #include +#include #include struct llarp_main; @@ -26,12 +27,12 @@ struct llarp_vpn_pkt_reader : public llarp_vpn_pkt_queue struct llarp_vpn_io_impl { - llarp_vpn_io_impl(llarp_main* p, llarp_vpn_io* io) : ptr(p), parent(io) + llarp_vpn_io_impl(llarp::Context* c, llarp_vpn_io* io) : ctx(c), parent(io) { } ~llarp_vpn_io_impl() = default; - llarp_main* ptr; + llarp::Context* ctx; llarp_vpn_io* parent; llarp_vpn_pkt_writer writer;