Bindings Crystal: working! Pong client works as expected.

master
Philippe Pittoli 2023-01-21 05:10:55 +01:00
parent e6ad0ce65c
commit 9696190e99
2 changed files with 62 additions and 24 deletions

View File

@ -5,5 +5,11 @@ VG_OPTS = --leak-check=full -v
VG_OPTS += --suppressions=valgrind.suppressions
VG_OPTS += --gen-suppressions=all
build:
CRYSTAL_LIBRARY_PATH=$(LDPATH) shards build
valgrind:
LD_LIBRARY_PATH=$(LDPATH) valgrind $(VG_OPTS) $(SRC)
run:
LD_LIBRARY_PATH=$(LDPATH) $(SRC)

View File

@ -12,34 +12,27 @@ lib LibIPC
# SwitchTx # Switch subsystem: message send.
# end
fun init = ipc_context_init (Pointer(Pointer(Void))) : LibC::Int
fun deinit = ipc_context_deinit (Pointer(Void)) : Void
fun init = ipc_context_init (Void**) : LibC::Int
fun deinit = ipc_context_deinit (Void*) : Void
# Connection functions.
# Context is allocated, ipcd is requested and the connection/initialisation is performed.
fun service_init = ipc_service_init (Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int
fun connect_service = ipc_connect_service(Void*, LibC::Int*, LibC::Char*, LibC::UInt16T) : LibC::Int
fun wait = ipc_wait_event(Void*, Char*, LibC::UInt64T*, LibC::Int*, Char*, LibC::UInt64T*) : LibC::Int
# Sending a message NOW.
# WARNING: unbuffered send do not wait the fd to become available.
fun write = ipc_write(Void*, LibC::Int, UInt8*, LibC::UInt64T) : LibC::Int
# Sending a message (will wait the fd to become available for IO operations).
fun schedule = ipc_schedule(Void*, LibC::Int, UInt8*, LibC::UInt64T) : LibC::Int
fun read = ipc_read_fd (Void*, LibC::Int, UInt8*, LibC::UInt64T*);
# # Connection functions.
# # Context is allocated, ipcd is requested and the connection/initialisation is performed.
# fun service_init = ipc_service_init (Pointer(Void), LibC::Int*, LibC::Char*, LibC::UInt16) : LibC::Int
# fun connect_service = ipc_connect_service(Pointer(Void), LibC::Int*, LibC::Char*, LibC::UInt16) : LibC::Int
#
# # Closing connections.
# fun ipc_close(Pointer(Void), index : LibC::UInt64T) : LibC::Int
# fun ipc_close_all(Pointer(Void)) : LibC::Int
#
# # Loop function.
# fun ipc_wait_event(Ctx*, Event*, LibC::Int*) : LibC::Int
#
# # Adding and removing file discriptors to read.
# fun ipc_add(Ctx*, Connection*, Pollfd*) : LibC::Int
# fun ipc_del(Ctx*, LibC::UInt) : LibC::Int
# fun ipc_add_fd(Ctx*, LibC::Int) : LibC::Int
# fun ipc_del_fd(Ctx*, LibC::Int) : LibC::Int
#
# # Sending a message (will wait the fd to become available for IO operations).
# fun ipc_write(Ctx*, Message*) : LibC::Int
# fun ipc_schedule(Pointer(Void), ) : LibC::Int
#
# # Sending a message NOW.
# # WARNING: unbuffered send do not wait the fd to become available.
# fun ipc_write_fd(Int32, Message*) : LibC::Int
end
# TODO: Write documentation for `Some::Crystal::App`
@ -49,5 +42,44 @@ module Some::Crystal::App
# TODO: Put your code here
ctx : Pointer(Void) = Pointer(Void).null
LibIPC.init (pointerof(ctx))
fd : Int32 = 0
LibIPC.connect_service(ctx, pointerof(fd), "pong", 4)
pp! fd
LibIPC.write(ctx, fd, "Hello", 5)
buflen : LibC::UInt64T = 10
#buffer = uninitialized UInt8[10]
buffer = Array(UInt8).new(10, 88)
#buffer[0] = 'a'
#buffer[1] = 'b'
#buffer[2] = 'c'
#buffer[3] = 'd'
#buffer[4] = 'e'
#buffer[5] = 'f'
#pp! buffer[0]
#pp! buffer[1]
#pp! buffer[2]
#pp! buffer[3]
#pp! buffer[4]
#pp! buffer[5]
LibIPC.read(ctx, fd, buffer.to_unsafe, pointerof(buflen))
pp! buflen
pp! buffer.size
buffer[buflen] = 0
str = String.new(buffer.to_unsafe)
pp! str
#received = String.build do |str|
# count = buflen
# while count > 0
# str << buffer[buflen - count]
# count -= 1
# end
#end
#pp! received
#received = Slice.new(buffer.to_unsafe, buflen)
##puts "Buffer is: [#{received}]"
LibIPC.deinit (ctx)
end