diff --git a/async/async.zig b/async/async.zig index 46859963..ad96a7b8 100644 --- a/async/async.zig +++ b/async/async.zig @@ -418,7 +418,7 @@ pub const Socket = struct { }; pub const UDP = struct { - const Inner = aio.TCP; + const Inner = aio.UDP; pub const Reader = std.io.GenericReader(UDP, stdx.meta.FnSignature(UDP.read, null).ReturnErrorSet.?, UDP.read); pub const WriterContext = struct { @@ -433,21 +433,19 @@ pub const Socket = struct { inner: aio.UDP, - pub fn listen(addr: std.net.Address) !Listener(UDP) { - var self: Listener(UDP) = .{ - .inner = aio.UDP.init(AsyncThread.current.executor, try xev.UDP.init(addr)), - }; + pub fn listen(addr: std.net.Address) !UDP { + var self: UDP = .{ .inner = aio.UDP.init(AsyncThread.current.executor, try xev.UDP.init(addr)) }; try self.inner.udp.bind(addr); - try self.inner.udp.listen(1024); + // try self.inner.udp.listen(1024); return self; } - pub fn read(self: UDP, buf: []u8) !usize { - return self.inner.read(.{ .slice = buf }); + pub fn read(self: UDP, buf: []u8) !struct { usize, std.net.Address } { + return self.inner.read_addr(.{ .slice = buf }); } pub fn write(self: UDP, addr: std.net.Address, buf: []const u8) !usize { - return self.inner.write(addr, .{ .slice = buf }); + return self.inner.write_addr(addr, .{ .slice = buf }); } pub fn close(self: *UDP) !void { @@ -455,11 +453,11 @@ pub const Socket = struct { return self.inner.close(); } - pub fn reader(self: File) Reader { + pub fn reader(self: UDP) Reader { return .{ .context = self }; } - pub fn writer(self: File, addr: std.net.Address) Writer { + pub fn writer(self: UDP, addr: std.net.Address) Writer { return .{ .context = .{ .file = self, diff --git a/async/asyncio.zig b/async/asyncio.zig index bbc842cd..114c8b54 100644 --- a/async/asyncio.zig +++ b/async/asyncio.zig @@ -526,10 +526,10 @@ pub const UDP = struct { exec: *Executor, udp: xev.UDP, - pub usingnamespace Closeable(Self, xev.TCP); - pub usingnamespace Pollable(Self, xev.TCP); - pub usingnamespace Readable(Self, xev.TCP); - pub usingnamespace Writeable(Self, xev.TCP); + pub usingnamespace Closeable(Self, xev.UDP); + pub usingnamespace Pollable(Self, xev.UDP); + pub usingnamespace Readable(Self, xev.UDP); + pub usingnamespace Writeable(Self, xev.UDP); pub fn init(exec: *Executor, udp: xev.UDP) Self { return .{ .exec = exec, .udp = udp }; @@ -540,11 +540,13 @@ pub const UDP = struct { } const ReadResult = xev.ReadError!usize; - pub fn read(self: Self, buf: xev.ReadBuffer) !usize { + + pub fn read_addr(self: Self, buf: xev.ReadBuffer) !struct { usize, std.net.Address } { const ResultT = ReadResult; const Data = struct { result: ResultT = undefined, frame: ?Frame = null, + addr: std.net.Address, fn callback( userdata: ?*@This(), @@ -559,11 +561,12 @@ pub const UDP = struct { _ = l; _ = c; _ = s; - _ = addr; _ = udp; _ = b; const data = userdata.?; data.result = result; + data.addr = addr; + if (data.frame != null) libcoro.xresume(data.frame.?); return .disarm; } @@ -572,16 +575,15 @@ pub const UDP = struct { const loop = self.exec.loop; var s: xev.UDP.State = undefined; var c: xev.Completion = .{}; - var data: Data = .{ .frame = libcoro.xframe() }; + var data: Data = .{ .frame = libcoro.xframe(), .addr = undefined }; self.udp.read(loop, &c, &s, buf, Data, &data, &Data.callback); try waitForCompletion(self.exec, &c); - - return data.result; + return .{ try data.result, data.addr }; } const WriteResult = xev.WriteError!usize; - pub fn write(self: Self, addr: std.net.Address, buf: xev.WriteBuffer) !usize { + pub fn write_addr(self: Self, addr: std.net.Address, buf: xev.WriteBuffer) !usize { const ResultT = WriteResult; const Data = struct { result: ResultT = undefined,