Add two examples: input2ipc and fromipc (serialization of IPC messages).

tmp
Philippe Pittoli 2023-05-14 14:39:30 +02:00
parent b567a25969
commit 1435515898
4 changed files with 29 additions and 3 deletions

View File

@ -9,8 +9,8 @@ CRYSTAL_BUILD_OPTIONS += $(CRYSTAL_STATIC_BUILD)
LIBIPC_RUNDIR ?= /tmp/libipc-run LIBIPC_RUNDIR ?= /tmp/libipc-run
all: build all: run
build: build-pongd build-pong build: build-pongd build-pong build-input2ipc build-fromipc
run: run-pongd run: run-pongd
include ../mk/makefile.utils include ../mk/makefile.utils
@ -27,3 +27,11 @@ build-pong:
run-pong: run-pong:
$(LD_LIBRARY_PATH) ./bin/pong $(LD_LIBRARY_PATH) ./bin/pong
-include makefile.user
build-input2ipc:
$(CRYSTAL_LIBRARY_PATH) shards build input2ipc $(CRYSTAL_BUILD_OPTIONS)
build-fromipc:
$(CRYSTAL_LIBRARY_PATH) shards build fromipc $(CRYSTAL_BUILD_OPTIONS)

View File

@ -13,11 +13,20 @@ targets:
pongd: pongd:
main: src/pongd.cr main: src/pongd.cr
# Those two examples show what it is required to serialize and deserialize
# an IPC message, which only is to add (and remove) the message length in
# the first 4 bytes of the message.
# They can be used to handle messages from a simple shell script.
fromipc:
main: src/fromipc.cr
input2ipc:
main: src/input2ipc.cr
dependencies: dependencies:
ipc: ipc:
git: https://git.baguette.netlib.re/Baguette/ipc.cr git: https://git.baguette.netlib.re/Baguette/ipc.cr
branch: master branch: master
crystal: 1.7.1 crystal: 1.8.2
license: ISC license: ISC

4
crystal/src/fromipc.cr Normal file
View File

@ -0,0 +1,4 @@
# Read an IPC network packet and remove the first 4 bytes.
buffer = Bytes.new 1_000_000
len = STDIN.read buffer
STDOUT.write buffer[4.. len -1]

5
crystal/src/input2ipc.cr Normal file
View File

@ -0,0 +1,5 @@
# Input to IPC network packets.
buffer = Bytes.new 1_000_000
len = STDIN.read buffer
STDOUT.write_bytes len, IO::ByteFormat::BigEndian
STDOUT.write buffer[0.. len -1]