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

net: add read_ptr/2 (from read/1) to UdpConn for consistency with TcpConn #24000

Merged
merged 2 commits into from
Mar 21, 2025
Merged
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
21 changes: 13 additions & 8 deletions vlib/net/udp.c.v
Original file line number Diff line number Diff line change
@@ -94,25 +94,25 @@ 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
}
code := error_code()
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)
}

Loading