diff options
Diffstat (limited to 'src/bin/main.zig')
| -rw-r--r-- | src/bin/main.zig | 69 |
1 files changed, 56 insertions, 13 deletions
diff --git a/src/bin/main.zig b/src/bin/main.zig index 76504ef..317885e 100644 --- a/src/bin/main.zig +++ b/src/bin/main.zig @@ -2,20 +2,30 @@ const std = @import("std"); const Io = std.Io; const App = @import("app.zig"); const Store = @import("store.zig"); +const Watchdog = @import("timeout.zig").Watchdog; +const Limits = @import("timeout.zig").Limits; const util = @import("util.zig"); const nv = @import("nvlope"); const MAILDIR: []const u8 = "/var/mail/"; const PORT = 2525; +const limits: Limits = .{}; + pub fn main(init: std.process.Init) !void { const io = init.io; - const alloc = std.heap.smp_allocator; + // const alloc = std.heap.smp_allocator; + var dbg: std.heap.DebugAllocator(.{}) = .init; + defer _ = dbg.deinit(); + const alloc = dbg.allocator(); var argiter = init.minimal.args.iterate(); - _ = argiter.skip(); + const program = argiter.next().?; if (argiter.next()) |arg| { if (std.ascii.eqlIgnoreCase(arg[0..arg.len], "--register")) { - const raw = argiter.next().?; + const raw = argiter.next() orelse { + std.debug.print("Usage: {s} --register <uname>\n", .{program}); + return; + }; var store = try Store.init(io, MAILDIR); defer store.maildir.close(io); try store.register(io, raw); @@ -36,13 +46,18 @@ pub fn main(init: std.process.Init) !void { defer group.cancel(io); // First time startup check - (try Store.init(io, MAILDIR)).maildir.close(io); + var store = try Store.init(io, MAILDIR); + store.maildir.close(io); // Async delivery worker here while (true) { const stream = try server.accept(io); - group.async(io, serve, .{ alloc, io, stream }); + group.concurrent(io, serve, .{ alloc, io, stream }) catch { + // ConcurrencyUnavailable: at capacity, refuse politely + var s = stream; + s.close(io); + }; } } @@ -55,6 +70,10 @@ fn serve(alloc: std.mem.Allocator, io: Io, stream: Io.net.Stream) void { } fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void { + var wd: Watchdog = .init(io, limits.session_s); + var dog = try io.concurrent(Watchdog.run, .{ &wd, io, stream.* }); + defer dog.cancel(io); // blocks until the task exits, so it can't touch a closed fd + var rbuf: [1024]u8 = undefined; var reader = stream.reader(io, &rbuf); const r = &reader.interface; @@ -82,38 +101,62 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void { // Greeting: the server speaks first try nv.Reply.single(.ready, "nvlope ready").send(w); try w.flush(); + app.state = .start; + app.resetTransaction(); var in_data = false; + var too_large = false; while (true) { - const line = r.takeDelimiterInclusive('\n') catch |err| switch (err) { - error.EndOfStream => return, // client hung up - error.StreamTooLong => { - try nv.Reply.single(.bad_cmd, "Line too long").send(w); - try w.flush(); + wd.arm(io, if (in_data) limits.data_s else if (app.state == .start) limits.greeting_s else limits.command_s); + const line = r.takeDelimiterInclusive('\n') catch |err| { + if (wd.fired.load(.acquire)) { + nv.Reply.single(.not_avail, "Connection Timed Out").send(w) catch {}; + w.flush() catch {}; return; - }, - else => |e| return e, + } + switch (err) { + // error.EndOfStream => return, // client hung up + error.StreamTooLong => { + try nv.Reply.single(.bad_cmd, "Line too long").send(w); + try w.flush(); + return; + }, + else => { + nv.Reply.single(.not_avail, "Client Disconnected").send(w) catch {}; + w.flush() catch {}; + return; + }, + // else => |e| return e, + } }; + wd.disarm(); _ = arena.reset(.retain_capacity); std.debug.print("C: {s}", .{line}); if (in_data) { if (std.mem.eql(u8, line, ".\r\n")) { in_data = false; + if (too_large) { + try nv.Reply.single(.exceeded, "Message too large").send(w); + app.resetTransaction(); + continue; + } app.queue() catch { try nv.Reply.single(.storage_error, "Internal storage error").send(w); try w.flush(); + app.resetTransaction(); continue; }; try nv.Reply.single(.ok, "Queued").send(w); + app.resetTransaction(); try w.flush(); // std.debug.print("App {{\n\t body: {s}\n\tsender: {s}\n\trecipients: {any}\n}}\n", .{ app.body.items, app.sender.?, app.recipients.items }); } else { const status = try app.appendBodyLine(line); switch (status) { .ok => continue, - .too_large => try nv.Reply.single(.exceeded, "Message too large").send(w), + .too_large => too_large = true, } try w.flush(); } |
