From 5ee0a6e6e0ba5716e430874953b9870051c0ca3a Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 1 Feb 2023 02:06:00 +0100 Subject: [PATCH] Crystal bindings: code structure. --- .../crystal/some-crystal-app/src/bindings.cr | 38 +++++++++++++ ...-crystal-app.cr => high-level-bindings.cr} | 53 ------------------- .../crystal/some-crystal-app/src/libauth.cr | 25 +++++++++ zig-impl/crystal/some-crystal-app/src/main.cr | 3 ++ .../crystal/some-crystal-app/src/message.cr | 25 +++++++++ 5 files changed, 91 insertions(+), 53 deletions(-) create mode 100644 zig-impl/crystal/some-crystal-app/src/bindings.cr rename zig-impl/crystal/some-crystal-app/src/{some-crystal-app.cr => high-level-bindings.cr} (61%) create mode 100644 zig-impl/crystal/some-crystal-app/src/libauth.cr create mode 100644 zig-impl/crystal/some-crystal-app/src/main.cr create mode 100644 zig-impl/crystal/some-crystal-app/src/message.cr diff --git a/zig-impl/crystal/some-crystal-app/src/bindings.cr b/zig-impl/crystal/some-crystal-app/src/bindings.cr new file mode 100644 index 0000000..878af8d --- /dev/null +++ b/zig-impl/crystal/some-crystal-app/src/bindings.cr @@ -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 diff --git a/zig-impl/crystal/some-crystal-app/src/some-crystal-app.cr b/zig-impl/crystal/some-crystal-app/src/high-level-bindings.cr similarity index 61% rename from zig-impl/crystal/some-crystal-app/src/some-crystal-app.cr rename to zig-impl/crystal/some-crystal-app/src/high-level-bindings.cr index a563ebd..e93fb98 100644 --- a/zig-impl/crystal/some-crystal-app/src/some-crystal-app.cr +++ b/zig-impl/crystal/some-crystal-app/src/high-level-bindings.cr @@ -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. diff --git a/zig-impl/crystal/some-crystal-app/src/libauth.cr b/zig-impl/crystal/some-crystal-app/src/libauth.cr new file mode 100644 index 0000000..c994dbc --- /dev/null +++ b/zig-impl/crystal/some-crystal-app/src/libauth.cr @@ -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 diff --git a/zig-impl/crystal/some-crystal-app/src/main.cr b/zig-impl/crystal/some-crystal-app/src/main.cr new file mode 100644 index 0000000..56c9695 --- /dev/null +++ b/zig-impl/crystal/some-crystal-app/src/main.cr @@ -0,0 +1,3 @@ +require "./bindings.cr" +require "./message.cr" +require "./high-level-bindings.cr" diff --git a/zig-impl/crystal/some-crystal-app/src/message.cr b/zig-impl/crystal/some-crystal-app/src/message.cr new file mode 100644 index 0000000..7b48fc5 --- /dev/null +++ b/zig-impl/crystal/some-crystal-app/src/message.cr @@ -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