Compare commits
1 Commits
4e95c440d3
...
070d5fe910
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
070d5fe910 |
@@ -1,5 +1,3 @@
|
|||||||
import gleam/bytes_tree
|
|
||||||
|
|
||||||
pub type Socket
|
pub type Socket
|
||||||
|
|
||||||
// https://www.erlang.org/doc/apps/kernel/inet#t:address_family/0
|
// https://www.erlang.org/doc/apps/kernel/inet#t:address_family/0
|
||||||
@@ -12,16 +10,16 @@ type ModeValue {
|
|||||||
Binary
|
Binary
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPCOption {
|
type GenTcpOption {
|
||||||
Active(Bool)
|
Active(Bool)
|
||||||
Mode(ModeValue)
|
Mode(ModeValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type IPCError {
|
pub type ConnectionError {
|
||||||
IPCError(reason: String)
|
ConnectionError(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn connect(socket_path: String) -> Result(Socket, IPCError) {
|
pub fn connect(socket_path: String) -> Result(Socket, ConnectionError) {
|
||||||
let options = [Mode(Binary), Active(False)]
|
let options = [Mode(Binary), Active(False)]
|
||||||
|
|
||||||
// timeout in ms
|
// timeout in ms
|
||||||
@@ -33,30 +31,12 @@ pub fn connect(socket_path: String) -> Result(Socket, IPCError) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(socket: Socket, message: String) -> Result(Nil, IPCError) {
|
// connect/4
|
||||||
gen_tcp_send(socket, bytes_tree.from_string(message))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn close(socket: Socket) -> Nil {
|
|
||||||
gen_tcp_close(socket)
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://www.erlang.org/doc/apps/kernel/gen_tcp.html#connect/4
|
// https://www.erlang.org/doc/apps/kernel/gen_tcp.html#connect/4
|
||||||
@external(erlang, "gen_tcp", "connect")
|
@external(erlang, "gen_tcp", "connect")
|
||||||
fn gen_tcp_connect(
|
fn gen_tcp_connect(
|
||||||
address: Local,
|
address: Local,
|
||||||
port: Int,
|
port: Int,
|
||||||
options: List(IPCOption),
|
options: List(GenTcpOption),
|
||||||
timeout: Int,
|
timeout: Int,
|
||||||
) -> Result(Socket, IPCError)
|
) -> Result(Socket, ConnectionError)
|
||||||
|
|
||||||
// https://www.erlang.org/doc/apps/kernel/gen_tcp.html#send/2
|
|
||||||
@external(erlang, "ipc_ffi", "send")
|
|
||||||
fn gen_tcp_send(
|
|
||||||
socket: Socket,
|
|
||||||
packet: bytes_tree.BytesTree,
|
|
||||||
) -> Result(Nil, IPCError)
|
|
||||||
|
|
||||||
// https://www.erlang.org/doc/apps/kernel/gen_tcp.html#close/1
|
|
||||||
@external(erlang, "gen_tcp", "close")
|
|
||||||
fn gen_tcp_close(socket: Socket) -> Nil
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
-module(ipc_ffi).
|
|
||||||
-export([send/2]).
|
|
||||||
|
|
||||||
send(Socket, Packet) ->
|
|
||||||
case gen_tcp:send(Socket, Packet) of
|
|
||||||
ok -> {ok, nil};
|
|
||||||
Res -> Res
|
|
||||||
end.
|
|
||||||
@@ -1,37 +1,5 @@
|
|||||||
import gleam/io
|
import gleam/io
|
||||||
import ipc
|
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
let socket_path = "/tmp/musicplayer.sock"
|
io.println("Hello from musicplayer!")
|
||||||
|
|
||||||
case ipc.connect(socket_path) {
|
|
||||||
Error(err) -> io.println("Failed to connect to socket: " <> err.reason)
|
|
||||||
Ok(socket) -> {
|
|
||||||
io.println("connected")
|
|
||||||
|
|
||||||
let messages = ["hello, \n", "world!\n"]
|
|
||||||
|
|
||||||
messages |> send_messages(socket, _)
|
|
||||||
|
|
||||||
io.println("closing")
|
|
||||||
ipc.close(socket)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn send_messages(socket: ipc.Socket, messages: List(String)) -> Nil {
|
|
||||||
case messages {
|
|
||||||
[] -> Nil
|
|
||||||
[message, ..rest] -> {
|
|
||||||
send_message(socket, message)
|
|
||||||
send_messages(socket, rest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn send_message(socket: ipc.Socket, message: String) -> Nil {
|
|
||||||
case ipc.send(socket, message) {
|
|
||||||
Error(err) -> io.println("Failed to send message to socket: " <> err.reason)
|
|
||||||
Ok(_) -> io.println("Sent message to socket")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user