Skip to content

Commit

Permalink
more wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Jan 26, 2024
1 parent e38fcea commit d7fc6e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
19 changes: 9 additions & 10 deletions src/bun.js/ipc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ const NamedPipeIPCData = struct {
return .{ .err = err };
}
ipc_pipe.data = @ptrCast(instance);
if (ipc_pipe.listenNamedPipe(named_pipe, 0, NewNamedPipeIPCHandler(Context).onNewClientConnect).asErr()) |err| {
if (ipc_pipe.listenNamedPipe(named_pipe, 0, instance, NewNamedPipeIPCHandler(Context).onNewClientConnect).asErr()) |err| {
return .{ .err = err };
}

Expand All @@ -340,7 +340,7 @@ const NamedPipeIPCData = struct {
try ipc_pipe.init(uv.Loop.get(), true).unwrap();
ipc_pipe.data = @ptrCast(instance);
this.connect_req.data = @ptrCast(instance);
try ipc_pipe.connect(&this.connect_req, named_pipe, NewNamedPipeIPCHandler(Context).onConnect).unwrap();
try ipc_pipe.connect(&this.connect_req, named_pipe, instance, NewNamedPipeIPCHandler(Context).onConnect).unwrap();

this.writeVersionPacket();
}
Expand Down Expand Up @@ -569,13 +569,12 @@ fn NewNamedPipeIPCHandler(comptime Context: type) type {
}
}

pub fn onNewClientConnect(req: *uv.uv_stream_t, status: c_int) callconv(.C) void {
log("onNewClientConnect {d}", .{status});
if (status < 0) {
pub fn onNewClientConnect(this: *Context, status: uv.ReturnCode) void {
log("onNewClientConnect {d}", .{status.int()});
if (status.errEnum()) |_| {
Output.printErrorln("Failed to connect IPC pipe", .{});
return;
}
const this = bun.cast(*Context, req.data);
const client = &this.ipc.pipe;
const server = &this.ipc.server;
client.init(uv.Loop.get(), true).unwrap() catch {
Expand All @@ -600,13 +599,13 @@ fn NewNamedPipeIPCHandler(comptime Context: type) type {
},
}
}
pub fn onConnect(req: *uv.uv_connect_t, status: c_int) callconv(.C) void {
log("onConnect {d}", .{status});
if (status < 0) {

pub fn onConnect(this: *Context, status: uv.ReturnCode) void {
log("onConnect {d}", .{status.int()});
if (status.errEnum()) |_| {
Output.printErrorln("Failed to connect IPC pipe", .{});
return;
}
const this = bun.cast(*Context, req.data);
this.ipc.pipe.readStart(this, onReadAlloc, onReadError, onRead).unwrap() catch {
this.ipc.close(Context);
Output.printErrorln("Failed to connect IPC pipe", .{});
Expand Down
32 changes: 21 additions & 11 deletions src/deps/libuv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,18 @@ fn ReqMixin(comptime Type: type) type {
// https://docs.libuv.org/en/v1.x/stream.html
fn StreamMixin(comptime Type: type) type {
return struct {

// pub extern fn uv_write(req: *uv_write_t, handle: *uv_stream_t, bufs: [*]const uv_buf_t, nbufs: c_uint, cb: uv_write_cb) ReturnCode;
// pub extern fn uv_write2(req: *uv_write_t, handle: *uv_stream_t, bufs: [*]const uv_buf_t, nbufs: c_uint, send_handle: *uv_stream_t, cb: uv_write_cb) ReturnCode;
pub fn getWriteQueueSize(this: *Type) usize {
return uv_stream_get_write_queue_size(@ptrCast(this));
}

pub fn listen(this: *Type, backlog: i32, cb: uv_connection_cb) Maybe(void) {
const rc = uv_listen(@ptrCast(this), backlog, cb);
pub fn listen(this: *Type, backlog: i32, context: anytype, comptime onConnect: *const (fn (@TypeOf(context), ReturnCode) void)) Maybe(void) {
this.data = @ptrCast(context);
const Wrapper = struct {
pub fn uvConnectCb(handle: *uv_stream_t, status: ReturnCode) callconv(.C) void {
onConnect(@ptrCast(@alignCast(handle.data)), status);
}
};
const rc = uv_listen(@ptrCast(this), backlog, &Wrapper.uvConnectCb);
if (rc.errno()) |errno| {
return .{ .err = .{ .errno = errno, .syscall = .listen, .from_libuv = true } };
}
Expand Down Expand Up @@ -1094,7 +1097,7 @@ const struct_unnamed_385 = extern struct {
write_reqs_pending: c_uint,
shutdown_req: [*c]uv_shutdown_t,
};
pub const uv_connection_cb = ?*const fn (*uv_stream_t, c_int) callconv(.C) void;
pub const uv_connection_cb = ?*const fn (*uv_stream_t, ReturnCode) callconv(.C) void;
const struct_unnamed_389 = extern struct {
connection_cb: uv_connection_cb,
};
Expand Down Expand Up @@ -1402,11 +1405,11 @@ pub const struct_uv_pipe_s = extern struct {
return .{ .result = {} };
}

pub fn listenNamedPipe(this: *@This(), named_pipe: []const u8, backlog: i32, onClientConnect: uv_connection_cb) Maybe(void) {
pub fn listenNamedPipe(this: *@This(), named_pipe: []const u8, backlog: i32, context: anytype, comptime onClientConnect: *const (fn (@TypeOf(context), ReturnCode) void)) Maybe(void) {
if (this.bind(named_pipe, 0).asErr()) |err| {
return .{ .err = err };
}
return this.listen(backlog, onClientConnect);
return this.listen(backlog, context, onClientConnect);
}

pub fn bind(this: *@This(), named_pipe: []const u8, flags: i32) Maybe(void) {
Expand All @@ -1417,8 +1420,15 @@ pub const struct_uv_pipe_s = extern struct {
return .{ .result = {} };
}

pub fn connect(this: *@This(), req: *uv_connect_t, name: []const u8, cb: uv_connect_cb) Maybe(void) {
const rc = uv_pipe_connect2(req, this, @ptrCast(name.ptr), name.len, 0, cb);
pub fn connect(this: *@This(), req: *uv_connect_t, name: []const u8, context: anytype, comptime onConnect: *const (fn (@TypeOf(context), ReturnCode) void)) Maybe(void) {
this.data = @ptrCast(context);
const Wrapper = struct {
pub fn uvConnectCb(handle: *uv_connect_t, status: ReturnCode) callconv(.C) void {
onConnect(@ptrCast(@alignCast(handle.data)), status);
}
};

const rc = uv_pipe_connect2(req, this, @ptrCast(name.ptr), name.len, 0, &Wrapper.uvConnectCb);
if (rc.errno()) |errno| {
return .{ .err = .{ .errno = errno, .syscall = .connect2, .from_libuv = true } };
}
Expand Down Expand Up @@ -1740,7 +1750,7 @@ const union_unnamed_441 = extern union {
connect: struct_unnamed_443,
};
pub const uv_connect_t = struct_uv_connect_s;
pub const uv_connect_cb = ?*const fn (*uv_connect_t, c_int) callconv(.C) void;
pub const uv_connect_cb = ?*const fn (*uv_connect_t, ReturnCode) callconv(.C) void;
pub const struct_uv_connect_s = extern struct {
data: ?*anyopaque,
type: uv_req_type,
Expand Down

0 comments on commit d7fc6e0

Please sign in to comment.