From 405647a24c18330d852d97bf6e05f43a400d762e Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Fri, 21 Jun 2024 21:12:25 +0200 Subject: [PATCH] Hexa dump on both input and output to the client. --- shard.yml | 3 +++ src/ipcd.cr | 32 +++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/shard.yml b/shard.yml index 44841ce..2f7f902 100644 --- a/shard.yml +++ b/shard.yml @@ -11,6 +11,9 @@ targets: dependencies: ipc: git: https://git.baguette.netlib.re/Baguette/ipc.cr + hexa: + branch: master + git: https://git.baguette.netlib.re/Baguette/hexa.cr.git crystal: '>= 1.12.2' diff --git a/src/ipcd.cr b/src/ipcd.cr index 8114eb7..94566f1 100644 --- a/src/ipcd.cr +++ b/src/ipcd.cr @@ -1,4 +1,5 @@ require "ipc" +require "hexa" # TODO: Write documentation for `IPCd` module IPCd @@ -8,11 +9,14 @@ module IPCd ipcd = IPC.new() ipcd.service_init "hello" ipcd.timer 10_000 + timer = 0 ipcd.loop do |event| begin case event.type when LibIPC::EventType::Timer - puts "Timer." + STDOUT.write "Timer #{timer}.\r".to_slice + STDOUT.flush + timer += 1 when LibIPC::EventType::Connection fd = ipcd.connect "pong" @@ -32,10 +36,10 @@ module IPCd puts "Message received from #{event.fd}." when LibIPC::EventType::SwitchRx - puts "Switch message received from #{event.fd}." + #puts "Switch message received from #{event.fd}." when LibIPC::EventType::SwitchTx - puts "Switch message sent to #{event.fd}." + #puts "Switch message sent to #{event.fd}." when LibIPC::EventType::Error puts "And error occured on fd #{event.fd}." @@ -55,13 +59,27 @@ module IPCd end end +#def print_hexa(message : String, aroundmsg : String) +# puts "#{aroundmsg} [[" +# m = IO::Memory.new(message) +# io = IO::Hexdump.new(m, output: STDERR, read: true) +# buffer = Bytes.new 4000 +# io.read (buffer) # reading = should print +# puts "]] #{aroundmsg}" +#end + def fn_input(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T*) : LibC::Char - puts "switch input: #{fd}" + # puts "switch input: #{fd}" io = IO::FileDescriptor.new fd, close_on_finalize: false slice = Bytes.new buffer, buflen.value len = io.read slice buflen.value = len.to_u64 return 2_u8 if (len == 0) + + hexa = Hexa.new 50_000 + bytes = Bytes.new buffer, buflen.value + puts hexa.dump "message received from #{fd}", bytes + 0_u8 rescue e puts "fn_input exception! #{e}" @@ -69,7 +87,11 @@ rescue e end def fn_output(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T) : LibC::Char - puts "switch output: #{fd}" + #puts "switch output: #{fd}" + hexa = Hexa.new 50_000 + bytes = Bytes.new buffer, buflen + puts hexa.dump "message to send to #{fd}", bytes + slice = Bytes.new buffer, buflen io = IO::FileDescriptor.new fd, close_on_finalize: false io.write slice