mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 03:34:48 +00:00
reorganize some things
This commit is contained in:
@@ -115,32 +115,36 @@ fn handleConnection(
|
|||||||
id: usize,
|
id: usize,
|
||||||
stream: std.Io.net.Stream,
|
stream: std.Io.net.Stream,
|
||||||
) !void {
|
) !void {
|
||||||
|
defer stream.close(io);
|
||||||
|
|
||||||
var client_allocator: std.heap.DebugAllocator(.{}) = .init;
|
var client_allocator: std.heap.DebugAllocator(.{}) = .init;
|
||||||
client_allocator.backing_allocator = server_allocator;
|
client_allocator.backing_allocator = server_allocator;
|
||||||
defer _ = client_allocator.deinit();
|
defer _ = client_allocator.deinit();
|
||||||
|
|
||||||
const allocator = client_allocator.allocator();
|
const allocator = client_allocator.allocator();
|
||||||
defer stream.close(io);
|
|
||||||
|
// Set up client writer
|
||||||
const w_buffer: []u8 = try allocator.alloc(u8, 1024);
|
const w_buffer: []u8 = try allocator.alloc(u8, 1024);
|
||||||
defer allocator.free(w_buffer);
|
defer allocator.free(w_buffer);
|
||||||
var writer = stream.writer(io, w_buffer);
|
var writer = stream.writer(io, w_buffer);
|
||||||
const out = &writer.interface;
|
const out = &writer.interface;
|
||||||
|
|
||||||
|
// Set up client reader
|
||||||
const r_buffer: []u8 = try allocator.alloc(u8, 1024);
|
const r_buffer: []u8 = try allocator.alloc(u8, 1024);
|
||||||
defer allocator.free(r_buffer);
|
defer allocator.free(r_buffer);
|
||||||
var reader = stream.reader(io, r_buffer);
|
var reader = stream.reader(io, r_buffer);
|
||||||
const in = &reader.interface;
|
const in = &reader.interface;
|
||||||
|
|
||||||
|
// Create client
|
||||||
var client: Client = .init(null, in, out);
|
var client: Client = .init(null, in, out);
|
||||||
try client.send(io, .{ .info = server.info });
|
try server.addClient(server_allocator, id, &client);
|
||||||
|
defer server.removeClient(io, server_allocator, id);
|
||||||
|
|
||||||
|
// Do initial handshake with client
|
||||||
|
try client.send(io, .{ .info = server.info });
|
||||||
var connect_arena: std.heap.ArenaAllocator = .init(allocator);
|
var connect_arena: std.heap.ArenaAllocator = .init(allocator);
|
||||||
defer connect_arena.deinit();
|
defer connect_arena.deinit();
|
||||||
client.connect = (Message.next(connect_arena.allocator(), in) catch return).connect;
|
client.connect = (Message.next(connect_arena.allocator(), in) catch return).connect;
|
||||||
|
|
||||||
try server.addClient(server_allocator, id, &client);
|
|
||||||
defer server.removeClient(io, server_allocator, id);
|
|
||||||
|
|
||||||
// Messages are owned by the server after they are received from the client
|
// Messages are owned by the server after they are received from the client
|
||||||
while (client.next(server_allocator)) |msg| {
|
while (client.next(server_allocator)) |msg| {
|
||||||
switch (msg) {
|
switch (msg) {
|
||||||
@@ -161,12 +165,14 @@ fn handleConnection(
|
|||||||
std.debug.panic("Unimplemented message: {any}\n", .{e});
|
std.debug.panic("Unimplemented message: {any}\n", .{e});
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else |err| {
|
} else |err| switch (err) {
|
||||||
// This is probably going to be normal on disconnect
|
error.EndOfStream => {
|
||||||
std.debug.print("Ran into error in client process loop: {}\n", .{err});
|
std.debug.print("Client {d} disconnected", .{});
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
return err;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// client_state.task.await(io);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Result is owned by the caller
|
// // Result is owned by the caller
|
||||||
|
|||||||
Reference in New Issue
Block a user