From bd064dd695dbfae26f559eab4b7fd7da16f19786 Mon Sep 17 00:00:00 2001 From: larpon <768942+larpon@users.noreply.github.com> Date: Fri, 21 Mar 2025 22:49:31 +0100 Subject: [PATCH] net: add `read_ptr/2` (from `read/1`) to `UdpConn` for consistency with `TcpConn` (#24000) --- vlib/net/udp.c.v | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/vlib/net/udp.c.v b/vlib/net/udp.c.v index a2897c9def..1b0e1923e0 100644 --- a/vlib/net/udp.c.v +++ b/vlib/net/udp.c.v @@ -94,16 +94,16 @@ pub fn (mut c UdpConn) write_to_string(addr Addr, s string) !int { return c.write_to_ptr(addr, s.str, s.len) } -// read reads from the socket into buf up to buf.len returning the number of bytes read -pub fn (mut c UdpConn) read(mut buf []u8) !(int, Addr) { +// read_ptr reads from the socket into `buf_ptr` up to `len` bytes, returning the number of bytes read and the `Addr` read from. +pub fn (c &UdpConn) read_ptr(buf_ptr &u8, len int) !(int, Addr) { mut addr := Addr{ addr: AddrData{ Ip6: Ip6{} } } - len := sizeof(Addr) - mut res := wrap_read_result(C.recvfrom(c.sock.handle, voidptr(buf.data), buf.len, - 0, voidptr(&addr), &len))! + addr_len := sizeof(Addr) + mut res := wrap_read_result(C.recvfrom(c.sock.handle, voidptr(buf_ptr), len, 0, voidptr(&addr), + &addr_len))! if res > 0 { return res, addr } @@ -111,8 +111,8 @@ pub fn (mut c UdpConn) read(mut buf []u8) !(int, Addr) { if code == int(error_ewouldblock) { c.wait_for_read()! // same setup as in tcp - res = wrap_read_result(C.recvfrom(c.sock.handle, voidptr(buf.data), buf.len, 0, - voidptr(&addr), &len))! + res = wrap_read_result(C.recvfrom(c.sock.handle, voidptr(buf_ptr), len, 0, voidptr(&addr), + &addr_len))! res2 := socket_error(res)! return res2, addr } else { @@ -121,6 +121,11 @@ pub fn (mut c UdpConn) read(mut buf []u8) !(int, Addr) { return error('none') } +// read reads from the socket into buf up to buf.len returning the number of bytes read +pub fn (mut c UdpConn) read(mut buf []u8) !(int, Addr) { + return c.read_ptr(buf.data, buf.len)! +} + pub fn (c &UdpConn) read_deadline() !time.Time { if c.read_deadline.unix() == 0 { return c.read_deadline @@ -160,7 +165,7 @@ pub fn (mut c UdpConn) set_write_timeout(t time.Duration) { } @[inline] -pub fn (mut c UdpConn) wait_for_read() ! { +pub fn (c &UdpConn) wait_for_read() ! { return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout) }