@@ -17,7 +17,7 @@ tardy is a thread-local, I/O driven runtime for Zig, providing the core implemen
1717- Coroutines (internally called Frames).
1818
1919## Installing
20- Compatible Zig Version: ` 0.14.0 `
20+ Compatible Zig Version: ` 0.15.2 `
2121
2222Latest Release: ` 0.3.0 `
2323```
@@ -60,46 +60,51 @@ A basic multi-threaded TCP echo server.
6060
6161``` zig
6262const std = @import("std");
63- const log = std.log.scoped(.@"tardy/example/echo");
6463
64+ const AcceptResult = @import("tardy").AcceptResult;
65+ const Cross = @import("tardy").Cross;
6566const Pool = @import("tardy").Pool;
67+ const RecvResult = @import("tardy").RecvResult;
6668const Runtime = @import("tardy").Runtime;
67- const Task = @import("tardy").Task;
68- const Tardy = @import("tardy").Tardy(.auto);
69- const Cross = @import("tardy").Cross;
70-
69+ const SendResult = @import("tardy").SendResult;
7170const Socket = @import("tardy").Socket;
71+ const Task = @import("tardy").Task;
7272const Timer = @import("tardy").Timer;
7373
74- const AcceptResult = @import("tardy").AcceptResult;
75- const RecvResult = @import("tardy").RecvResult;
76- const SendResult = @import("tardy").SendResult;
74+ const Tardy = @import("tardy").Tardy(.auto);
75+ const log = std.log.scoped(.@"tardy/example/echo");
7776
7877fn echo_frame(rt: *Runtime, server: *const Socket) !void {
7978 const socket = try server.accept(rt);
8079 defer socket.close_blocking();
8180
82- // you can use the standard Zig Reader/Writer if you want!
83- const reader = socket.reader(rt);
84- const writer = socket.writer(rt);
81+ var sock_reader = socket.reader(rt, &.{});
82+ const sock_r = &sock_reader.interface;
83+
84+ var sock_writer = socket.writer(rt, &.{});
85+ const sock_w = &sock_writer.interface;
86+ defer sock_w.flush() catch unreachable;
8587
8688 log.debug(
87- "{d} - accepted socket [{}]",
89+ "{d} - accepted socket [{f }]",
8890 .{ std.time.milliTimestamp(), socket.addr },
8991 );
9092
93+ // spawn off a new frame.
9194 try rt.spawn(.{ rt, server }, echo_frame, 1024 * 16);
9295
93- var buffer: [1024 ]u8 = undefined;
96+ var buffer: [501 ]u8 = undefined;
9497 while (true) {
95- const recv_length = reader.read (&buffer) catch |e| {
96- log.err("Failed to recv on socket | {}", .{e});
97- return ;
98+ const recv_length = sock_r.readSliceShort (&buffer) catch |e| {
99+ log.err("Failed to recv on socket | {t }", .{e});
100+ break ;
98101 };
99102
100- writer.writeAll(buffer[0..recv_length]) catch |e| {
101- log.err("Failed to send on socket | {}", .{e});
102- return;
103+ if (recv_length == 0) return;
104+
105+ sock_w.writeAll(buffer[0..recv_length]) catch |e| {
106+ log.err("Failed to send on socket | {t}", .{e});
107+ break;
103108 };
104109
105110 log.debug("Echoed: {s}", .{buffer[0..recv_length]});
@@ -111,21 +116,26 @@ pub fn main() !void {
111116 const allocator = gpa.allocator();
112117 defer _ = gpa.deinit();
113118
114- // tardy by default is
115- // - multithreaded
116- // - unbounded in terms of spawnable tasks
117- var tardy: Tardy = try .init(allocator, .{});
119+ var tardy: Tardy = try .init(allocator, .{
120+ .threading = .single,
121+ .pooling = .static,
122+ .size_tasks_initial = 256,
123+ .size_aio_reap_max = 256,
124+ });
118125 defer tardy.deinit();
119126
120- const server: Socket = try .init(.{ .tcp = .{ .host = "127.0.0.1", .port = 9862 } });
127+ const host = "0.0.0.0";
128+ const port = 9862;
129+
130+ const server: Socket = try .init(.{ .tcp = .{ .host = host, .port = port } });
121131 try server.bind();
122- try server.listen(256 );
132+ try server.listen(1024 );
123133
124134 try tardy.entry(
125135 &server,
126136 struct {
127137 fn start(rt: *Runtime, tcp_server: *const Socket) !void {
128- try rt.spawn(.{ rt, tcp_server }, echo_frame, 1024 * 1024 * 4 );
138+ try rt.spawn(.{ rt, tcp_server }, echo_frame, 1024 * 16 );
129139 }
130140 }.start,
131141 );
0 commit comments