From 8cc19999f54002648baebcbe3d7539c0564c8ade Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 15 Jun 2024 22:31:01 +0200 Subject: [PATCH] New LibIPC API. --- shard.yml | 6 +++--- src/bindings.cr | 4 ++-- src/high-level-bindings.cr | 25 ++++++++++++++----------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/shard.yml b/shard.yml index 7759e92..4f3a0fc 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: ipc -version: 0.1.0 +version: 0.1.2 authors: - Philippe Pittoli @@ -7,7 +7,7 @@ authors: description: | High-level Crystal bindings to libipc. -crystal: 1.7.1 +crystal: 1.12.2 dependencies: cbor: @@ -15,6 +15,6 @@ dependencies: branch: master libraries: - libipc: ">= 0.1" + libipc: ">= 0.1.2" license: ISC diff --git a/src/bindings.cr b/src/bindings.cr index 444062d..dea5128 100644 --- a/src/bindings.cr +++ b/src/bindings.cr @@ -18,8 +18,8 @@ lib LibIPC fun service_init = ipc_service_init(Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int fun connect_service = ipc_connect_service(Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int - # Context EventType index fd buffer buflen - fun wait = ipc_wait_event(Void*, UInt8*, LibC::UInt64T*, LibC::Int*, UInt8*, LibC::UInt64T*) : LibC::Int + # Context EventType index serverfd newclientfd buffer buflen + fun wait = ipc_wait_event(Void*, UInt8*, LibC::UInt64T*, LibC::Int*, LibC::Int*, UInt8*, LibC::UInt64T*) : LibC::Int # Sending a message NOW. # WARNING: doesn't wait the fd to become available. diff --git a/src/high-level-bindings.cr b/src/high-level-bindings.cr index 8a1dcff..50aa65f 100644 --- a/src/high-level-bindings.cr +++ b/src/high-level-bindings.cr @@ -5,12 +5,13 @@ class IPC @reception_buffer_len : LibC::UInt64T = 2_000_000 class Event - property type : LibIPC::EventType - property index : LibC::UInt64T - property fd : Int32 - property message : Array(UInt8)? = nil + property type : LibIPC::EventType # Event type. + property index : LibC::UInt64T # Index of the connection on which the action happens. + property fd : Int32 # File descriptor on which the action happens. + property newfd : Int32 # FD of the new client (when a connection happens). + property message : Array(UInt8)? = nil # Received message. - def initialize(t : UInt8, @index, @fd, buffer, buflen) + def initialize(t : UInt8, @index, @fd, @newfd, buffer, buflen) @type = LibIPC::EventType.new t if buflen > 0 # Array -> Pointer -> Slice -> Array @@ -111,19 +112,21 @@ class IPC eventtype : UInt8 = 0 index : LibC::UInt64T = 0 fd : Int32 = 0 + newfd : Int32 = 0 buflen = @reception_buffer_len ret = LibIPC.wait(@context, - pointerof(eventtype), - pointerof(index), - pointerof(fd), - @reception_buffer.to_unsafe, - pointerof(buflen)) + pointerof(eventtype), # event type + pointerof(index), # index of the connection on which the action happens + pointerof(fd), # fd on which the action happens + pointerof(newfd), # new client fd (in case of a new connection) + @reception_buffer.to_unsafe, # received message + pointerof(buflen)) # received message length if ret != 0 raise "Oh noes, 'wait' iz brkn" end - Event.new(eventtype, index, fd, @reception_buffer, buflen) + Event.new(eventtype, index, fd, newfd, @reception_buffer, buflen) end def loop(&block : Proc(IPC::Event, Nil))