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

This commit is contained in:
larpon 2025-03-21 22:49:31 +01:00 committed by GitHub
parent 7bbe510a8d
commit bd064dd695
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)
}