From 266f1daaadd0f44e50562224ad44de3c895d7dbc Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sat, 31 Dec 2022 04:59:06 +0100 Subject: [PATCH] Pong client. --- zig-impl/src/pong.zig | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 zig-impl/src/pong.zig diff --git a/zig-impl/src/pong.zig b/zig-impl/src/pong.zig new file mode 100644 index 0000000..ec54367 --- /dev/null +++ b/zig-impl/src/pong.zig @@ -0,0 +1,61 @@ +const std = @import("std"); +const hexdump = @import("./hexdump.zig"); +const net = std.net; +const fmt = std.fmt; +const os = std.os; + +const ipc = @import("./main.zig"); + +const builtin = @import("builtin"); +const native_os = builtin.target.os.tag; +const print = std.debug.print; +const testing = std.testing; +const print_eq = @import("./util.zig").print_eq; + +pub fn main() !u8 { + const config = .{.safety = true}; + var gpa = std.heap.GeneralPurposeAllocator(config){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + + var ctx = try ipc.Context.init(allocator); + defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful? + + // TODO: currently using a bullshit path. + const path = "/tmp/.TEST_USOCK"; + + // CLIENT SIDE: connecting to a service. + var sfd = try ctx.connect (path); + + var first_msg = try ipc.Message.init(sfd, ipc.Message.Type.DATA, allocator, "hello this is pong!!"); + try ctx.write(first_msg); + first_msg.deinit(); + + var some_event: ipc.Event = undefined; + ctx.timer = 10000; // 10 seconds + while(true) { + some_event = try ctx.wait_event(); + switch (some_event.t) { + .TIMER => { + print("Timer!\n", .{}); + }, + .MESSAGE => { + if (some_event.m) |m| { + print("Message received: {}\n", .{m}); + m.deinit(); + } + else { + print("A message should have been received, weird\n", .{}); + } + break; + }, + else => { + print("Unexpected message: {s}\n", .{some_event}); + break; + }, + } + } + + print("Goodbye\n", .{}); + return 0; +}