Compare commits
2 Commits
musicplaye
...
738798da3b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
738798da3b | ||
|
|
0fa2ce4d0e |
59
src/ipc.gleam
Normal file
59
src/ipc.gleam
Normal file
@@ -0,0 +1,59 @@
|
||||
import gleam/bytes_tree
|
||||
|
||||
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 IPCOption {
|
||||
Active(Bool)
|
||||
Mode(ModeValue)
|
||||
}
|
||||
|
||||
pub type IPCError {
|
||||
IPCError(reason: String)
|
||||
}
|
||||
|
||||
pub fn connect(socket_path: String) -> Result(Socket, IPCError) {
|
||||
let options = [Mode(Binary), Active(False)]
|
||||
|
||||
// timeout in ms
|
||||
let timeout = 1000
|
||||
|
||||
gen_tcp_connect(Local(socket_path), 0, options, timeout)
|
||||
}
|
||||
|
||||
pub fn send(socket: Socket, message: String) -> Result(Nil, IPCError) {
|
||||
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
|
||||
@external(erlang, "gen_tcp", "connect")
|
||||
fn gen_tcp_connect(
|
||||
address: Local,
|
||||
port: Int,
|
||||
options: List(IPCOption),
|
||||
timeout: Int,
|
||||
) -> Result(Socket, IPCError)
|
||||
|
||||
// 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
|
||||
8
src/ipc_ffi.erl
Normal file
8
src/ipc_ffi.erl
Normal file
@@ -0,0 +1,8 @@
|
||||
-module(ipc_ffi).
|
||||
-export([send/2]).
|
||||
|
||||
send(Socket, Packet) ->
|
||||
case gen_tcp:send(Socket, Packet) of
|
||||
ok -> {ok, nil};
|
||||
Res -> Res
|
||||
end.
|
||||
@@ -1,5 +1,37 @@
|
||||
import gleam/io
|
||||
import ipc
|
||||
|
||||
pub fn main() -> Nil {
|
||||
io.println("Hello from musicplayer!")
|
||||
let socket_path = "/tmp/musicplayer.sock"
|
||||
|
||||
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