Obsolete
/
ipc.cr-old
Archived
3
0
Fork 0

wait_event simplification & send_now

dev
Karchnu 2020-07-23 19:09:15 +02:00
parent c7c0909f35
commit 8998c9adc8
3 changed files with 44 additions and 13 deletions

View File

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

View File

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

View File

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