Hexa dump on both input and output to the client.

This commit is contained in:
Philippe PITTOLI 2024-06-21 21:12:25 +02:00
parent f7c67698c6
commit 405647a24c
2 changed files with 30 additions and 5 deletions

View File

@ -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'

View File

@ -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