Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/zig-impl/crystal/some-crystal-app/src/high-level-bindings.cr

130 lines
2.9 KiB
Crystal
Raw Normal View History

class IPC
# Reception buffer with a big capacity.
# Allocated once.
@reception_buffer = Array(UInt8).new 2_000_000
@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
def initialize(t : UInt8, @index, @fd, buffer, buflen)
@type = LibIPC::EventType.new t
if buflen > 0
# Array -> Pointer -> Slice -> Array
@message = buffer.to_unsafe.to_slice(buflen).to_a
end
end
end
2023-01-21 05:52:18 +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.
def deinit
LibIPC.deinit(pointerof(@context))
end
2023-01-21 05:52:18 +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
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"
end
fd
end
def timer(value : LibC::Int)
LibIPC.timer(@context, value)
end
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
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
def write(fd : Int32, buffer : Bytes)
self.write(fd, buffer.to_unsafe, buffer.size.to_u64)
end
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
def schedule(fd : Int32, buffer : Array(UInt8), buflen : Int32)
self.schedule(fd, buffer.to_unsafe, buflen.to_u64)
end
def schedule(fd : Int32, buffer : Bytes)
self.schedule(fd, buffer.to_unsafe, buffer.size.to_u64)
end
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"
end
end
def wait() : IPC::Event
eventtype : UInt8 = 0
index : LibC::UInt64T = 0
fd : Int32 = 0
buflen = @reception_buffer_len
ret = LibIPC.wait(@context,
pointerof(eventtype),
pointerof(index),
pointerof(fd),
@reception_buffer.to_unsafe,
pointerof(buflen))
if ret != 0
raise "Oh noes, 'wait' iz brkn"
end
Event.new(eventtype, index, fd, @reception_buffer, buflen)
end
2023-01-24 15:22:51 +01:00
def loop(&block : Proc(IPC::Event, Nil))
::loop do
yield wait
end
end
end