@@ -44,6 +44,11 @@ pub const SingleThreaded = struct {
44
44
cbk_error : bool = false ,
45
45
46
46
const Self = @This ();
47
+ pub const Completion = IO .Completion ;
48
+
49
+ pub const ConnectError = IO .ConnectError ;
50
+ pub const RecvError = IO .RecvError ;
51
+ pub const SendError = IO .SendError ;
47
52
48
53
pub fn init (alloc : std.mem.Allocator ) ! Self {
49
54
const io = try alloc .create (IO );
@@ -66,7 +71,7 @@ pub const SingleThreaded = struct {
66
71
// on the go when they are executed (ie. nested I/O events).
67
72
pub fn run (self : * Self ) ! void {
68
73
while (self .eventsNb () > 0 ) {
69
- try self .io .tick ( );
74
+ try self .io .run_for_ns ( 10 * std . time . ns_per_ms );
70
75
// at each iteration we might have new events registred by previous callbacks
71
76
}
72
77
// TODO: return instead immediatly on the first JS callback error
@@ -97,8 +102,8 @@ pub const SingleThreaded = struct {
97
102
self .alloc .destroy (ctx );
98
103
}
99
104
100
- // Callback-based APIs
101
- // -------------------
105
+ // JS callbacks APIs
106
+ // -----------------
102
107
103
108
// Timeout
104
109
@@ -207,107 +212,81 @@ pub const SingleThreaded = struct {
207
212
}
208
213
}
209
214
210
- // Yield
211
- pub fn Yield (comptime Ctx : type ) type {
212
- // TODO check ctx interface funcs:
213
- // - onYield(ctx: *Ctx, ?anyerror) void
214
- return struct {
215
- const YieldImpl = @This ();
216
- const Loop = Self ;
217
-
218
- loop : * Loop ,
219
- ctx : * Ctx ,
220
- completion : IO.Completion ,
221
-
222
- pub fn init (loop : * Loop ) YieldImpl {
223
- return .{
224
- .loop = loop ,
225
- .completion = undefined ,
226
- .ctx = undefined ,
227
- };
228
- }
229
-
230
- pub fn tick (self : * YieldImpl ) ! void {
231
- return try self .loop .io .tick ();
232
- }
215
+ // IO callbacks APIs
216
+ // -----------------
233
217
234
- pub fn yield (self : * YieldImpl , ctx : * Ctx ) void {
235
- self .ctx = ctx ;
236
- _ = self .loop .addEvent ();
237
- self .loop .io .timeout (* YieldImpl , self , YieldImpl .yieldCbk , & self .completion , 0 );
238
- }
218
+ // Connect
239
219
240
- fn yieldCbk (self : * YieldImpl , _ : * IO.Completion , result : IO .TimeoutError ! void ) void {
241
- _ = self .loop .removeEvent ();
242
- _ = result catch | err | return self .ctx .onYield (err );
243
- return self .ctx .onYield (null );
244
- }
245
- };
220
+ pub fn connect (
221
+ self : * Self ,
222
+ comptime Ctx : type ,
223
+ ctx : * Ctx ,
224
+ completion : * Completion ,
225
+ comptime cbk : fn (ctx : * Ctx , _ : * Completion , res : ConnectError ! void ) void ,
226
+ socket : std.posix.socket_t ,
227
+ address : std.net.Address ,
228
+ ) void {
229
+ const old_events_nb = self .addEvent ();
230
+ self .io .connect (* Ctx , ctx , cbk , completion , socket , address );
231
+ if (builtin .is_test ) {
232
+ report ("start connect {d}" , .{old_events_nb + 1 });
233
+ }
246
234
}
247
235
248
- // Network
249
- pub fn Network (comptime Ctx : type ) type {
250
-
251
- // TODO check ctx interface funcs:
252
- // - onConnect(ctx: *Ctx, ?anyerror) void
253
- // - onReceive(ctx: *Ctx, usize, ?anyerror) void
254
- // - onSend(ctx: *Ctx, usize, ?anyerror) void
255
-
256
- return struct {
257
- const NetworkImpl = @This ();
258
- const Loop = Self ;
259
-
260
- loop : * Loop ,
261
- ctx : * Ctx ,
262
- completion : IO.Completion ,
263
-
264
- pub fn init (loop : * Loop ) NetworkImpl {
265
- return .{
266
- .loop = loop ,
267
- .completion = undefined ,
268
- .ctx = undefined ,
269
- };
270
- }
271
-
272
- pub fn tick (self : * NetworkImpl ) ! void {
273
- return try self .loop .io .tick ();
274
- }
236
+ pub fn onConnect (self : * Self , _ : ConnectError ! void ) void {
237
+ const old_events_nb = self .removeEvent ();
238
+ if (builtin .is_test ) {
239
+ report ("connect done, remaining events: {d}" , .{old_events_nb - 1 });
240
+ }
241
+ }
275
242
276
- pub fn connect (self : * NetworkImpl , ctx : * Ctx , socket : std.posix.socket_t , address : std.net.Address ) void {
277
- self .ctx = ctx ;
278
- _ = self .loop .addEvent ();
279
- self .loop .io .connect (* NetworkImpl , self , NetworkImpl .connectCbk , & self .completion , socket , address );
280
- }
243
+ // Send
281
244
282
- fn connectCbk (self : * NetworkImpl , _ : * IO.Completion , result : IO .ConnectError ! void ) void {
283
- _ = self .loop .removeEvent ();
284
- _ = result catch | err | return self .ctx .onConnect (err );
285
- return self .ctx .onConnect (null );
286
- }
245
+ pub fn send (
246
+ self : * Self ,
247
+ comptime Ctx : type ,
248
+ ctx : * Ctx ,
249
+ completion : * Completion ,
250
+ comptime cbk : fn (ctx : * Ctx , completion : * Completion , res : SendError ! usize ) void ,
251
+ socket : std.posix.socket_t ,
252
+ buf : []const u8 ,
253
+ ) void {
254
+ const old_events_nb = self .addEvent ();
255
+ self .io .send (* Ctx , ctx , cbk , completion , socket , buf );
256
+ if (builtin .is_test ) {
257
+ report ("start send {d}" , .{old_events_nb + 1 });
258
+ }
259
+ }
287
260
288
- pub fn receive (self : * NetworkImpl , ctx : * Ctx , socket : std.posix.socket_t , buffer : []u8 ) void {
289
- self .ctx = ctx ;
290
- _ = self .loop .addEvent ();
291
- self .loop .io .recv (* NetworkImpl , self , NetworkImpl .receiveCbk , & self .completion , socket , buffer );
292
- }
261
+ pub fn onSend (self : * Self , _ : SendError ! usize ) void {
262
+ const old_events_nb = self .removeEvent ();
263
+ if (builtin .is_test ) {
264
+ report ("send done, remaining events: {d}" , .{old_events_nb - 1 });
265
+ }
266
+ }
293
267
294
- fn receiveCbk (self : * NetworkImpl , _ : * IO.Completion , result : IO .RecvError ! usize ) void {
295
- _ = self .loop .removeEvent ();
296
- const ln = result catch | err | return self .ctx .onReceive (0 , err );
297
- return self .ctx .onReceive (ln , null );
298
- }
268
+ // Recv
299
269
300
- pub fn send (self : * NetworkImpl , ctx : * Ctx , socket : std.posix.socket_t , buffer : []const u8 ) void {
301
- self .ctx = ctx ;
302
- _ = self .loop .addEvent ();
303
- self .loop .io .send (* NetworkImpl , self , NetworkImpl .sendCbk , & self .completion , socket , buffer );
304
- }
270
+ pub fn recv (
271
+ self : * Self ,
272
+ comptime Ctx : type ,
273
+ ctx : * Ctx ,
274
+ completion : * Completion ,
275
+ comptime cbk : fn (ctx : * Ctx , completion : * Completion , res : RecvError ! usize ) void ,
276
+ socket : std.posix.socket_t ,
277
+ buf : []u8 ,
278
+ ) void {
279
+ const old_events_nb = self .addEvent ();
280
+ self .io .recv (* Ctx , ctx , cbk , completion , socket , buf );
281
+ if (builtin .is_test ) {
282
+ report ("start recv {d}" , .{old_events_nb + 1 });
283
+ }
284
+ }
305
285
306
- fn sendCbk (self : * NetworkImpl , _ : * IO.Completion , result : IO .SendError ! usize ) void {
307
- _ = self .loop .removeEvent ();
308
- const ln = result catch | err | return self .ctx .onSend (0 , err );
309
- return self .ctx .onSend (ln , null );
310
- }
311
- };
286
+ pub fn onRecv (self : * Self , _ : RecvError ! usize ) void {
287
+ const old_events_nb = self .removeEvent ();
288
+ if (builtin .is_test ) {
289
+ report ("recv done, remaining events: {d}" , .{old_events_nb - 1 });
290
+ }
312
291
}
313
292
};
0 commit comments