From 05e1c366459625b73cd7aa347d4637fc4ac56dc5 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 1 Jan 2020 14:29:05 +0100 Subject: [PATCH] Bindings adapted to libipc v0.5.0. 'Events' alias no more top-level. --- shard.yml | 4 +- src/ipc/client.cr | 4 +- src/ipc/connection.cr | 43 ++++++++------- src/ipc/event.cr | 4 +- src/ipc/lowlevel.cr | 125 +++++++++++------------------------------- src/ipc/service.cr | 16 +++--- 6 files changed, 70 insertions(+), 126 deletions(-) diff --git a/shard.yml b/shard.yml index 01cd0e8..2c110c7 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: ipc -version: 0.4.0 +version: 0.5.0 authors: - Philippe Pittoli @@ -9,6 +9,6 @@ description: | High-level Crystal bindings to libipc. libraries: - libipc: ">= 0.4" + libipc: ">= 0.5" license: ISC diff --git a/src/ipc/client.cr b/src/ipc/client.cr index d8d7caf..124df1f 100644 --- a/src/ipc/client.cr +++ b/src/ipc/client.cr @@ -14,7 +14,7 @@ class IPC::Client < IPC::Connections self << @connection end - def initialize(name : String, &block : Proc(Events|Exception, Nil)) + def initialize(name : String, &block : Proc(IPC::Event::Events|Exception, Nil)) initialize name ::loop &block close @@ -33,7 +33,7 @@ class IPC::Client < IPC::Connections @connection.fd end - def loop(&block : Proc(Events|Exception, Nil)) + def loop(&block : Proc(IPC::Event::Events|Exception, Nil)) super(nil, &block) end diff --git a/src/ipc/connection.cr b/src/ipc/connection.cr index 7e39412..c4b5c12 100644 --- a/src/ipc/connection.cr +++ b/src/ipc/connection.cr @@ -13,9 +13,14 @@ class IPC::Connection def initialize(service_name : String) @connection = LibIPC::Connection.new + + # TODO + pp! self.pointer + pp! @connection + r = LibIPC.ipc_connection(LibC.environ, self.pointer, service_name) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "error during connection establishment: #{m}" end end @@ -37,8 +42,8 @@ class IPC::Connection message = LibIPC::Message.new type: LibIPC::MessageType::Data.to_u8, user_type: type, length: payload.bytesize, payload: payload.to_unsafe r = LibIPC.ipc_write(self.pointer, pointerof(message)) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "error writing a message: #{m}" end end @@ -54,8 +59,8 @@ class IPC::Connection def read message = LibIPC::Message.new r = LibIPC.ipc_read(pointerof(@connection), pointerof(message)) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "error reading a message: #{m}" end @@ -66,8 +71,8 @@ class IPC::Connection return if @closed r = LibIPC.ipc_close(self.pointer) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "cannot correctly close the connection: #{m}" end @@ -106,16 +111,16 @@ class IPC::Connections def << (client : IPC::Connection) r = LibIPC.ipc_add(self.pointer, client.pointer) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + 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) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + 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 @@ -123,16 +128,16 @@ class IPC::Connections def remove (client : IPC::Connection) c = client.connection r = LibIPC.ipc_del(self.pointer, pointerof(c)) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "cannot remove a client: #{m}" end end def remove_fd (fd : Int) r = LibIPC.ipc_del_fd(self.pointer, fd) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "cannot remove an arbitrary file descriptor: #{m}" end end @@ -146,8 +151,8 @@ class IPC::Connections end r = LibIPC.ipc_wait_event self.pointer, serverp, pointerof(event), pointerof(@timer) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice yield IPC::Exception.new "error waiting for a new event: #{m}" end @@ -166,7 +171,7 @@ class IPC::Connections return eventtype, IPC::Message.new(message), connection end - def loop(server : IPC::Connection | IPC::Server | ::Nil, &block : Proc(Events|Exception, Nil)) + def loop(server : IPC::Connection | IPC::Server | ::Nil, &block : Proc(IPC::Event::Events|Exception, Nil)) if @base_timer > 0 && @timer == 0 @timer = @base_timer end diff --git a/src/ipc/event.cr b/src/ipc/event.cr index 3c793e9..4d655d0 100644 --- a/src/ipc/event.cr +++ b/src/ipc/event.cr @@ -3,6 +3,8 @@ require "./message" require "./connection" class IPC::Event + alias Events = IPC::Event::Timer | IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message | IPC::Event::ExtraSocket | IPC::Event::Switch | IPC::Event::LookUp + class Timer end @@ -35,5 +37,3 @@ class IPC::Event end end -alias Events = IPC::Event::Timer | IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message | IPC::Event::ExtraSocket | IPC::Event::Switch | IPC::Event::LookUp - diff --git a/src/ipc/lowlevel.cr b/src/ipc/lowlevel.cr index 168d034..ecd75fc 100644 --- a/src/ipc/lowlevel.cr +++ b/src/ipc/lowlevel.cr @@ -24,70 +24,6 @@ lib LibIPC size : LibC::UInt end - enum Errors - None = 0 - NotEnoughMemory - ClosedRecipient - ServerInitNoEnvironmentParam - ServerInitNoServiceParam - ServerInitNoServerNameParam - ServerInitMalloc - ConnectionNoServer - ConnectionNoServiceName - ConnectionNoEnvironmentParam - ConnectionGenNoCinfo - AcceptNoServiceParam - AcceptNoClientParam - Accept - HandleNewConnectionNoCinfoParam - HandleNewConnectionNoCinfosParam - WaitEventSelect - WaitEventNoClientsParam - WaitEventNoEventParam - HandleNewConnectionMalloc - AddEmptyList - AddNoParamClients - AddNoParamClient - AddFdNoParamCinfos - DelEmptyList - DelEmptiedList - DelCannotFindClient - DelNoClientsParam - DelNoClientParam - UsockSend - UsockConnectSocket - UsockConnectWrongFileDescriptor - UsockConnectEmptyPath - UsockConnectConnect - UsockClose - UsockRemoveUnlink - UsockRemoveNoFile - UsockInitEmptyFileDescriptor - UsockInitWrongFileDescriptor - UsockInitEmptyPath - UsockInitBind - UsockInitListen - UsockAcceptPathFileDescriptor - UsockAccept - UsockRecvNoBuffer - UsockRecvNoLength - UsockRecv - MessageNewNoMessageParam - MessageReadNomessageparam - MessageWriteNoMessageParam - MessageWriteNotEnoughData - MessageFormatNoMessageParam - MessageFormatInconsistentParams - MessageFormatLength - MessageFormatWriteEmptyMessage - MessageFormatWriteEmptyMsize - MessageFormatWriteEmptyBuffer - MessageFormatReadEmptyMessage - MessageFormatReadEmptyBuffer - MessageFormatReadMessageSize - MessageEmptyEmptyMessageList - end - enum MessageType ServerClose Error @@ -120,49 +56,52 @@ lib LibIPC message : Message* end - fun ipc_server_init(env : LibC::Char**, connection : Connection*, sname : LibC::Char*) : LibC::Int - fun ipc_server_close(Connection*) : LibC::Int - fun ipc_close(Connection*) : LibC::Int + struct IPCError + # This is the size of an enumeration in C. + error_code : UInt32 + error_message : LibC::Char[8192] + end + + fun ipc_server_init(env : LibC::Char**, connection : Connection*, sname : LibC::Char*) : IPCError + fun ipc_server_close(Connection*) : IPCError + fun ipc_close(Connection*) : IPCError # connection to a service - fun ipc_connection(LibC::Char**, Connection*, LibC::Char*) : LibC::Int + fun ipc_connection(LibC::Char**, Connection*, LibC::Char*) : IPCError - fun ipc_read(Connection*, Message*) : LibC::Int - fun ipc_write(Connection*, Message*) : LibC::Int + fun ipc_read(Connection*, Message*) : IPCError + fun ipc_write(Connection*, Message*) : IPCError - fun ipc_wait_event(Connections*, Connection*, Event*, LibC::Long*) : LibC::Int + fun ipc_wait_event(Connections*, Connection*, Event*, LibC::Long*) : IPCError - fun ipc_add(Connections*, Connection*) : LibC::Int - fun ipc_del(Connections*, Connection*) : LibC::Int - fun ipc_add_fd(Connections*, LibC::Int) : LibC::Int - fun ipc_del_fd(Connections*, LibC::Int) : LibC::Int + fun ipc_add(Connections*, Connection*) : IPCError + fun ipc_del(Connections*, Connection*) : IPCError + fun ipc_add_fd(Connections*, LibC::Int) : IPCError + fun ipc_del_fd(Connections*, LibC::Int) : IPCError - fun ipc_connection_copy(Connection*) : Connection* - fun ipc_connection_eq(Connection*, Connection*) : LibC::Int + fun ipc_connection_gen(Connection*, LibC::UInt, LibC::UInt) : IPCError - fun ipc_connection_gen(Connection*, LibC::UInt, LibC::UInt) + fun ipc_connections_free(Connections*) # Void + fun ipc_connections_close(Connections*) # Void - fun ipc_connections_free(Connections*) - fun ipc_connections_close(Connections*) - fun ipc_get(Connections*) + # This function let the user get the default error message based on the error code. + # The error message is contained in the IPCError structure, this function should not be used, in most cases. fun ipc_errors_get (LibC::Int) : LibC::Char* # networkd-related functions - fun ipc_wait_event_networkd(Connections*, Connection*, Event*, Switchings*, LibC::Long*) : LibC::Int + fun ipc_wait_event_networkd(Connections*, Connection*, Event*, Switchings*, LibC::Long*) : IPCError - fun ipc_receive_fd (sock : LibC::Int, fd : LibC::Int*) : LibC::Int - fun ipc_provide_fd (sock : LibC::Int, fd : LibC::Int) : LibC::Int + fun ipc_receive_fd (sock : LibC::Int, fd : LibC::Int*) : IPCError + fun ipc_provide_fd (sock : LibC::Int, fd : LibC::Int ) : IPCError - fun ipc_switching_add (switch : Switchings*, fd1 : LibC::Int, fd2 : LibC::Int) - fun ipc_switching_del (switch : Switchings*, fd : LibC::Int ) : LibC::Int - fun ipc_switching_get (switch : Switchings*, fd : LibC::Int ) : LibC::Int - fun ipc_switching_free (switch : Switchings* ) : LibC::Int - fun ipc_switching_print (switch : Switchings*) + fun ipc_switching_add (switch : Switchings*, fd1 : LibC::Int, fd2 : LibC::Int) # Void + fun ipc_switching_del (switch : Switchings*, fd : LibC::Int ) : LibC::Int + fun ipc_switching_get (switch : Switchings*, fd : LibC::Int ) : LibC::Int + fun ipc_switching_free (switch : Switchings* ) # Void # non public functions (for testing purposes) - fun service_path (path : LibC::Char*, sname : LibC::Char*, index : Int32, version : Int32) : LibC::Int - fun log_get_logfile_dir (buf : LibC::Char*, size : LibC::UInt) : LibC::Char* - fun log_get_logfile_name (buf : LibC::Char*, size : LibC::UInt) - fun ipc_connections_print(Connections*) + fun ipc_switching_print (switch : Switchings*) # Void + fun service_path (path : LibC::Char*, sname : LibC::Char*, index : Int32, version : Int32) : IPCError + fun ipc_connections_print (Connections*) # Void end diff --git a/src/ipc/service.cr b/src/ipc/service.cr index 6eb96ca..c7094bd 100644 --- a/src/ipc/service.cr +++ b/src/ipc/service.cr @@ -10,8 +10,8 @@ class IPC::Server < IPC::Connection def initialize(name : String) @connection = LibIPC::Connection.new r = LibIPC.ipc_server_init(LibC.environ, self.pointer, name) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "cannot initialize the server named #{name}: #{m}" end @@ -23,8 +23,8 @@ class IPC::Server < IPC::Connection return if @closed r = LibIPC.ipc_server_close(self.pointer) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice raise Exception.new "cannot close the server correctly: #{m}" end @@ -40,7 +40,7 @@ class IPC::Service < IPC::Connections super() end - def initialize(name : String, &block : Proc(Events|Exception, Nil)) + def initialize(name : String, &block : Proc(IPC::Event::Events|Exception, Nil)) initialize name loop &block close @@ -51,7 +51,7 @@ class IPC::Service < IPC::Connections @service_info.fd end - def loop(&block : Proc(Events|Exception, Nil)) + def loop(&block : Proc(IPC::Event::Events|Exception, Nil)) super(@service_info, &block) end @@ -76,8 +76,8 @@ class IPC::SwitchingService < IPC::Service serverp = server.pointer r = LibIPC.ipc_wait_event_networkd self.pointer, serverp, pointerof(event), @switch.pointer, pointerof(@timer) - if r != 0 - m = String.new LibIPC.ipc_errors_get (r) + if r.error_code != 0 + m = String.new r.error_message.to_slice yield IPC::Exception.new "error waiting for a new event: #{m}" end