tcp: pass the app mailbox to the command handler

This commit is contained in:
Aarnav Tale
2024-04-21 19:00:32 -04:00
parent f172b3d616
commit 08fa78315b
6 changed files with 23 additions and 9 deletions

View File

@ -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(

View File

@ -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());

View File

@ -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(),
}

View File

@ -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,
};
}

View File

@ -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,

View File

@ -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]);
}