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
version: 0.1.0
version: 0.1.2
authors:
- Philippe Pittoli <karchnu@karchnu.fr>
@ -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

View File

@ -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.

View File

@ -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))