async: remove context from loop impl init

This commit is contained in:
Pierre Tachoire
2024-01-31 15:14:55 +01:00
parent 89409a4847
commit 0693011ad3
3 changed files with 16 additions and 30 deletions

View File

@@ -42,23 +42,20 @@ pub const Conn = struct {
loop: *Loop, loop: *Loop,
pub fn connect(self: *Conn, socket: std.os.socket_t, address: std.net.Address) !void { pub fn connect(self: *Conn, socket: std.os.socket_t, address: std.net.Address) !void {
var cmd = Command{ .impl = undefined }; var cmd = Command{ .impl = NetworkImpl.init(self.loop) };
cmd.impl = NetworkImpl.init(self.loop, &cmd); cmd.impl.connect(&cmd, socket, address);
cmd.impl.connect(socket, address);
_ = try cmd.wait(); _ = try cmd.wait();
} }
pub fn send(self: *Conn, socket: std.os.socket_t, buffer: []const u8) !usize { pub fn send(self: *Conn, socket: std.os.socket_t, buffer: []const u8) !usize {
var cmd = Command{ .impl = undefined }; var cmd = Command{ .impl = NetworkImpl.init(self.loop) };
cmd.impl = NetworkImpl.init(self.loop, &cmd); cmd.impl.send(&cmd, socket, buffer);
cmd.impl.send(socket, buffer);
return try cmd.wait(); return try cmd.wait();
} }
pub fn receive(self: *Conn, socket: std.os.socket_t, buffer: []u8) !usize { pub fn receive(self: *Conn, socket: std.os.socket_t, buffer: []u8) !usize {
var cmd = Command{ .impl = undefined }; var cmd = Command{ .impl = NetworkImpl.init(self.loop) };
cmd.impl = NetworkImpl.init(self.loop, &cmd); cmd.impl.receive(&cmd, socket, buffer);
cmd.impl.receive(socket, buffer);
return try cmd.wait(); return try cmd.wait();
} }
}; };

View File

@@ -77,11 +77,10 @@ const AsyncClient = struct {
pub fn deinit(self: *AsyncRequest) void { pub fn deinit(self: *AsyncRequest) void {
self.headers.deinit(); self.headers.deinit();
self.cli.allocator.destroy(self);
} }
pub fn fetch(self: *AsyncRequest) void { pub fn fetch(self: *AsyncRequest) void {
return self.impl.yield(); return self.impl.yield(self);
} }
fn onerr(self: *AsyncRequest, err: anyerror) void { fn onerr(self: *AsyncRequest, err: anyerror) void {
@@ -118,16 +117,13 @@ const AsyncClient = struct {
self.cli.deinit(); self.cli.deinit();
} }
pub fn create(self: *AsyncClient, uri: std.Uri) !*AsyncRequest { pub fn create(self: *AsyncClient, uri: std.Uri) !AsyncRequest {
var req = try self.cli.allocator.create(AsyncRequest); return .{
req.* = AsyncRequest{ .impl = YieldImpl.init(self.cli.loop),
.impl = undefined,
.cli = &self.cli, .cli = &self.cli,
.uri = uri, .uri = uri,
.headers = .{ .allocator = self.cli.allocator, .owned = false }, .headers = .{ .allocator = self.cli.allocator, .owned = false },
}; };
req.impl = YieldImpl.init(self.cli.loop, req);
return req;
} }
}; };
@@ -140,7 +136,7 @@ test "non blocking client" {
var client = AsyncClient.init(alloc, &loop); var client = AsyncClient.init(alloc, &loop);
defer client.deinit(); defer client.deinit();
var reqs: [10]*AsyncClient.AsyncRequest = undefined; var reqs: [10]AsyncClient.AsyncRequest = undefined;
for (0..reqs.len) |i| { for (0..reqs.len) |i| {
reqs[i] = try client.create(try std.Uri.parse(url)); reqs[i] = try client.create(try std.Uri.parse(url));
reqs[i].fetch(); reqs[i].fetch();

View File

@@ -98,23 +98,17 @@ pub const XMLHttpRequest = struct {
asyn: bool = true, asyn: bool = true,
err: ?anyerror = null, err: ?anyerror = null,
pub fn constructor(alloc: std.mem.Allocator, loop: *Loop) !*XMLHttpRequest { pub fn constructor(alloc: std.mem.Allocator, loop: *Loop) !XMLHttpRequest {
var req = try alloc.create(XMLHttpRequest); return .{
req.* = XMLHttpRequest{
.proto = try XMLHttpRequestEventTarget.constructor(), .proto = try XMLHttpRequestEventTarget.constructor(),
.headers = .{ .allocator = alloc, .owned = false }, .headers = .{ .allocator = alloc, .owned = false },
.impl = undefined, .impl = YieldImpl.init(loop),
.url = null, .url = null,
.uri = undefined, .uri = undefined,
.readyState = UNSENT, .readyState = UNSENT,
// TODO retrieve the HTTP client globally to reuse existing connections. // TODO retrieve the HTTP client globally to reuse existing connections.
.cli = .{ .cli = .{ .allocator = alloc, .loop = loop },
.allocator = alloc,
.loop = loop,
},
}; };
req.impl = YieldImpl.init(loop, req);
return req;
} }
pub fn deinit(self: *XMLHttpRequest, alloc: std.mem.Allocator) void { pub fn deinit(self: *XMLHttpRequest, alloc: std.mem.Allocator) void {
@@ -123,7 +117,6 @@ pub const XMLHttpRequest = struct {
if (self.url) |url| alloc.free(url); if (self.url) |url| alloc.free(url);
// TODO the client must be shared between requests. // TODO the client must be shared between requests.
self.cli.deinit(); self.cli.deinit();
alloc.destroy(self);
} }
pub fn get_readyState(self: *XMLHttpRequest) u16 { pub fn get_readyState(self: *XMLHttpRequest) u16 {
@@ -176,7 +169,7 @@ pub const XMLHttpRequest = struct {
} }
pub fn _send(self: *XMLHttpRequest) void { pub fn _send(self: *XMLHttpRequest) void {
self.impl.yield(); self.impl.yield(self);
} }
fn onerr(self: *XMLHttpRequest, err: anyerror) void { fn onerr(self: *XMLHttpRequest, err: anyerror) void {