Compare commits
1 Commits
4e95c440d3
...
070d5fe910
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
070d5fe910 |
@@ -1,5 +1,3 @@
|
||||
import gleam/bytes_tree
|
||||
|
||||
pub type Socket
|
||||
|
||||
// https://www.erlang.org/doc/apps/kernel/inet#t:address_family/0
|
||||
@@ -12,16 +10,16 @@ type ModeValue {
|
||||
Binary
|
||||
}
|
||||
|
||||
type IPCOption {
|
||||
type GenTcpOption {
|
||||
Active(Bool)
|
||||
Mode(ModeValue)
|
||||
}
|
||||
|
||||
pub type IPCError {
|
||||
IPCError(reason: String)
|
||||
pub type ConnectionError {
|
||||
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)]
|
||||
|
||||
// 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) {
|
||||
gen_tcp_send(socket, bytes_tree.from_string(message))
|
||||
}
|
||||
|
||||
pub fn close(socket: Socket) -> Nil {
|
||||
gen_tcp_close(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(IPCOption),
|
||||
options: List(GenTcpOption),
|
||||
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
|
||||
) -> Result(Socket, ConnectionError)
|
||||
|
||||
@@ -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 ipc
|
||||
|
||||
pub fn main() -> Nil {
|
||||
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")
|
||||
}
|
||||
io.println("Hello from musicplayer!")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user