const std = @import("std"); const Io = std.Io; const nvlope = @import("nvlope"); pub fn main(init: std.process.Init) !void { const io = init.io; const alloc = std.heap.smp_allocator; // Initialize server on localhost at the specified port const port = 8080; const addr = try Io.net.IpAddress.parse("127.0.0.1", port); var server = try addr.listen(io, .{ .mode = .stream, .protocol = .tcp, .reuse_address = true, }); defer server.socket.close(io); // Async server while (true) { std.debug.print("Waiting for conn...\n", .{}); _ = io.async(client, .{ alloc, io, &server }); } } fn client(alloc: std.mem.Allocator, io: Io, server: *Io.net.Server) !void { _ = alloc; var client_stream = try server.accept(io); defer client_stream.close(io); var buf: [512]u8 = undefined; var reader = client_stream.reader(io, &buf); const reader_interface = &reader.interface; // Temporary outbuff for printing var outbuff: [128]u8 = undefined; var writer: Io.File.Writer = .init(.stdout(), io, &outbuff); const writer_interface = &writer.interface; try writer_interface.print("New Connection: {}\n", .{client_stream.socket.address}); try writer_interface.flush(); // Logic loop while (true) { const line = reader_interface.takeDelimiterInclusive('\n') catch |err| switch (err) { error.EndOfStream => break, // client closed the connection error.StreamTooLong => break, else => |e| return e, }; const parsed = try nvlope.SMTP.parse(line); try writer_interface.print("{}\n", .{parsed}); try writer_interface.flush(); if (!std.mem.endsWith(u8, line, "\r\n")) { // DATA section continue; } } }