Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: direct connection callback #8

Merged
merged 7 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tokio = { version = "1.36.0", features = ["rt-multi-thread"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = "2.5.0"
futures-lite = "2.3.0"

[features]
# generate headers
Expand Down
53 changes: 53 additions & 0 deletions irohnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ enum EndpointResult {
* It is common to simply log this error and move on.
*/
ENDPOINT_RESULT_INCOMING_ERROR,
/** \brief
* Unable to find connection for the given `NodeId`
*/
ENDPOINT_RESULT_CONNECTION_TYPE_ERROR,
}
#ifndef DOXYGEN
; typedef uint8_t
Expand Down Expand Up @@ -513,6 +517,55 @@ typedef struct PublicKey {
uint8_32_array_t key;
} PublicKey_t;

/** <No documentation available> */
/** \remark Has the same ABI as `uint8_t` **/
#ifdef DOXYGEN
typedef
#endif
enum ConnectionType {
/** \brief
* Direct UDP connection
*/
CONNECTION_TYPE_DIRECT = 0,
/** \brief
* Relay connection over relay
*/
CONNECTION_TYPE_RELAY,
/** \brief
* Both a UDP and a relay connection are used.
*
* This is the case if we do have a UDP address, but are missing a recent confirmation that
* the address works.
*/
CONNECTION_TYPE_MIXED,
/** \brief
* We have no verified connection to this PublicKey
*/
CONNECTION_TYPE_NONE,
}
#ifndef DOXYGEN
; typedef uint8_t
#endif
ConnectionType_t;

/** \brief
* Run a callback once you have a direct connection to a peer
*
* Does not block. The provided callback will be called when we have a direct
* connection to the peer associated with the `node_id`, or the timeout has occurred.
*
* To wait indefinitely, provide -1 for the timeout parameter.
*
* `ctx` is passed along to the callback, to allow passing context, it must be thread safe as the callback is
* called from another thread.
*/
void
endpoint_conn_type_cb (
Endpoint_t * ep,
void const * ctx,
PublicKey_t const * node_id,
void (*cb)(void const *, EndpointResult_t, ConnectionType_t));

/** \brief
* Represents a valid URL.
*/
Expand Down
46 changes: 46 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,24 @@ run_server (EndpointConfig_t * config, slice_ref_uint8_t alpn_slice, bool json_o
return 0;
}

typedef struct ConnectionStatus {
EndpointResult_t res;
ConnectionType_t conn_type;
} ConnectionStatus;

void
callback(
void const * ctx,
EndpointResult_t res,
ConnectionType_t conn_type
)
{
ConnectionStatus *cs;
cs = (ConnectionStatus *)ctx;
cs->res = res;
cs->conn_type = conn_type;
}

int
run_client (
EndpointConfig_t * config,
Expand Down Expand Up @@ -315,6 +333,10 @@ run_client (
return -1;
}

ConnectionStatus *conn_status;
conn_status = malloc(sizeof(ConnectionStatus));
endpoint_conn_type_cb(ep, (const void *)conn_status, &node_addr.node_id, callback);

SendStream_t * send_stream = send_stream_default();
ret = connection_open_uni(&conn, &send_stream);
if (ret != 0) {
Expand Down Expand Up @@ -389,6 +411,29 @@ run_client (
printf("received: '%s'\n", recv_str);

fflush(stdout);
// check that we were able to use the conn_type callback
if (conn_status->res != ENDPOINT_RESULT_OK) {
fprintf(stderr, "callback failed to send a connection type\n");
return -1;
} else {
switch (conn_status->conn_type) {
case CONNECTION_TYPE_DIRECT:
printf("had a direct connection\n");
break;
case CONNECTION_TYPE_RELAY:
printf("had a relay connection\n");
break;
case CONNECTION_TYPE_MIXED:
printf("had a mixed connection\n");
break;
case CONNECTION_TYPE_NONE:
fprintf(stderr, "callback reported no connection\n");
return -1;
default:
fprintf(stderr, "unknown connection type reported: %i\n", conn_status->conn_type);
return -1;
}
}

// finish
ret = send_stream_finish(send_stream);
Expand All @@ -413,6 +458,7 @@ run_client (
// cleanup
free(recv_str);
free(recv_buffer);
free(conn_status);
recv_stream_free(recv_stream);
return 0;
}
Expand Down
Binary file added multi-thread-client
Binary file not shown.
Binary file added multi-thread-server
Binary file not shown.
Binary file added single-thread-server
ramfox marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Loading
Loading