2023-01-21 07:32:44 +01:00
|
|
|
class IPC
|
2023-01-21 19:08:24 +01:00
|
|
|
# Reception buffer with a big capacity.
|
|
|
|
# Allocated once.
|
|
|
|
@reception_buffer = Array(UInt8).new 2_000_000
|
|
|
|
@reception_buffer_len : LibC::UInt64T = 2_000_000
|
|
|
|
|
2023-01-21 07:32:44 +01:00
|
|
|
class Event
|
|
|
|
property type : LibIPC::EventType
|
|
|
|
property index : LibC::UInt64T
|
|
|
|
property fd : Int32
|
2023-01-21 19:08:24 +01:00
|
|
|
property message : Array(UInt8)? = nil
|
2023-01-21 07:32:44 +01:00
|
|
|
|
|
|
|
def initialize(t : UInt8, @index, @fd, buffer, buflen)
|
|
|
|
@type = LibIPC::EventType.new t
|
2023-01-21 19:08:24 +01:00
|
|
|
if buflen > 0
|
|
|
|
# Array -> Pointer -> Slice -> Array
|
|
|
|
@message = buffer.to_unsafe.to_slice(buflen).to_a
|
|
|
|
end
|
2023-01-21 07:32:44 +01:00
|
|
|
end
|
|
|
|
end
|
2023-01-21 05:52:18 +01:00
|
|
|
|
2023-01-21 07:32:44 +01:00
|
|
|
def initialize()
|
|
|
|
@context = Pointer(Void).null
|
|
|
|
LibIPC.init(pointerof(@context))
|
|
|
|
at_exit { deinit }
|
|
|
|
end
|
2023-01-21 05:52:18 +01:00
|
|
|
|
2023-01-24 15:22:51 +01:00
|
|
|
# Closes all connections then remove the structure from memory.
|
2023-01-21 07:32:44 +01:00
|
|
|
def deinit
|
2023-01-21 19:08:24 +01:00
|
|
|
LibIPC.deinit(pointerof(@context))
|
2023-01-21 07:32:44 +01:00
|
|
|
end
|
2023-01-21 05:52:18 +01:00
|
|
|
|
2023-01-25 04:47:17 +01:00
|
|
|
def service_init(name : String) : Int
|
|
|
|
fd = uninitialized Int32
|
|
|
|
puts "service name: #{name}"
|
|
|
|
puts "service name len: #{name.size}"
|
|
|
|
if LibIPC.service_init(@context, pointerof(fd), name, name.size) != 0
|
|
|
|
raise "oh noes, 'service_init' iz brkn"
|
|
|
|
end
|
|
|
|
fd
|
|
|
|
end
|
|
|
|
|
2023-01-21 07:32:44 +01:00
|
|
|
def connect(name : String) : Int
|
|
|
|
fd = uninitialized Int32
|
|
|
|
if LibIPC.connect_service(@context, pointerof(fd), name, name.size) != 0
|
2023-01-24 15:22:51 +01:00
|
|
|
raise "oh noes, 'connect_service' iz brkn"
|
2023-01-21 07:32:44 +01:00
|
|
|
end
|
|
|
|
fd
|
|
|
|
end
|
|
|
|
|
2023-02-01 20:31:44 +01:00
|
|
|
def timer(value : LibC::Int)
|
|
|
|
LibIPC.timer(@context, value)
|
|
|
|
end
|
|
|
|
|
2023-01-21 07:32:44 +01:00
|
|
|
def write(fd : Int, string : String)
|
|
|
|
self.write(fd, string.to_unsafe, string.size.to_u64)
|
|
|
|
end
|
2023-01-21 05:52:18 +01:00
|
|
|
|
2023-01-21 07:32:44 +01:00
|
|
|
def write(fd : Int, buffer : UInt8*, buflen : UInt64)
|
|
|
|
if LibIPC.write(@context, fd, buffer, buflen) != 0
|
2023-01-24 15:22:51 +01:00
|
|
|
raise "oh noes, 'write' iz brkn"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-01 20:31:44 +01:00
|
|
|
def write(fd : Int32, buffer : Bytes)
|
|
|
|
self.write(fd, buffer.to_unsafe, buffer.size.to_u64)
|
|
|
|
end
|
|
|
|
|
2023-01-25 04:47:17 +01:00
|
|
|
def schedule(fd : Int32, string : String)
|
2023-01-24 15:22:51 +01:00
|
|
|
self.schedule(fd, string.to_unsafe, string.size.to_u64)
|
|
|
|
end
|
|
|
|
|
2023-01-25 04:47:17 +01:00
|
|
|
def schedule(fd : Int32, buffer : Array(UInt8), buflen : Int32)
|
|
|
|
self.schedule(fd, buffer.to_unsafe, buflen.to_u64)
|
|
|
|
end
|
|
|
|
|
2023-02-01 20:31:44 +01:00
|
|
|
def schedule(fd : Int32, buffer : Bytes)
|
|
|
|
self.schedule(fd, buffer.to_unsafe, buffer.size.to_u64)
|
|
|
|
end
|
|
|
|
|
2023-01-25 04:47:17 +01:00
|
|
|
def schedule(fd : Int32, buffer : UInt8*, buflen : UInt64)
|
2023-01-24 15:22:51 +01:00
|
|
|
if LibIPC.schedule(@context, fd, buffer, buflen) != 0
|
|
|
|
raise "oh noes, 'schedule' iz brkn"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def close(index : LibC::UInt64T)
|
|
|
|
if LibIPC.close(@context, index) != 0
|
|
|
|
raise "Oh noes, 'close index' iz brkn"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def close(fd : LibC::Int)
|
|
|
|
if LibIPC.close_fd(@context, fd) != 0
|
|
|
|
raise "Oh noes, 'close fd' iz brkn"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def close_all()
|
|
|
|
if LibIPC.close_all(@context) != 0
|
|
|
|
raise "Oh noes, 'close all' iz brkn"
|
2023-01-21 07:32:44 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait() : IPC::Event
|
|
|
|
eventtype : UInt8 = 0
|
|
|
|
index : LibC::UInt64T = 0
|
|
|
|
fd : Int32 = 0
|
2023-01-21 19:08:24 +01:00
|
|
|
buflen = @reception_buffer_len
|
2023-01-21 07:32:44 +01:00
|
|
|
ret = LibIPC.wait(@context,
|
|
|
|
pointerof(eventtype),
|
|
|
|
pointerof(index),
|
|
|
|
pointerof(fd),
|
2023-01-21 19:08:24 +01:00
|
|
|
@reception_buffer.to_unsafe,
|
2023-01-21 07:32:44 +01:00
|
|
|
pointerof(buflen))
|
|
|
|
|
|
|
|
if ret != 0
|
|
|
|
raise "Oh noes, 'wait' iz brkn"
|
|
|
|
end
|
|
|
|
|
2023-01-21 19:08:24 +01:00
|
|
|
Event.new(eventtype, index, fd, @reception_buffer, buflen)
|
2023-01-21 07:32:44 +01:00
|
|
|
end
|
|
|
|
|
2023-01-24 15:22:51 +01:00
|
|
|
def loop(&block : Proc(IPC::Event, Nil))
|
|
|
|
::loop do
|
|
|
|
yield wait
|
|
|
|
end
|
|
|
|
end
|
2023-01-21 07:32:44 +01:00
|
|
|
end
|