Archived
3
0

Bindings adapted to libipc v0.5.0. 'Events' alias no more top-level.

This commit is contained in:
Philippe PITTOLI 2020-01-01 14:29:05 +01:00
parent 7f56378d06
commit 05e1c36645
6 changed files with 70 additions and 126 deletions

View File

@ -1,5 +1,5 @@
name: ipc name: ipc
version: 0.4.0 version: 0.5.0
authors: authors:
- Philippe Pittoli <karchnu@karchnu.fr> - Philippe Pittoli <karchnu@karchnu.fr>
@ -9,6 +9,6 @@ description: |
High-level Crystal bindings to libipc. High-level Crystal bindings to libipc.
libraries: libraries:
libipc: ">= 0.4" libipc: ">= 0.5"
license: ISC license: ISC

View File

@ -14,7 +14,7 @@ class IPC::Client < IPC::Connections
self << @connection self << @connection
end end
def initialize(name : String, &block : Proc(Events|Exception, Nil)) def initialize(name : String, &block : Proc(IPC::Event::Events|Exception, Nil))
initialize name initialize name
::loop &block ::loop &block
close close
@ -33,7 +33,7 @@ class IPC::Client < IPC::Connections
@connection.fd @connection.fd
end end
def loop(&block : Proc(Events|Exception, Nil)) def loop(&block : Proc(IPC::Event::Events|Exception, Nil))
super(nil, &block) super(nil, &block)
end end

View File

@ -13,9 +13,14 @@ class IPC::Connection
def initialize(service_name : String) def initialize(service_name : String)
@connection = LibIPC::Connection.new @connection = LibIPC::Connection.new
# TODO
pp! self.pointer
pp! @connection
r = LibIPC.ipc_connection(LibC.environ, self.pointer, service_name) r = LibIPC.ipc_connection(LibC.environ, self.pointer, service_name)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "error during connection establishment: #{m}" raise Exception.new "error during connection establishment: #{m}"
end end
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 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)) r = LibIPC.ipc_write(self.pointer, pointerof(message))
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "error writing a message: #{m}" raise Exception.new "error writing a message: #{m}"
end end
end end
@ -54,8 +59,8 @@ class IPC::Connection
def read def read
message = LibIPC::Message.new message = LibIPC::Message.new
r = LibIPC.ipc_read(pointerof(@connection), pointerof(message)) r = LibIPC.ipc_read(pointerof(@connection), pointerof(message))
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "error reading a message: #{m}" raise Exception.new "error reading a message: #{m}"
end end
@ -66,8 +71,8 @@ class IPC::Connection
return if @closed return if @closed
r = LibIPC.ipc_close(self.pointer) r = LibIPC.ipc_close(self.pointer)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "cannot correctly close the connection: #{m}" raise Exception.new "cannot correctly close the connection: #{m}"
end end
@ -106,16 +111,16 @@ class IPC::Connections
def << (client : IPC::Connection) def << (client : IPC::Connection)
r = LibIPC.ipc_add(self.pointer, client.pointer) r = LibIPC.ipc_add(self.pointer, client.pointer)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "cannot add an arbitrary file descriptor: #{m}" raise Exception.new "cannot add an arbitrary file descriptor: #{m}"
end end
end end
def << (fd : Int) def << (fd : Int)
r = LibIPC.ipc_add_fd(self.pointer, fd) r = LibIPC.ipc_add_fd(self.pointer, fd)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "cannot add an arbitrary file descriptor: #{m}" raise Exception.new "cannot add an arbitrary file descriptor: #{m}"
end end
end end
@ -123,16 +128,16 @@ class IPC::Connections
def remove (client : IPC::Connection) def remove (client : IPC::Connection)
c = client.connection c = client.connection
r = LibIPC.ipc_del(self.pointer, pointerof(c)) r = LibIPC.ipc_del(self.pointer, pointerof(c))
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "cannot remove a client: #{m}" raise Exception.new "cannot remove a client: #{m}"
end end
end end
def remove_fd (fd : Int) def remove_fd (fd : Int)
r = LibIPC.ipc_del_fd(self.pointer, fd) r = LibIPC.ipc_del_fd(self.pointer, fd)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "cannot remove an arbitrary file descriptor: #{m}" raise Exception.new "cannot remove an arbitrary file descriptor: #{m}"
end end
end end
@ -146,8 +151,8 @@ class IPC::Connections
end end
r = LibIPC.ipc_wait_event self.pointer, serverp, pointerof(event), pointerof(@timer) r = LibIPC.ipc_wait_event self.pointer, serverp, pointerof(event), pointerof(@timer)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
yield IPC::Exception.new "error waiting for a new event: #{m}" yield IPC::Exception.new "error waiting for a new event: #{m}"
end end
@ -166,7 +171,7 @@ class IPC::Connections
return eventtype, IPC::Message.new(message), connection return eventtype, IPC::Message.new(message), connection
end 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 if @base_timer > 0 && @timer == 0
@timer = @base_timer @timer = @base_timer
end end

View File

@ -3,6 +3,8 @@ require "./message"
require "./connection" require "./connection"
class IPC::Event 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 class Timer
end end
@ -35,5 +37,3 @@ class IPC::Event
end end
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

View File

@ -24,70 +24,6 @@ lib LibIPC
size : LibC::UInt size : LibC::UInt
end 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 enum MessageType
ServerClose ServerClose
Error Error
@ -120,49 +56,52 @@ lib LibIPC
message : Message* message : Message*
end end
fun ipc_server_init(env : LibC::Char**, connection : Connection*, sname : LibC::Char*) : LibC::Int struct IPCError
fun ipc_server_close(Connection*) : LibC::Int # This is the size of an enumeration in C.
fun ipc_close(Connection*) : LibC::Int 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 # 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_read(Connection*, Message*) : IPCError
fun ipc_write(Connection*, Message*) : LibC::Int 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_add(Connections*, Connection*) : IPCError
fun ipc_del(Connections*, Connection*) : LibC::Int fun ipc_del(Connections*, Connection*) : IPCError
fun ipc_add_fd(Connections*, LibC::Int) : LibC::Int fun ipc_add_fd(Connections*, LibC::Int) : IPCError
fun ipc_del_fd(Connections*, LibC::Int) : LibC::Int fun ipc_del_fd(Connections*, LibC::Int) : IPCError
fun ipc_connection_copy(Connection*) : Connection* fun ipc_connection_gen(Connection*, LibC::UInt, LibC::UInt) : IPCError
fun ipc_connection_eq(Connection*, Connection*) : LibC::Int
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*) # This function let the user get the default error message based on the error code.
fun ipc_connections_close(Connections*) # The error message is contained in the IPCError structure, this function should not be used, in most cases.
fun ipc_get(Connections*)
fun ipc_errors_get (LibC::Int) : LibC::Char* fun ipc_errors_get (LibC::Int) : LibC::Char*
# networkd-related functions # 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_receive_fd (sock : LibC::Int, fd : LibC::Int*) : IPCError
fun ipc_provide_fd (sock : LibC::Int, fd : LibC::Int) : LibC::Int 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_add (switch : Switchings*, fd1 : LibC::Int, fd2 : LibC::Int) # Void
fun ipc_switching_del (switch : Switchings*, fd : LibC::Int ) : 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_get (switch : Switchings*, fd : LibC::Int ) : LibC::Int
fun ipc_switching_free (switch : Switchings* ) : LibC::Int fun ipc_switching_free (switch : Switchings* ) # Void
fun ipc_switching_print (switch : Switchings*)
# non public functions (for testing purposes) # non public functions (for testing purposes)
fun service_path (path : LibC::Char*, sname : LibC::Char*, index : Int32, version : Int32) : LibC::Int fun ipc_switching_print (switch : Switchings*) # Void
fun log_get_logfile_dir (buf : LibC::Char*, size : LibC::UInt) : LibC::Char* fun service_path (path : LibC::Char*, sname : LibC::Char*, index : Int32, version : Int32) : IPCError
fun log_get_logfile_name (buf : LibC::Char*, size : LibC::UInt) fun ipc_connections_print (Connections*) # Void
fun ipc_connections_print(Connections*)
end end

View File

@ -10,8 +10,8 @@ class IPC::Server < IPC::Connection
def initialize(name : String) def initialize(name : String)
@connection = LibIPC::Connection.new @connection = LibIPC::Connection.new
r = LibIPC.ipc_server_init(LibC.environ, self.pointer, name) r = LibIPC.ipc_server_init(LibC.environ, self.pointer, name)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "cannot initialize the server named #{name}: #{m}" raise Exception.new "cannot initialize the server named #{name}: #{m}"
end end
@ -23,8 +23,8 @@ class IPC::Server < IPC::Connection
return if @closed return if @closed
r = LibIPC.ipc_server_close(self.pointer) r = LibIPC.ipc_server_close(self.pointer)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
raise Exception.new "cannot close the server correctly: #{m}" raise Exception.new "cannot close the server correctly: #{m}"
end end
@ -40,7 +40,7 @@ class IPC::Service < IPC::Connections
super() super()
end end
def initialize(name : String, &block : Proc(Events|Exception, Nil)) def initialize(name : String, &block : Proc(IPC::Event::Events|Exception, Nil))
initialize name initialize name
loop &block loop &block
close close
@ -51,7 +51,7 @@ class IPC::Service < IPC::Connections
@service_info.fd @service_info.fd
end end
def loop(&block : Proc(Events|Exception, Nil)) def loop(&block : Proc(IPC::Event::Events|Exception, Nil))
super(@service_info, &block) super(@service_info, &block)
end end
@ -76,8 +76,8 @@ class IPC::SwitchingService < IPC::Service
serverp = server.pointer serverp = server.pointer
r = LibIPC.ipc_wait_event_networkd self.pointer, serverp, pointerof(event), @switch.pointer, pointerof(@timer) r = LibIPC.ipc_wait_event_networkd self.pointer, serverp, pointerof(event), @switch.pointer, pointerof(@timer)
if r != 0 if r.error_code != 0
m = String.new LibIPC.ipc_errors_get (r) m = String.new r.error_message.to_slice
yield IPC::Exception.new "error waiting for a new event: #{m}" yield IPC::Exception.new "error waiting for a new event: #{m}"
end end