From b76453a12317031787b6d03fb56c7cb624ac11a5 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Fri, 28 Jun 2024 17:53:27 +0200 Subject: [PATCH] Proxy: removing keepalive messages, and some code split. --- src/ipcproxy.cr | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/ipcproxy.cr b/src/ipcproxy.cr index 67a917f..9c51e1e 100644 --- a/src/ipcproxy.cr +++ b/src/ipcproxy.cr @@ -12,6 +12,7 @@ module IPCProxy @@proxy_name = ARGV[1] if ARGV.size >= 2 puts "proxy named #{@@proxy_name} for service #{@@proxied_service}" + puts "removing keepalive messages" ipc_proxy = IPC.new() ipc_proxy.service_init @@proxy_name @@ -66,36 +67,59 @@ module IPCProxy end end -def fn_input(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T*) : LibC::Char +def read_input(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T*) : LibC::Char 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}" + puts "read_input exception! #{e}" 1_u8 end -def fn_output(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T) : LibC::Char +def keepalive?(buffer : LibC::Char*) : Bool + return (buffer[4] == 250) +end + +def print_hexa(buffer : LibC::Char*, buflen : LibC::UInt64T, title : String) hexa = Hexa.new 50_000 bytes = Bytes.new buffer, buflen - puts hexa.dump "message to send to #{fd}", bytes + puts hexa.dump title, bytes +end +def fn_input(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T*) : LibC::Char + # First, get the content. + ret = read_input fd, buffer, buflen + return ret if ret != 0 + + # Second, is this a keepalive message? + return ret if keepalive? buffer + + # Finally, print an hexadecimal presentation of the content. + print_hexa buffer, buflen.value, "message received from #{fd}" + + ret +end + +def write_output(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T) : LibC::Char slice = Bytes.new buffer, buflen io = IO::FileDescriptor.new fd, close_on_finalize: false io.write slice io.flush 0_u8 rescue e - puts "fn_output exception! #{e}" + puts "write_output exception! #{e}" 1_u8 end +def fn_output(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T) : LibC::Char + unless keepalive? buffer + print_hexa buffer, buflen, "message to send to #{fd}" + end + write_output fd, buffer, buflen +end + IPCProxy.start