mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
server: simplify Send I/O
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
@@ -290,47 +290,40 @@ pub const Ctx = struct {
|
|||||||
// I/O Send
|
// I/O Send
|
||||||
// --------
|
// --------
|
||||||
|
|
||||||
|
// NOTE: to allow concurrent send we create each time a dedicated context
|
||||||
|
// (with its own completion), allocated on the heap.
|
||||||
|
// After the send (on the sendCbk) the dedicated context will be destroy
|
||||||
|
// and the msg slice will be free.
|
||||||
const Send = struct {
|
const Send = struct {
|
||||||
ctx: *Ctx,
|
ctx: *Ctx,
|
||||||
buf: []const u8,
|
msg: []const u8,
|
||||||
|
completion: Completion = undefined,
|
||||||
|
|
||||||
fn init(ctx: *Ctx, msg: []const u8) !struct {
|
fn init(ctx: *Ctx, msg: []const u8) !*Send {
|
||||||
ctx: *Send,
|
|
||||||
completion: *Completion,
|
|
||||||
} {
|
|
||||||
// NOTE: it seems we can't use the same completion for concurrent
|
|
||||||
// recv and timeout operations, that's why we create a new completion here
|
|
||||||
const completion = try ctx.alloc().create(Completion);
|
|
||||||
// NOTE: to handle concurrent calls we create each time a new context
|
|
||||||
// If no concurrent calls where required we could just use the main Ctx
|
|
||||||
const sd = try ctx.alloc().create(Send);
|
const sd = try ctx.alloc().create(Send);
|
||||||
sd.* = .{
|
sd.* = .{ .ctx = ctx, .msg = msg };
|
||||||
.ctx = ctx,
|
return sd;
|
||||||
.buf = msg,
|
|
||||||
};
|
|
||||||
return .{ .ctx = sd, .completion = completion };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(self: *Send, completion: *Completion) void {
|
fn deinit(self: *Send) void {
|
||||||
self.ctx.alloc().destroy(completion);
|
self.ctx.alloc().free(self.msg);
|
||||||
self.ctx.alloc().free(self.buf);
|
|
||||||
self.ctx.alloc().destroy(self);
|
self.ctx.alloc().destroy(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn asyncCbk(self: *Send, completion: *Completion, result: SendError!usize) void {
|
fn asyncCbk(self: *Send, _: *Completion, result: SendError!usize) void {
|
||||||
const size = result catch |err| {
|
const size = result catch |err| {
|
||||||
self.ctx.err = err;
|
self.ctx.err = err;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
std.log.debug("send async {d} bytes", .{size});
|
std.log.debug("send async {d} bytes", .{size});
|
||||||
self.deinit(completion);
|
self.deinit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn sendAsync(ctx: *Ctx, msg: []const u8) !void {
|
pub fn sendAsync(ctx: *Ctx, msg: []const u8) !void {
|
||||||
const sd = try Send.init(ctx, msg);
|
const sd = try Send.init(ctx, msg);
|
||||||
ctx.loop.io.send(*Send, sd.ctx, Send.asyncCbk, sd.completion, ctx.conn_socket, msg);
|
ctx.loop.io.send(*Send, sd, Send.asyncCbk, &sd.completion, ctx.conn_socket, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sendSync(ctx: *Ctx, msg: []const u8) !void {
|
pub fn sendSync(ctx: *Ctx, msg: []const u8) !void {
|
||||||
|
|||||||
Reference in New Issue
Block a user