Obsolete
/
ipc.cr-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
ipc.cr-old/src/ipc/client.cr

40 lines
877 B
Crystal
Raw Normal View History

2019-07-27 15:29:27 +02:00
class IPC::Client < IPC::Context
2020-07-14 17:04:51 +02:00
property server_fd : Int32?
2020-07-13 18:40:21 +02:00
# By default, this is a client.
def initialize(service_name : String)
2019-07-27 15:29:27 +02:00
super()
2020-07-13 18:40:21 +02:00
serverfd = 0
r = LibIPC.ipc_connection(self.pointer, service_name, pointerof(serverfd))
if r.error_code != 0
m = String.new r.error_message.to_slice
raise Exception.new "error during connection establishment: #{m}"
end
2020-07-14 17:04:51 +02:00
@server_fd = serverfd
2020-07-13 18:40:21 +02:00
# Very important as there are filesystem side-effects.
at_exit { close }
2019-07-27 15:29:27 +02:00
end
2020-07-14 17:04:51 +02:00
2020-07-23 19:09:15 +02:00
def fd
@server_fd
end
2020-07-14 17:04:51 +02:00
def read
unless (fd = @server_fd).nil?
message = LibIPC::Message.new
r = LibIPC.ipc_read_fd(fd, pointerof(message))
if r.error_code != 0
m = String.new r.error_message.to_slice
raise Exception.new "error reading a message: #{m}"
end
IPC::Message.new pointerof(message)
else
raise "Client not connected to a server"
end
end
2019-07-27 15:29:27 +02:00
end