1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net: add -d trace_tcp support for more TcpConn methods; support again -d net_blocking_sockets too

This commit is contained in:
Delyan Angelov 2021-08-27 13:10:34 +03:00
parent a85467eb0f
commit c954c2834a
2 changed files with 56 additions and 16 deletions

View File

@ -42,6 +42,9 @@ pub fn dial_tcp(address string) ?&TcpConn {
} }
pub fn (mut c TcpConn) close() ? { pub fn (mut c TcpConn) close() ? {
$if trace_tcp ? {
eprintln(' TcpConn.close | c.sock.handle: ${c.sock.handle:6}')
}
c.sock.close() ? c.sock.close() ?
} }
@ -52,6 +55,10 @@ pub fn (mut c TcpConn) write_ptr(b &byte, len int) ?int {
'>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' + '>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
unsafe { b.vstring_with_len(len) }) unsafe { b.vstring_with_len(len) })
} }
$if trace_tcp_data_write ? {
eprintln('>>> TcpConn.write_ptr | data.len: ${len:6} | data: ' +
unsafe { b.vstring_with_len(len) })
}
unsafe { unsafe {
mut ptr_base := &byte(b) mut ptr_base := &byte(b)
mut total_sent := 0 mut total_sent := 0
@ -59,6 +66,9 @@ pub fn (mut c TcpConn) write_ptr(b &byte, len int) ?int {
ptr := ptr_base + total_sent ptr := ptr_base + total_sent
remaining := len - total_sent remaining := len - total_sent
mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal) mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal)
$if trace_tcp_data_write ? {
eprintln('>>> TcpConn.write_ptr | data chunk, total_sent: ${total_sent:6}, chunk_size: ${chunk_size:6}, sent: ${sent:6}, ptr: ${ptr_str(ptr)}')
}
if sent < 0 { if sent < 0 {
code := error_code() code := error_code()
if code == int(error_ewouldblock) { if code == int(error_ewouldblock) {
@ -90,6 +100,10 @@ pub fn (mut c TcpConn) read_ptr(buf_ptr &byte, len int) ?int {
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res') eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
} }
if res > 0 { if res > 0 {
$if trace_tcp_data_read ? {
eprintln('<<< TcpConn.read_ptr | 1 data.len: ${res:6} | data: ' +
unsafe { buf_ptr.vstring_with_len(res) })
}
return res return res
} }
code := error_code() code := error_code()
@ -99,6 +113,12 @@ pub fn (mut c TcpConn) read_ptr(buf_ptr &byte, len int) ?int {
$if trace_tcp ? { $if trace_tcp ? {
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res') eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
} }
$if trace_tcp_data_read ? {
if res > 0 {
eprintln('<<< TcpConn.read_ptr | 2 data.len: ${res:6} | data: ' +
unsafe { buf_ptr.vstring_with_len(res) })
}
}
return socket_error(res) return socket_error(res)
} else { } else {
wrap_error(code) ? wrap_error(code) ?
@ -211,6 +231,9 @@ pub fn listen_tcp(family AddrFamily, saddr string) ?&TcpListener {
} }
pub fn (mut l TcpListener) accept() ?&TcpConn { pub fn (mut l TcpListener) accept() ?&TcpConn {
$if trace_tcp ? {
eprintln(' TcpListener.accept | l.sock.handle: ${l.sock.handle:6}')
}
addr := Addr{ addr := Addr{
addr: AddrData{ addr: AddrData{
Ip6: Ip6{} Ip6: Ip6{}
@ -226,6 +249,9 @@ pub fn (mut l TcpListener) accept() ?&TcpConn {
} }
} }
new_sock := tcp_socket_from_handle(new_handle) ? new_sock := tcp_socket_from_handle(new_handle) ?
$if trace_tcp ? {
eprintln(' TcpListener.accept | << new_sock.handle: ${new_sock.handle:6}')
}
return &TcpConn{ return &TcpConn{
sock: new_sock sock: new_sock
read_timeout: net.tcp_default_read_timeout read_timeout: net.tcp_default_read_timeout
@ -274,6 +300,10 @@ fn new_tcp_socket(family AddrFamily) ?TcpSocket {
mut s := TcpSocket{ mut s := TcpSocket{
handle: handle handle: handle
} }
$if trace_tcp ? {
eprintln(' new_tcp_socket | s.handle: ${s.handle:6}')
}
// TODO(emily): // TODO(emily):
// we shouldnt be using ioctlsocket in the 21st century // we shouldnt be using ioctlsocket in the 21st century
// use the non-blocking socket option instead please :) // use the non-blocking socket option instead please :)
@ -281,11 +311,14 @@ fn new_tcp_socket(family AddrFamily) ?TcpSocket {
// TODO(emily): // TODO(emily):
// Move this to its own function on the socket // Move this to its own function on the socket
s.set_option_int(.reuse_addr, 1) ? s.set_option_int(.reuse_addr, 1) ?
$if windows {
t := u32(1) // true $if !net_blocking_sockets ? {
socket_error(C.ioctlsocket(handle, fionbio, &t)) ? $if windows {
} $else { t := u32(1) // true
socket_error(C.fcntl(handle, C.F_SETFL, C.fcntl(handle, C.F_GETFL) | C.O_NONBLOCK)) ? socket_error(C.ioctlsocket(handle, fionbio, &t)) ?
} $else {
socket_error(C.fcntl(handle, C.F_SETFL, C.fcntl(handle, C.F_GETFL) | C.O_NONBLOCK)) ?
}
} }
return s return s
} }
@ -294,16 +327,21 @@ fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
mut s := TcpSocket{ mut s := TcpSocket{
handle: sockfd handle: sockfd
} }
$if trace_tcp ? {
eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}')
}
// s.set_option_bool(.reuse_addr, true)? // s.set_option_bool(.reuse_addr, true)?
s.set_option_int(.reuse_addr, 1) ? s.set_option_int(.reuse_addr, 1) ?
s.set_dualstack(true) or { s.set_dualstack(true) or {
// Not ipv6, we dont care // Not ipv6, we dont care
} }
$if windows { $if !net_blocking_sockets ? {
t := u32(1) // true $if windows {
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? t := u32(1) // true
} $else { socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? } $else {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
}
} }
return s return s
} }

View File

@ -213,12 +213,14 @@ fn new_udp_socket(local_addr Addr) ?&UdpSocket {
s.set_dualstack(true) ? s.set_dualstack(true) ?
} }
// NOTE: refer to comments in tcp.v $if !net_blocking_sockets ? {
$if windows { // NOTE: refer to comments in tcp.v
t := u32(1) // true $if windows {
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? t := u32(1) // true
} $else { socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ? } $else {
socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ?
}
} }
// cast to the correct type // cast to the correct type