From 070d5fe910fb554924602724ce80ce2a7f7417d3 Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Sun, 9 Nov 2025 18:23:06 +0100 Subject: [PATCH] wip --- src/ipc.gleam | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/ipc.gleam diff --git a/src/ipc.gleam b/src/ipc.gleam new file mode 100644 index 0000000..4cdff46 --- /dev/null +++ b/src/ipc.gleam @@ -0,0 +1,42 @@ +pub type Socket + +// https://www.erlang.org/doc/apps/kernel/inet#t:address_family/0 +// local socket +type Local { + Local(socket_path: String) +} + +type ModeValue { + Binary +} + +type GenTcpOption { + Active(Bool) + Mode(ModeValue) +} + +pub type ConnectionError { + ConnectionError(String) +} + +pub fn connect(socket_path: String) -> Result(Socket, ConnectionError) { + let options = [Mode(Binary), Active(False)] + + // timeout in ms + let timeout = 1000 + + case gen_tcp_connect(Local(socket_path), 0, options, timeout) { + Error(err) -> Error(err) + Ok(socket) -> Ok(socket) + } +} + +// connect/4 +// https://www.erlang.org/doc/apps/kernel/gen_tcp.html#connect/4 +@external(erlang, "gen_tcp", "connect") +fn gen_tcp_connect( + address: Local, + port: Int, + options: List(GenTcpOption), + timeout: Int, +) -> Result(Socket, ConnectionError)