Skip to content

Commit 6be60fa

Browse files
committed
Use resources on stream handler
1 parent f6fb020 commit 6be60fa

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

libp2p/libp2p.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ static ERL_NIF_TERM get_handle_result(ErlNifEnv *env, ErlNifResourceType *type,
114114
return make_ok_tuple2(env, term);
115115
}
116116

117+
void send_message(erl_pid_t _pid, uintptr_t stream_handle)
118+
{
119+
// Passed as void* to avoid including erl_nif.h in the header.
120+
ErlNifPid *pid = (ErlNifPid *)_pid;
121+
ErlNifEnv *env = enif_alloc_env();
122+
123+
ERL_NIF_TERM message = get_handle_result(env, Stream, stream_handle);
124+
125+
int result = enif_send(NULL, pid, env, message);
126+
// On error, the env isn't freed by the function.
127+
if (!result)
128+
{
129+
enif_free_env(env);
130+
}
131+
}
132+
117133
/*********/
118134
/* Utils */
119135
/*********/
@@ -171,7 +187,7 @@ ERL_FUNCTION(host_set_stream_handler)
171187

172188
IF_ERROR(!enif_self(env, pid), "failed to get pid");
173189

174-
SetStreamHandler(host, proto_id, (void *)pid);
190+
SetStreamHandler(host, proto_id, (void *)pid, send_message);
175191

176192
return enif_make_atom(env, "ok");
177193
}

libp2p/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,13 @@ func (h C.uintptr_t) HostClose() {
8282
}
8383

8484
//export SetStreamHandler
85-
func (h C.uintptr_t) SetStreamHandler(protoId string, procId C.erl_pid_t) {
85+
func (h C.uintptr_t) SetStreamHandler(protoId string, procId C.erl_pid_t, callback C.send_message_t) {
8686
handle := cgo.Handle(h)
8787
host := handle.Value().(host.Host)
8888
// WARN: we clone the string because the underlying buffer is owned by Elixir
8989
goProtoId := protocol.ID(strings.Clone(protoId))
9090
handler := func(stream network.Stream) {
91-
// NOTE: the stream handle should be deleted by calling Stream.Close()
92-
C.send_message(procId, C.uintptr_t(cgo.NewHandle(stream)))
91+
C.run_callback(callback, procId, C.uintptr_t(cgo.NewHandle(stream)))
9392
}
9493
host.SetStreamHandler(protocol.ID(goProtoId), handler)
9594
}

libp2p/utils.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
#include "utils.h"
2-
#include <erl_nif.h>
32

4-
ErlNifPid *get_pid(erl_pid_t _pid)
3+
void run_callback(send_message_t send_message, erl_pid_t pid, uintptr_t stream)
54
{
6-
return (ErlNifPid *)_pid;
7-
}
8-
9-
void send_message(erl_pid_t _pid, uintptr_t stream_handle)
10-
{
11-
// Passed as void* to avoid including erl_nif.h in the header.
12-
ErlNifPid *pid = get_pid(_pid);
13-
ErlNifEnv *env = enif_alloc_env();
14-
15-
ERL_NIF_TERM message = enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_uint64(env, stream_handle));
16-
17-
int result = enif_send(NULL, pid, env, message);
18-
// On error, the env isn't freed by the function.
19-
if (!result)
20-
{
21-
enif_free_env(env);
22-
}
5+
send_message(pid, stream);
236
}

libp2p/utils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
#include <stdint.h> // for uintptr_t
44

5+
// For better readability. Pointer is casted to opaque,
6+
// to avoid having to include erl_nif.h in Go.
57
typedef void *erl_pid_t;
68

7-
void send_message(erl_pid_t pid, uintptr_t stream);
9+
// send_message function signature.
10+
typedef void (*send_message_t)(erl_pid_t pid, uintptr_t stream);
11+
12+
void run_callback(send_message_t send_message, erl_pid_t pid, uintptr_t stream);

0 commit comments

Comments
 (0)