From f4c9ccad6061d9acdd2d8fe3a115dee2ce0c88b1 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Tue, 14 Jul 2020 17:02:14 +0200 Subject: [PATCH] fixing client init --- src/ipc/client.cr | 34 +++++++++++++++++----------------- tests/pongc.cr | 1 + 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/ipc/client.cr b/src/ipc/client.cr index 280328d..31960dc 100644 --- a/src/ipc/client.cr +++ b/src/ipc/client.cr @@ -2,6 +2,23 @@ class IPC::Client < IPC::Context property server_fd : Int32? + # By default, this is a client. + def initialize(service_name : String) + super() + serverfd = 0 + + r = LibIPC.ipc_connection(self.pointer, service_name, pointerof(serverfd)) + if r.error_code != 0 + m = String.new r.error_message.to_slice + raise Exception.new "error during connection establishment: #{m}" + end + + @server_fd = serverfd + + # Very important as there are filesystem side-effects. + at_exit { close } + end + def read unless (fd = @server_fd).nil? message = LibIPC::Message.new @@ -15,21 +32,4 @@ class IPC::Client < IPC::Context raise "Client not connected to a server" end end - - # By default, this is a client. - def initialize(service_name : String) - super() - serverfd = 0 - - r = LibIPC.ipc_connection(self.pointer, service_name, pointerof(serverfd)) - if r.error_code != 0 - m = String.new r.error_message.to_slice - raise Exception.new "error during connection establishment: #{m}" - end - - @server_fd = server_fd - - # Very important as there are filesystem side-effects. - at_exit { close } - end end diff --git a/tests/pongc.cr b/tests/pongc.cr index e95b6aa..509c301 100644 --- a/tests/pongc.cr +++ b/tests/pongc.cr @@ -5,6 +5,7 @@ client = IPC::Client.new "pong" server_fd = client.server_fd if server_fd.nil? + puts "there is no server_fd!!" exit 1 end