Archived
3
0

Crystal bindings: code structure.

This commit is contained in:
Philippe Pittoli 2023-02-01 02:06:00 +01:00
parent 0a836c56cd
commit 5ee0a6e6e0
5 changed files with 91 additions and 53 deletions

View File

@ -0,0 +1,38 @@
@[Link("ipc")]
lib LibIPC
enum EventType
Error # Self explanatory.
Connection # New user.
Disconnection # User disconnected.
MessageRx # Message received.
MessageTx # Message sent.
Timer # Timeout in the poll(2) function.
External # Message received from a non IPC socket.
SwitchRx # Switch subsystem: message received.
SwitchTx # Switch subsystem: message send.
end
fun init = ipc_context_init (Void**) : LibC::Int
fun deinit = ipc_context_deinit (Void**) : Void
fun service_init = ipc_service_init (Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int
fun connect_service = ipc_connect_service(Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int
# Context EventType index fd buffer buflen
fun wait = ipc_wait_event(Void*, UInt8*, LibC::UInt64T*, LibC::Int*, UInt8*, LibC::UInt64T*) : LibC::Int
# Sending a message NOW.
# WARNING: doesn't wait the fd to become available.
fun write = ipc_write(Void*, LibC::Int, UInt8*, LibC::UInt64T) : LibC::Int
# Sending a message (will wait the fd to become available for IO operations).
fun schedule = ipc_schedule(Void*, LibC::Int, UInt8*, LibC::UInt64T) : LibC::Int
fun read = ipc_read_fd (Void*, LibC::Int, UInt8*, LibC::UInt64T*);
fun timer = ipc_context_timer (Void*, LibC::Int)
# Closing connections.
fun close = ipc_close(Void*, LibC::UInt64T) : LibC::Int
fun close_fd = ipc_close_fd(Void*, LibC::Int) : LibC::Int
fun close_all = ipc_close_all(Void*) : LibC::Int
end

View File

@ -1,56 +1,3 @@
@[Link("ipc")]
lib LibIPC
enum EventType
Error # Self explanatory.
Connection # New user.
Disconnection # User disconnected.
MessageRx # Message received.
MessageTx # Message sent.
Timer # Timeout in the poll(2) function.
External # Message received from a non IPC socket.
SwitchRx # Switch subsystem: message received.
SwitchTx # Switch subsystem: message send.
end
fun init = ipc_context_init (Void**) : LibC::Int
fun deinit = ipc_context_deinit (Void**) : Void
fun service_init = ipc_service_init (Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int
fun connect_service = ipc_connect_service(Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int
# Context EventType index fd buffer buflen
fun wait = ipc_wait_event(Void*, UInt8*, LibC::UInt64T*, LibC::Int*, UInt8*, LibC::UInt64T*) : LibC::Int
# Sending a message NOW.
# WARNING: doesn't wait the fd to become available.
fun write = ipc_write(Void*, LibC::Int, UInt8*, LibC::UInt64T) : LibC::Int
# Sending a message (will wait the fd to become available for IO operations).
fun schedule = ipc_schedule(Void*, LibC::Int, UInt8*, LibC::UInt64T) : LibC::Int
fun read = ipc_read_fd (Void*, LibC::Int, UInt8*, LibC::UInt64T*);
fun timer = ipc_context_timer (Void*, LibC::Int)
# Closing connections.
fun close = ipc_close(Void*, LibC::UInt64T) : LibC::Int
fun close_fd = ipc_close_fd(Void*, LibC::Int) : LibC::Int
fun close_all = ipc_close_all(Void*) : LibC::Int
end
# TODO:
module IPCMessage
class TypedMessage
@fd : Int32
@type : UInt8
@payload : Bytes
def initialize(@fd, @type, string : String)
@payload = Bytes.new string
end
def initialize(@fd, @type, @payload)
end
end
end
class IPC
# Reception buffer with a big capacity.
# Allocated once.

View File

@ -0,0 +1,25 @@
require "./message.cr"
# TODO:
class AuthMessage < IPCMessage::TypedMessage
def to_s
content = String.new(@payload.to_unsafe, @payload.size)
"AUTH MSG: type #{@type} fd #{@fd} bytes #{@payload.size} [#{content}]"
rescue
"AUTH MSG: type #{@type} fd #{@fd} bytes #{@payload.size} [#{@payload}]"
end
end
module Request
class Auth < AuthMessage
@type = 1
end
end
module Response
class Auth < AuthMessage
@type = 2
end
end
puts Request::Auth.new(5, "hello I'm karchnu").to_s
puts Response::Auth.new(3, "hello okay you're in").to_s

View File

@ -0,0 +1,3 @@
require "./bindings.cr"
require "./message.cr"
require "./high-level-bindings.cr"

View File

@ -0,0 +1,25 @@
# TODO:
module IPCMessage
class UntypedMessage
@fd : Int32
@payload : Bytes
def initialize(@fd, string : String)
@payload = Bytes.new string.to_unsafe, string.size
end
def initialize(@fd, @payload)
end
end
class TypedMessage < UntypedMessage
@type : UInt8? = nil
def initialize(fd, @type, string : String)
super fd, string
end
def initialize(fd, @type, payload)
super fd, payload
end
def initialize(fd, payload)
super fd, payload
end
end
end