-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
async.zig
48 lines (39 loc) · 1.28 KB
/
async.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const std = @import("std");
const network = @import("network");
pub const io_mode = .evented;
pub fn main() !void {
const allocator = std.heap.page_allocator;
try network.init();
defer network.deinit();
var server = try network.Socket.create(.ipv4, .tcp);
defer server.close();
try server.bind(.{
.address = .{ .ipv4 = network.Address.IPv4.any },
.port = 2501,
});
try server.listen();
std.log.info("listening at {}\n", .{try server.getLocalEndPoint()});
while (true) {
std.debug.print("Waiting for connection\n", .{});
const client = try allocator.create(Client);
client.* = Client{
.conn = try server.accept(),
.handle_frame = async client.handle(),
};
}
}
const Client = struct {
conn: network.Socket,
handle_frame: @Frame(Client.handle),
fn handle(self: *Client) !void {
try self.conn.writer().writeAll("server: welcome to the chat server\n");
while (true) {
var buf: [100]u8 = undefined;
const amt = try self.conn.receive(&buf);
if (amt == 0)
break; // We're done, end of connection
const msg = buf[0..amt];
std.debug.print("Client wrote: {s}", .{msg});
}
}
};