2019-07-27 15:29:27 +02:00
|
|
|
|
2020-07-13 14:17:29 +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
|
|
|
|
2020-07-13 14:17:29 +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))
|
2020-07-13 14:17:29 +02:00
|
|
|
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
|
|
|
|
2020-07-13 14:17:29 +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:12:10 +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
|