diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2026-01-06 18:45:17 -0500 |
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2026-01-06 20:43:49 -0500 |
| commit | b87412ee66197d4c89f1fbf93b32fe63ed1c63ab (patch) | |
| tree | 00613d0d3f7178d0c5b974ce04a752443e9a816e /src/subcommand/server.zig | |
| parent | 025a5344c8c922a8f46c4ee0e73a00ce0c3c4790 (diff) | |
Restructuring
Add a bunch of tests for the client
Diffstat (limited to 'src/subcommand/server.zig')
| -rw-r--r-- | src/subcommand/server.zig | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/subcommand/server.zig b/src/subcommand/server.zig new file mode 100644 index 0000000..1aaf572 --- /dev/null +++ b/src/subcommand/server.zig @@ -0,0 +1,66 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const AtomicValue = std.atomic.Value; +const DebugAllocator = std.heap.DebugAllocator; +const Sigaction = std.posix.Sigaction; + +const Io = std.Io; +const Threaded = Io.Threaded; + +const builtin = @import("builtin"); + +const zits = @import("zits"); +const Message = zits.Server.Message; +const ServerInfo = Message.ServerInfo; + +const Server = zits.Server; + +const safe_build = builtin.mode == .Debug or builtin.mode == .ReleaseSafe; + +var keep_running = AtomicValue(bool).init(true); + +fn handleSigInt(sig: std.os.linux.SIG) callconv(.c) void { + _ = sig; + keep_running.store(false, .monotonic); +} + +pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void { + // Configure the signal action + const act = Sigaction{ + .handler = .{ .handler = handleSigInt }, + .mask = std.posix.sigemptyset(), + .flags = 0, + }; + + // Register the handler for SIGINT (Ctrl+C) + std.posix.sigaction(std.posix.SIG.INT, &act, null); + + { + var dba: DebugAllocator(.{}) = .init; + dba.backing_allocator = outer_alloc; + defer _ = dba.deinit(); + const alloc = if (safe_build) dba.allocator() else outer_alloc; + + var threaded: Threaded = .init(alloc, .{}); + defer threaded.deinit(); + const io = threaded.io(); + + var server: Server = .{ + .info = server_config, + }; + defer server.deinit(io, alloc); + + var server_task = try io.concurrent(Server.start, .{ &server, io, alloc }); + defer server_task.cancel(io) catch {}; + + // Block until Ctrl+C + while (keep_running.load(.monotonic)) { + try io.sleep(.fromMilliseconds(1), .awake); + } + + std.debug.print("\n", .{}); + std.log.info("Shutting down...", .{}); + server_task.cancel(io) catch {}; + } + std.log.info("Goodbye", .{}); +} |
