From 8998c9adc8d42eed86af79cc86317ae1dde49bd1 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Thu, 23 Jul 2020 19:09:15 +0200 Subject: [PATCH] wait_event simplification & send_now --- src/ipc/client.cr | 4 ++++ src/ipc/context.cr | 49 +++++++++++++++++++++++++++++++++------------ src/ipc/lowlevel.cr | 4 ++++ 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/ipc/client.cr b/src/ipc/client.cr index 31960dc..4f2dd8c 100644 --- a/src/ipc/client.cr +++ b/src/ipc/client.cr @@ -19,6 +19,10 @@ class IPC::Client < IPC::Context at_exit { close } end + def fd + @server_fd + end + def read unless (fd = @server_fd).nil? message = LibIPC::Message.new diff --git a/src/ipc/context.cr b/src/ipc/context.cr index 28ed66a..20758ef 100644 --- a/src/ipc/context.cr +++ b/src/ipc/context.cr @@ -40,13 +40,13 @@ class IPC::Context end end - def wait_event(&block) : IPC::Event::Events | Exception + def wait_event : IPC::Event::Events | Exception event = LibIPC::Event.new r = LibIPC.ipc_wait_event self.pointer, pointerof(event), pointerof(@timer) if r.error_code != 0 m = String.new r.error_message.to_slice - yield IPC::Exception.new "error waiting for a new event: #{m}" + return IPC::Exception.new "error waiting for a new event: #{m}" end eventtype = event.type.unsafe_as(LibIPC::EventType) @@ -88,32 +88,55 @@ class IPC::Context @timer = @base_timer end - yield wait_event &block + break if yield wait_event end end + def send_now(message : LibIPC::Message) + r = LibIPC.ipc_write_fd(message.fd, pointerof(message)) + if r.error_code != 0 + m = String.new r.error_message.to_slice + raise Exception.new "error writing a message: #{m}" + end + end + + def send_now(message : IPC::Message) + send_now fd: message.fd, utype: message.utype, payload: message.payload + end + + def send_now(fd : Int32, utype : UInt8, payload : Bytes) + message = LibIPC::Message.new fd: fd, + type: LibIPC::MessageType::Data.to_u8, + user_type: utype, + length: payload.bytesize, + payload: payload.to_unsafe + send_now message + end + + def send(message : LibIPC::Message) + r = LibIPC.ipc_write(self.pointer, pointerof(message)) + if r.error_code != 0 + m = String.new r.error_message.to_slice + raise Exception.new "error writing a message: #{m}" + end + end + def send(message : IPC::Message) + send fd: message.fd, utype: message.utype, payload: message.payload + end + def send(fd : Int32, utype : UInt8, payload : Bytes) message = LibIPC::Message.new fd: fd, type: LibIPC::MessageType::Data.to_u8, user_type: utype, length: payload.bytesize, payload: payload.to_unsafe - - r = LibIPC.ipc_write(self.pointer, pointerof(message)) - if r.error_code != 0 - m = String.new r.error_message.to_slice - raise Exception.new "error writing a message: #{m}" - end + send message end def send(fd : Int32, utype : UInt8, payload : String) send(fd, utype, Bytes.new(payload.to_unsafe, payload.bytesize)) end - def send(message : IPC::Message) - send(message.fd, message.utype, message.payload) - end - def read(index : UInt32) message = LibIPC::Message.new r = LibIPC.ipc_read(self.pointer, index, pointerof(message)) diff --git a/src/ipc/lowlevel.cr b/src/ipc/lowlevel.cr index 600bd0d..1b04252 100644 --- a/src/ipc/lowlevel.cr +++ b/src/ipc/lowlevel.cr @@ -118,6 +118,10 @@ lib LibIPC # Sending a message (will wait the fd to become available for IO operations). fun ipc_write(Ctx*, Message*) : IPCError + # Sending a message NOW. + # WARNING: unbuffered send do not wait the fd to become available. + fun ipc_write_fd(Int32, Message*) : IPCError + # 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*