mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
exposed callbacks for TunnelManager::open and ::close s.t. session can pass functions that handle success/failure on open and close
This commit is contained in:
parent
7cc6c1f2be
commit
61afd96abd
5 changed files with 38 additions and 23 deletions
|
@ -98,7 +98,7 @@ main(int argc, char* argv[])
|
|||
|
||||
// hard-coded IP:port for liblokinet to bind to
|
||||
// connect via tcp will stream traffic to whatever
|
||||
lokinet_outbound_stream(&stream_res, target.c_str(), "127.0.0.1:54321", ctx);
|
||||
lokinet_outbound_stream(&stream_res, target.c_str(), "127.0.0.1:54321", ctx, nullptr, nullptr, nullptr);
|
||||
|
||||
if (stream_res.error)
|
||||
{
|
||||
|
|
|
@ -31,7 +31,10 @@ extern "C"
|
|||
struct lokinet_stream_result* result,
|
||||
const char* remoteAddr,
|
||||
const char* localAddr,
|
||||
struct lokinet_context* context);
|
||||
struct lokinet_context* context,
|
||||
void (*open_cb)(bool success, void* user_data),
|
||||
void (*close_cb)(int rv, void* user_data),
|
||||
void* user_data);
|
||||
|
||||
/// stream accept filter determines if we should accept a stream or not
|
||||
/// return 0 to accept
|
||||
|
|
|
@ -647,7 +647,10 @@ extern "C"
|
|||
struct lokinet_stream_result* result,
|
||||
const char* remote,
|
||||
const char* local,
|
||||
struct lokinet_context* ctx)
|
||||
struct lokinet_context* ctx,
|
||||
void (*open_cb)(bool success, void* user_data),
|
||||
void (*close_cb)(int rv, void* user_data),
|
||||
void* user_data)
|
||||
{
|
||||
if (ctx == nullptr)
|
||||
{
|
||||
|
@ -695,6 +698,27 @@ extern "C"
|
|||
return;
|
||||
}
|
||||
|
||||
auto on_open = [localAddr, remotehost, remoteport, open_cb](bool success, void* user_data) {
|
||||
llarp::log::info(
|
||||
logcat,
|
||||
"Quic tunnel {}<->{}:{} {}.",
|
||||
localAddr,
|
||||
remotehost,
|
||||
remoteport,
|
||||
success ? "opened successfully" : "failed");
|
||||
|
||||
if (open_cb)
|
||||
open_cb(success, user_data);
|
||||
};
|
||||
|
||||
auto on_close = [localAddr, remotehost, remoteport, close_cb](int rv, void* user_data) {
|
||||
llarp::log::info(
|
||||
logcat, "Quic tunnel {}<->{}:{} closed.", localAddr, remotehost, remoteport);
|
||||
|
||||
if (close_cb)
|
||||
close_cb(rv, user_data);
|
||||
};
|
||||
|
||||
auto call = [&promise,
|
||||
ctx,
|
||||
result,
|
||||
|
@ -702,6 +726,8 @@ extern "C"
|
|||
remotehost,
|
||||
remoteport,
|
||||
endpoint, // FIXME: appears unused?
|
||||
on_open,
|
||||
on_close,
|
||||
localAddr]() {
|
||||
auto ep = ctx->endpoint();
|
||||
if (ep == nullptr)
|
||||
|
@ -719,20 +745,6 @@ extern "C"
|
|||
}
|
||||
try
|
||||
{
|
||||
auto on_open = [localAddr, remotehost, remoteport](bool success) {
|
||||
llarp::log::info(
|
||||
logcat,
|
||||
"Quic tunnel {}<->{}:{} {}.",
|
||||
localAddr,
|
||||
remotehost,
|
||||
remoteport,
|
||||
success ? "opened successfully" : "failed");
|
||||
};
|
||||
auto on_close = [localAddr, remotehost, remoteport]() {
|
||||
llarp::log::info(
|
||||
logcat, "Quic tunnel {}<->{}:{} closed.", localAddr, remotehost, remoteport);
|
||||
};
|
||||
|
||||
auto [addr, id] = quic->open(
|
||||
remotehost, remoteport, std::move(on_open), std::move(on_close), localAddr);
|
||||
auto [host, port] = split_host_port(addr.ToString());
|
||||
|
|
|
@ -246,7 +246,7 @@ namespace llarp::quic
|
|||
{
|
||||
log::debug(logcat, "All sockets closed on quic:{}, destroying tunnel data", port);
|
||||
if (ct.close_cb)
|
||||
ct.close_cb();
|
||||
ct.close_cb(0, nullptr);
|
||||
ctit = client_tunnels_.erase(ctit);
|
||||
}
|
||||
else
|
||||
|
@ -432,7 +432,7 @@ namespace llarp::quic
|
|||
log::warning(logcat, "QUIC tunnel to {} failed during {}; aborting tunnel", addr, step_name);
|
||||
it->second.tcp->close();
|
||||
if (it->second.open_cb)
|
||||
it->second.open_cb(false);
|
||||
it->second.open_cb(false, nullptr);
|
||||
client_tunnels_.erase(it);
|
||||
}
|
||||
return step_success;
|
||||
|
@ -618,7 +618,7 @@ namespace llarp::quic
|
|||
if (it->second.open_cb)
|
||||
{
|
||||
log::trace(logcat, "Calling ClientTunnel.open_cb()");
|
||||
it->second.open_cb(true);
|
||||
it->second.open_cb(true, nullptr);
|
||||
it->second.open_cb = nullptr; // only call once
|
||||
}
|
||||
}
|
||||
|
@ -633,7 +633,7 @@ namespace llarp::quic
|
|||
if (it->second.close_cb)
|
||||
{
|
||||
log::trace(logcat, "Calling ClientTunnel.close_cb()");
|
||||
it->second.close_cb();
|
||||
it->second.close_cb(0, nullptr);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
@ -78,10 +78,10 @@ namespace llarp::quic
|
|||
forget(int id);
|
||||
|
||||
/// Called when open succeeds or times out.
|
||||
using OpenCallback = std::function<void(bool success)>;
|
||||
using OpenCallback = std::function<void(bool success, void* user_data)>;
|
||||
|
||||
/// Called when the tunnel is closed for any reason
|
||||
using CloseCallback = std::function<void(void)>;
|
||||
using CloseCallback = std::function<void(int rv, void* user_data)>;
|
||||
|
||||
/// Opens a quic tunnel to some remote lokinet address. (Should only be called from the event
|
||||
/// loop thread.)
|
||||
|
|
Loading…
Reference in a new issue