From 08fa78315bd45d9b577dc0f63b1dc47040dc6420 Mon Sep 17 00:00:00 2001 From: Aarnav Tale Date: Sun, 21 Apr 2024 19:00:32 -0400 Subject: [PATCH] tcp: pass the app mailbox to the command handler --- src/main_ghostty.zig | 2 +- src/tcp.zig | 1 + src/tcp/Command.zig | 4 +++- src/tcp/Server.zig | 6 ++++++ src/tcp/Thread.zig | 17 +++++++++++------ src/tcp/handlers/reader.zig | 2 +- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main_ghostty.zig b/src/main_ghostty.zig index 88d4b00ac..c22b97c25 100644 --- a/src/main_ghostty.zig +++ b/src/main_ghostty.zig @@ -114,7 +114,7 @@ pub fn main() !MainReturn { if (@hasDecl(apprt.App, "startQuitTimer")) app_runtime.startQuitTimer(); // Not sure where this should go tbh - var tcp_thread = try tcp.Thread.init(alloc); + var tcp_thread = try tcp.Thread.init(alloc, &app.mailbox); defer tcp_thread.deinit(); var tcp_thr = try std.Thread.spawn( diff --git a/src/tcp.zig b/src/tcp.zig index cc77459b0..19beac4f7 100644 --- a/src/tcp.zig +++ b/src/tcp.zig @@ -1,5 +1,6 @@ //! TCP implementation. The TCP implementation is responsible for //! responding to TCP requests and dispatching them to the app's Mailbox. +pub const Thread = @import("tcp/Thread.zig"); test { @import("std").testing.refAllDecls(@This()); diff --git a/src/tcp/Command.zig b/src/tcp/Command.zig index 97eb592ee..33bdecb7f 100644 --- a/src/tcp/Command.zig +++ b/src/tcp/Command.zig @@ -1,5 +1,6 @@ const std = @import("std"); const log = std.log.scoped(.tcp_thread); +const Server = @import("Server.zig"); const ping = @import("commands/ping.zig").ping; @@ -30,7 +31,8 @@ pub const Command = enum { return error.InvalidCommand; } - pub fn handle(self: Command) ![]const u8 { + pub fn handle(self: Command, server: *Server) ![]const u8 { + _ = server; // TODO: Only pass into commands that actually need it switch (self) { .ping => return ping(), } diff --git a/src/tcp/Server.zig b/src/tcp/Server.zig index 1f7235585..7be88506d 100644 --- a/src/tcp/Server.zig +++ b/src/tcp/Server.zig @@ -2,6 +2,7 @@ const std = @import("std"); const xev = @import("xev"); const Config = @import("../config/Config.zig"); const connections = @import("./handlers/connections.zig"); +const App = @import("../App.zig"); const Allocator = std.mem.Allocator; const CompletionPool = std.heap.MemoryPool(xev.Completion); @@ -39,11 +40,15 @@ addr: std.net.Address, /// Maximum clients allowed max_clients: u8, +/// A reference to the app's main mailbox to dispathc messages +mailbox: *App.Mailbox.Queue, + /// Initializes the server with the given allocator and address pub fn init( alloc: Allocator, addr: std.net.Address, max_clients: u8, + mailbox: *App.Mailbox.Queue, ) !Server { return Server{ .alloc = alloc, @@ -55,6 +60,7 @@ pub fn init( .clients_count = 0, .addr = addr, .max_clients = max_clients, + .mailbox = mailbox, }; } diff --git a/src/tcp/Thread.zig b/src/tcp/Thread.zig index e348718e0..bafa80ad5 100644 --- a/src/tcp/Thread.zig +++ b/src/tcp/Thread.zig @@ -3,9 +3,9 @@ pub const Thread = @This(); const std = @import("std"); const xev = @import("xev"); -const tcp = @import("../tcp.zig"); const App = @import("../App.zig"); const Config = @import("../config/Config.zig"); +const Server = @import("Server.zig"); const Allocator = std.mem.Allocator; const log = std.log.scoped(.tcp_thread); @@ -14,17 +14,17 @@ const log = std.log.scoped(.tcp_thread); alloc: std.mem.Allocator, /// The TCP server for handling incoming connections. -server: ?tcp.Server, +server: ?Server, /// Initialize the thread. This does not START the thread. This only sets /// up all the internal state necessary prior to starting the thread. It /// is up to the caller to start the thread with the threadMain entrypoint. -pub fn init(alloc: Allocator) !Thread { +pub fn init(alloc: Allocator, mailbox: *App.Mailbox.Queue) !Thread { const config = try Config.load(alloc); const max_clients = config.@"remote-max-connections"; const addr = config.@"remote-tcp-socket"; - const parsedAddr = tcp.Server.parseAddress(addr) catch |err| { + const parsedAddr = Server.parseAddress(addr) catch |err| { log.err("failed to parse address addr={any} err={any}", .{ addr, err }); return Thread{ .alloc = alloc, @@ -33,9 +33,14 @@ pub fn init(alloc: Allocator) !Thread { }; log.debug("parsed address addr={any}", .{parsedAddr}); - var server = try tcp.Server.init(alloc, parsedAddr, max_clients); - errdefer server.deinit(); + var server = try Server.init( + alloc, + parsedAddr, + max_clients, + mailbox, + ); + errdefer server.deinit(); return Thread{ .alloc = alloc, .server = server, diff --git a/src/tcp/handlers/reader.zig b/src/tcp/handlers/reader.zig index 8596c2d92..cf29e9c62 100644 --- a/src/tcp/handlers/reader.zig +++ b/src/tcp/handlers/reader.zig @@ -59,7 +59,7 @@ fn rHandler( continue; }; - const res = try Command.handle(cmd); + const res = try Command.handle(cmd, self); @memcpy(b_w.ptr, res.ptr[0..res.len]); }