diff --git a/src/ipc/client.cr b/src/ipc/client.cr index b3729a8..7c65637 100644 --- a/src/ipc/client.cr +++ b/src/ipc/client.cr @@ -1,12 +1,9 @@ require "./lowlevel" require "./message" require "./event" -require "./service" require "./context" class IPC::Client < IPC::Context - property connection : IPC::Connection - # By default, this is a client. def initialize(service_name : String) super() @@ -15,12 +12,9 @@ class IPC::Client < IPC::Context m = String.new r.error_message.to_slice raise Exception.new "error during connection establishment: #{m}" end - end - def initialize(name : String) - super() - @connection = IPC::Connection.new name - self << @connection + # Very important as there are filesystem side-effects. + at_exit { close } end def initialize(name : String, &block : Proc(IPC::Event::Events|Exception, Nil)) @@ -28,25 +22,4 @@ class IPC::Client < IPC::Context ::loop &block close end - - def send(*args) - @connection.send *args - end - - def read(*args) - @connection.read *args - end - - # sanitizer - def fd - @connection.fd - end - - def loop(&block : Proc(IPC::Event::Events|Exception, Nil)) - super(nil, &block) - end - - def close - @connection.close - end end diff --git a/src/ipc/connection.cr b/src/ipc/connection.cr index 5b2a04e..9d38b71 100644 --- a/src/ipc/connection.cr +++ b/src/ipc/connection.cr @@ -9,6 +9,7 @@ class IPC::Connection def initialize @connection = LibIPC::Connection.new + @connection.type = LibIPC::ConnectionType::Server @pollfd = LibIPC::Pollfd.new end diff --git a/src/ipc/context.cr b/src/ipc/context.cr index 51ce20e..0cc4577 100644 --- a/src/ipc/context.cr +++ b/src/ipc/context.cr @@ -22,13 +22,13 @@ class IPC::Context close end - def << (client : IPC::Connection) - r = LibIPC.ipc_add(self.pointer, client.pointer, pointerof(client.pollfd)) - if r.error_code != 0 - m = String.new r.error_message.to_slice - raise Exception.new "cannot add an arbitrary file descriptor: #{m}" - end - end + # def << (client : IPC::Connection) + # r = LibIPC.ipc_add(self.pointer, client.pointer, pointerof(client.pollfd)) + # if r.error_code != 0 + # m = String.new r.error_message.to_slice + # raise Exception.new "cannot add an arbitrary file descriptor: #{m}" + # end + # end def << (fd : Int) r = LibIPC.ipc_add_fd(self.pointer, fd) @@ -38,16 +38,14 @@ class IPC::Context end end - def remove (client : IPC::Connection) - c = client.connection - r = LibIPC.ipc_del(self.pointer, pointerof(c)) + def remove_index (index : UInt32) + r = LibIPC.ipc_del(self.pointer, index) if r.error_code != 0 m = String.new r.error_message.to_slice - raise Exception.new "cannot remove a client: #{m}" + raise Exception.new "cannot remove an arbitrary file descriptor: #{m}" end end - - def remove_fd (fd : Int) + def remove_fd (fd : Int32) r = LibIPC.ipc_del_fd(self.pointer, fd) if r.error_code != 0 m = String.new r.error_message.to_slice @@ -163,13 +161,7 @@ class IPC::Context def close return if @closed - - r = LibIPC.ipc_close_all(self.pointer) - if r.error_code != 0 - m = String.new r.error_message.to_slice - raise Exception.new "cannot correctly close the connection: #{m}" - end - + LibIPC.ipc_close_all(self.pointer) @closed = true end diff --git a/src/ipc/service.cr b/src/ipc/service.cr index ff64136..c24bd48 100644 --- a/src/ipc/service.cr +++ b/src/ipc/service.cr @@ -1,12 +1,10 @@ -require "./lowlevel" -require "./message" -require "./event" -require "./connection" +require "./client" -# the server is a connection with a different function call for the connection +# the server is a client with a different init function # ipc_connection => ipc_server_init -class IPC::Server < IPC::Connection +class IPC::Server < IPC::Client def initialize(name : String) + super r = LibIPC.ipc_server_init(self.pointer, name) if r.error_code != 0 m = String.new r.error_message.to_slice