Skip to content

Commit d0e006b

Browse files
loop: Add IO methods
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
1 parent 2c66f41 commit d0e006b

File tree

1 file changed

+74
-53
lines changed

1 file changed

+74
-53
lines changed

src/loop.zig

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ pub const SingleThreaded = struct {
4444
cbk_error: bool = false,
4545

4646
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;
4752

4853
pub fn init(alloc: std.mem.Allocator) !Self {
4954
const io = try alloc.create(IO);
@@ -97,8 +102,8 @@ pub const SingleThreaded = struct {
97102
self.alloc.destroy(ctx);
98103
}
99104

100-
// Callback-based APIs
101-
// -------------------
105+
// JS callbacks APIs
106+
// -----------------
102107

103108
// Timeout
104109

@@ -207,65 +212,81 @@ pub const SingleThreaded = struct {
207212
}
208213
}
209214

210-
// Network
211-
pub fn Network(comptime Ctx: type) type {
212-
213-
// TODO check ctx interface funcs:
214-
// - onConnect(ctx: *Ctx, ?anyerror) void
215-
// - onReceive(ctx: *Ctx, usize, ?anyerror) void
216-
// - onSend(ctx: *Ctx, usize, ?anyerror) void
215+
// IO callbacks APIs
216+
// -----------------
217217

218-
return struct {
219-
const NetworkImpl = @This();
220-
const Loop = Self;
218+
// Connect
221219

222-
loop: *Loop,
223-
ctx: *Ctx,
224-
completion: IO.Completion,
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+
}
234+
}
225235

226-
pub fn init(loop: *Loop) NetworkImpl {
227-
return .{
228-
.loop = loop,
229-
.completion = undefined,
230-
.ctx = undefined,
231-
};
232-
}
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+
}
233242

234-
pub fn connect(self: *NetworkImpl, ctx: *Ctx, socket: std.posix.socket_t, address: std.net.Address) void {
235-
self.ctx = ctx;
236-
_ = self.loop.addEvent();
237-
self.loop.io.connect(*NetworkImpl, self, NetworkImpl.connectCbk, &self.completion, socket, address);
238-
}
243+
// Send
239244

240-
fn connectCbk(self: *NetworkImpl, _: *IO.Completion, result: IO.ConnectError!void) void {
241-
_ = self.loop.removeEvent();
242-
_ = result catch |err| return self.ctx.onConnect(err);
243-
return self.ctx.onConnect(null);
244-
}
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+
}
245260

246-
pub fn receive(self: *NetworkImpl, ctx: *Ctx, socket: std.posix.socket_t, buffer: []u8) void {
247-
self.ctx = ctx;
248-
_ = self.loop.addEvent();
249-
self.loop.io.recv(*NetworkImpl, self, NetworkImpl.receiveCbk, &self.completion, socket, buffer);
250-
}
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+
}
251267

252-
fn receiveCbk(self: *NetworkImpl, _: *IO.Completion, result: IO.RecvError!usize) void {
253-
_ = self.loop.removeEvent();
254-
const ln = result catch |err| return self.ctx.onReceive(0, err);
255-
return self.ctx.onReceive(ln, null);
256-
}
268+
// Recv
257269

258-
pub fn send(self: *NetworkImpl, ctx: *Ctx, socket: std.posix.socket_t, buffer: []const u8) void {
259-
self.ctx = ctx;
260-
_ = self.loop.addEvent();
261-
self.loop.io.send(*NetworkImpl, self, NetworkImpl.sendCbk, &self.completion, socket, buffer);
262-
}
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+
}
263285

264-
fn sendCbk(self: *NetworkImpl, _: *IO.Completion, result: IO.SendError!usize) void {
265-
_ = self.loop.removeEvent();
266-
const ln = result catch |err| return self.ctx.onSend(0, err);
267-
return self.ctx.onSend(ln, null);
268-
}
269-
};
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+
}
270291
}
271292
};

0 commit comments

Comments
 (0)