New LibIPC API.

This commit is contained in:
Philippe PITTOLI 2024-06-15 22:31:01 +02:00
parent 736b5b5472
commit 8cc19999f5
3 changed files with 19 additions and 16 deletions

View File

@ -1,5 +1,5 @@
name: ipc name: ipc
version: 0.1.0 version: 0.1.2
authors: authors:
- Philippe Pittoli <karchnu@karchnu.fr> - Philippe Pittoli <karchnu@karchnu.fr>
@ -7,7 +7,7 @@ authors:
description: | description: |
High-level Crystal bindings to libipc. High-level Crystal bindings to libipc.
crystal: 1.7.1 crystal: 1.12.2
dependencies: dependencies:
cbor: cbor:
@ -15,6 +15,6 @@ dependencies:
branch: master branch: master
libraries: libraries:
libipc: ">= 0.1" libipc: ">= 0.1.2"
license: ISC license: ISC

View File

@ -18,8 +18,8 @@ lib LibIPC
fun service_init = ipc_service_init(Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int 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 fun connect_service = ipc_connect_service(Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int
# Context EventType index fd buffer buflen # Context EventType index serverfd newclientfd buffer buflen
fun wait = ipc_wait_event(Void*, UInt8*, LibC::UInt64T*, LibC::Int*, UInt8*, LibC::UInt64T*) : LibC::Int fun wait = ipc_wait_event(Void*, UInt8*, LibC::UInt64T*, LibC::Int*, LibC::Int*, UInt8*, LibC::UInt64T*) : LibC::Int
# Sending a message NOW. # Sending a message NOW.
# WARNING: doesn't wait the fd to become available. # WARNING: doesn't wait the fd to become available.

View File

@ -5,12 +5,13 @@ class IPC
@reception_buffer_len : LibC::UInt64T = 2_000_000 @reception_buffer_len : LibC::UInt64T = 2_000_000
class Event class Event
property type : LibIPC::EventType property type : LibIPC::EventType # Event type.
property index : LibC::UInt64T property index : LibC::UInt64T # Index of the connection on which the action happens.
property fd : Int32 property fd : Int32 # File descriptor on which the action happens.
property message : Array(UInt8)? = nil 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 @type = LibIPC::EventType.new t
if buflen > 0 if buflen > 0
# Array -> Pointer -> Slice -> Array # Array -> Pointer -> Slice -> Array
@ -111,19 +112,21 @@ class IPC
eventtype : UInt8 = 0 eventtype : UInt8 = 0
index : LibC::UInt64T = 0 index : LibC::UInt64T = 0
fd : Int32 = 0 fd : Int32 = 0
newfd : Int32 = 0
buflen = @reception_buffer_len buflen = @reception_buffer_len
ret = LibIPC.wait(@context, ret = LibIPC.wait(@context,
pointerof(eventtype), pointerof(eventtype), # event type
pointerof(index), pointerof(index), # index of the connection on which the action happens
pointerof(fd), pointerof(fd), # fd on which the action happens
@reception_buffer.to_unsafe, pointerof(newfd), # new client fd (in case of a new connection)
pointerof(buflen)) @reception_buffer.to_unsafe, # received message
pointerof(buflen)) # received message length
if ret != 0 if ret != 0
raise "Oh noes, 'wait' iz brkn" raise "Oh noes, 'wait' iz brkn"
end end
Event.new(eventtype, index, fd, @reception_buffer, buflen) Event.new(eventtype, index, fd, newfd, @reception_buffer, buflen)
end end
def loop(&block : Proc(IPC::Event, Nil)) def loop(&block : Proc(IPC::Event, Nil))