const std = @import("std");
const net = std.net;
const os = std.os;
const log = std.log.scoped(.server);
pub fn main() !void {
var listener = net.StreamServer.init(.{});
defer listener.deinit();
const fname = "/tmp/tomateto";
os.unlink(fname) catch {};
var uds = try net.Address.initUnix(fname);
try listener.listen(uds);
log.info("Listening at: {}", .{listener.listen_address});
var server: Server = .{ .listener = listener };
var server_frame = async server.serve();
defer await server_frame catch |err| log.warn("Server error: {}", .{err});
}
const Server = struct {
listener: net.StreamServer,
pub fn serve(self: *Server) !void {
while (true) {
const conn = try self.listener.accept();
_ = try self.handleClient(conn);
}
}
pub fn handleClient(self: *Server, conn: net.StreamServer.Connection) !void {
log.info("Client connected", .{});
defer log.info("Client disconnected", .{});
_ = self;
var buffer: [1024]u8 = undefined;
while (true) {
const num_bytes = try conn.stream.read(&buffer);
if (num_bytes == 0) return;
log.debug("Received {} bytes", .{num_bytes});
}
}
};