Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle file descriptors as usize #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lwip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn release() void {

const VTable = struct {
lwip_new_tcp_pcb: *const fn (ip_type: u8) callconv(.C) usize = lwip_new_tcp_pcb,
lwip_set_fd: *const fn (pcb: *anyopaque, fd_ptr: *i32) callconv(.C) void = lwip_set_fd,
lwip_set_fd: *const fn (pcb: *anyopaque, fd_ptr: *usize) callconv(.C) void = lwip_set_fd,
lwip_tcp_bind: *const fn (pcb: *anyopaque, ipaddr: *anyopaque, port: i32) callconv(.C) i8 = lwip_tcp_bind,
tcp_listen_with_backlog: *const fn (pcb: *anyopaque, backblog: u8) callconv(.C) ?*anyopaque = tcp_listen_with_backlog,
lwip_accept: *const fn (pcb: *anyopaque) callconv(.C) void = lwip_accept,
Expand All @@ -35,7 +35,7 @@ const VTable = struct {
};

extern fn lwip_new_tcp_pcb(ip_type: u8) usize;
extern fn lwip_set_fd(pcb: *anyopaque, fd_ptr: *i32) void;
extern fn lwip_set_fd(pcb: *anyopaque, fd_ptr: *usize) void;
extern fn lwip_tcp_bind(pcb: *anyopaque, ipaddr: *anyopaque, port: i32) i8;
extern fn tcp_listen_with_backlog(pcb: *anyopaque, backblog: u8) ?*anyopaque;
extern fn lwip_accept(pcb: *anyopaque) void;
Expand Down
2 changes: 1 addition & 1 deletion src/poll.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn poll(wasi_subscriptions: []WasiSubscription, events: []Event, nsubscripti
else
sub.content.type.fd_write.fd;

const s = stream.fd_table.get(fd);
const s = stream.fd_table.get(@intCast(fd));
if (s == null) {
events[nevents] = Event{
.userdata = sub.userdata,
Expand Down
16 changes: 8 additions & 8 deletions src/stream.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const FdTable = struct {

const Self = @This();

pub fn get(self: *Self, fd: i32) ?*Stream {
pub fn get(self: *Self, fd: usize) ?*Stream {
const streams = self.streams.acquire();
defer self.streams.release();
const s = &streams.*[@as(usize, @intCast(fd))];
const s = &streams.*[fd];
if (s.* == null) {
return null;
}
Expand All @@ -41,7 +41,7 @@ const FdTable = struct {
}

// If the stream has fd field, it will be set to the new fd
pub fn set(self: *Self, stream: Stream) Stream.Error!i32 {
pub fn set(self: *Self, stream: Stream) Stream.Error!usize {
const streams = self.streams.acquire();
defer self.streams.release();
var i = (self.index + 1) % STREAM_NUM;
Expand All @@ -52,27 +52,27 @@ const FdTable = struct {
streams.*[i] = stream;
const set_stream = &streams.*[i];

const new_fd = @as(i32, @intCast(i));
// const new_fd = @as(i32, @intCast(i));
switch (set_stream.*.?) {
Stream.uart => {},
Stream.socket => |*sock| {
sock.setFd(new_fd);
sock.setFd(i);
},
Stream.opened_file => {},
Stream.dir => {},
}

return new_fd;
return @intCast(i);
}
}

return Stream.Error.FdFull;
}

pub fn remove(self: *Self, fd: i32) void {
pub fn remove(self: *Self, fd: usize) void {
const streams = self.streams.acquire();
defer self.streams.release();
streams.*[@as(usize, @intCast(fd))] = null;
streams.*[fd] = null;
}
};

Expand Down
18 changes: 9 additions & 9 deletions src/tcpip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub const Socket = struct {
pcb_addr: usize,
buffer: sync.SpinLock(util.RingBuffer),
waiter: sync.Waiter,
fd: i32 = -1,
fd: usize = 0,
flags: u16 = 0,
is_connected: bool = false,
is_read_shutdown: bool = false,
Expand Down Expand Up @@ -261,7 +261,7 @@ pub const Socket = struct {
return port;
}

pub fn setFd(self: *Self, fd: i32) void {
pub fn setFd(self: *Self, fd: usize) void {
self.fd = fd;
lwip.acquire().lwip_set_fd(@as(*anyopaque, @ptrFromInt(self.pcb_addr)), &self.fd);
lwip.release();
Expand Down Expand Up @@ -329,7 +329,7 @@ export fn transmit(addr: [*c]u8, size: u32) callconv(.C) void {
}

export fn socketPush(fd: i32, ptr: [*]u8, len: usize) i32 {
const s = stream.fd_table.get(fd) orelse @panic("socketPush: invalid fd");
const s = stream.fd_table.get(@intCast(fd)) orelse @panic("socketPush: invalid fd");
var socket = switch (s.*) {
stream.Stream.socket => &s.socket,
else => @panic("socketPush: invalid fd"),
Expand All @@ -342,9 +342,9 @@ export fn socketPush(fd: i32, ptr: [*]u8, len: usize) i32 {
return 0;
}

export fn notifyAccepted(pcb: *anyopaque, fd: i32) callconv(.C) ?*i32 {
export fn notifyAccepted(pcb: *anyopaque, fd: i32) callconv(.C) ?*usize {
// unset waiter
const s = stream.fd_table.get(fd) orelse @panic("notifyAccepted: invalid fd");
const s = stream.fd_table.get(@intCast(fd)) orelse @panic("notifyAccepted: invalid fd");
var socket = switch (s.*) {
stream.Stream.socket => &s.socket,
else => @panic("notifyAccepted: invalid fd"),
Expand All @@ -364,7 +364,7 @@ export fn notifyAccepted(pcb: *anyopaque, fd: i32) callconv(.C) ?*i32 {
// This function is called when in the lwIP receive callback.
// It notifies the socket that data is available by setting the waiter.
export fn notifyReceived(fd: i32) callconv(.C) void {
const s = stream.fd_table.get(fd) orelse @panic("notifyConnected: invalid fd");
const s = stream.fd_table.get(@intCast(fd)) orelse @panic("notifyConnected: invalid fd");
var socket = switch (s.*) {
stream.Stream.socket => &s.socket,
else => @panic("notifyReceived: invalid fd"),
Expand All @@ -376,7 +376,7 @@ export fn notifyReceived(fd: i32) callconv(.C) void {
}

export fn notifyConnected(fd: i32) callconv(.C) void {
const s = stream.fd_table.get(fd) orelse @panic("notifyConnected: invalid fd");
const s = stream.fd_table.get(@intCast(fd)) orelse @panic("notifyConnected: invalid fd");
var socket = switch (s.*) {
stream.Stream.socket => &s.socket,
else => @panic("notifyConnected: invalid fd"),
Expand All @@ -387,7 +387,7 @@ export fn notifyConnected(fd: i32) callconv(.C) void {

export fn notifyClosed(fd: i32) callconv(.C) void {
// if the socket is already closed, just return
const s = stream.fd_table.get(fd) orelse return;
const s = stream.fd_table.get(@intCast(fd)) orelse return;
var socket = switch (s.*) {
stream.Stream.socket => &s.socket,
else => @panic("notifyClosed: invalid fd"),
Expand All @@ -400,7 +400,7 @@ export fn notifyError(fd: i32, err: i32) callconv(.C) void {
_ = err;

// if the socket is already closed, just return
const s = stream.fd_table.get(fd) orelse return;
const s = stream.fd_table.get(@intCast(fd)) orelse return;
var socket = switch (s.*) {
stream.Stream.socket => &s.socket,
else => @panic("notifyError: invalid fd"),
Expand Down
Loading
Loading