From 07f209955368c646a686460a1d18108b0dc2ce12 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Fri, 17 Jan 2020 22:02:24 +0100 Subject: [PATCH] -debug pp!, +Connection init for simple fd, +Message#to_packet --- shard.yml | 2 +- src/ipc/connection.cr | 11 +++++++---- src/ipc/message.cr | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/shard.yml b/shard.yml index 2c110c7..880cae7 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: ipc -version: 0.5.0 +version: 0.5.1 authors: - Philippe Pittoli diff --git a/src/ipc/connection.cr b/src/ipc/connection.cr index c4b5c12..c1da50d 100644 --- a/src/ipc/connection.cr +++ b/src/ipc/connection.cr @@ -14,10 +14,6 @@ class IPC::Connection def initialize(service_name : String) @connection = LibIPC::Connection.new - # TODO - pp! self.pointer - pp! @connection - r = LibIPC.ipc_connection(LibC.environ, self.pointer, service_name) if r.error_code != 0 m = String.new r.error_message.to_slice @@ -33,6 +29,13 @@ class IPC::Connection close end + # Adds a new connection based on the socket file descriptor + def initialize(fd : Int32) + external_connection = LibIPC::Connection.new + external_connection.fd = fd + initialize(external_connection) + end + # sanitizer def fd @connection.fd diff --git a/src/ipc/message.cr b/src/ipc/message.cr index b5c4cae..8b2fa79 100644 --- a/src/ipc/message.cr +++ b/src/ipc/message.cr @@ -1,6 +1,21 @@ require "./lowlevel" class IPC::Message + + def self.to_packet (user_type : Int, message : String) + payload = Bytes.new (6 + message.to_slice.size) + + # true start + payload[0] = 1.to_u8 + IO::ByteFormat::NetworkEndian.encode message.to_slice.size, (payload + 1) + + # second part: user message + payload[5] = user_type.to_u8 + (payload + 6).copy_from message.to_slice + + return payload + end + getter mtype : UInt8 # libipc message type property type : UInt8 # libipc user message type property payload : Bytes @@ -32,6 +47,10 @@ class IPC::Message initialize(mtype, type, Bytes.new(payload.to_unsafe, payload.bytesize)) end + def to_packet + IPC::Message.to_packet @type, String.new(@payload) + end + def to_s "(internal) type #{@mtype}, (user) type #{@type}, payload #{String.new @payload}" end