Archived
3
0

Crystal bindings: write & schedule (fd, Bytes).

This commit is contained in:
Philippe Pittoli 2023-02-01 20:31:44 +01:00
parent db561d99b5
commit bfdbacb981
2 changed files with 16 additions and 4 deletions

View File

@ -48,6 +48,10 @@ class IPC
fd fd
end end
def timer(value : LibC::Int)
LibIPC.timer(@context, value)
end
def write(fd : Int, string : String) def write(fd : Int, string : String)
self.write(fd, string.to_unsafe, string.size.to_u64) self.write(fd, string.to_unsafe, string.size.to_u64)
end end
@ -58,6 +62,10 @@ class IPC
end end
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) def schedule(fd : Int32, string : String)
self.schedule(fd, string.to_unsafe, string.size.to_u64) self.schedule(fd, string.to_unsafe, string.size.to_u64)
end end
@ -66,6 +74,10 @@ class IPC
self.schedule(fd, buffer.to_unsafe, buflen.to_u64) self.schedule(fd, buffer.to_unsafe, buflen.to_u64)
end 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) def schedule(fd : Int32, buffer : UInt8*, buflen : UInt64)
if LibIPC.schedule(@context, fd, buffer, buflen) != 0 if LibIPC.schedule(@context, fd, buffer, buflen) != 0
raise "oh noes, 'schedule' iz brkn" raise "oh noes, 'schedule' iz brkn"

View File

@ -15,7 +15,7 @@ module IPCMessage
def initialize(@payload) def initialize(@payload)
end end
def self.deserialize(payload) : UntypedMessage def self.deserialize(payload : Bytes) : UntypedMessage
IPCMessage::UntypedMessage.new payload IPCMessage::UntypedMessage.new payload
end end
@ -49,7 +49,7 @@ module IPCMessage
def serialize def serialize
bytes = Bytes.new (1 + @payload.size) bytes = Bytes.new (1 + @payload.size)
type = @type type = @type
bytes[0] = type.nil? ? 0 : type bytes[0] = type.nil? ? 0.to_u8 : type
bytes[1..].copy_from @payload bytes[1..].copy_from @payload
bytes bytes
end end
@ -60,10 +60,10 @@ end
class IPC class IPC
def schedule(fd : Int32, m : (IPCMessage::TypedMessage | IPCMessage::UntypedMessage)) def schedule(fd : Int32, m : (IPCMessage::TypedMessage | IPCMessage::UntypedMessage))
payload = m.serialize payload = m.serialize
schedule fd, payload, payload.size schedule fd, payload
end end
def write(fd : Int32, m : (IPCMessage::TypedMessage | IPCMessage::UntypedMessage)) def write(fd : Int32, m : (IPCMessage::TypedMessage | IPCMessage::UntypedMessage))
payload = m.serialize payload = m.serialize
write fd, payload, payload.size write fd, payload
end end
end end