net: add support for `-d trace_tcp` to ease debugging

pull/7688/head
Delyan Angelov 2020-12-29 15:41:46 +02:00
parent 40ce18fa3c
commit 17e0a65611
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 7 additions and 1 deletions

View File

@ -34,6 +34,9 @@ pub fn (c TcpConn) close() ? {
// write_ptr blocks and attempts to write all data
pub fn (c TcpConn) write_ptr(b byteptr, len int) ? {
$if trace_tcp ? {
eprintln('>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len')
}
unsafe {
mut ptr_base := byteptr(b)
mut total_sent := 0
@ -71,6 +74,9 @@ pub fn (c TcpConn) write_str(s string) ? {
pub fn (c TcpConn) read_ptr(buf_ptr byteptr, len int) ?int {
mut res := wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
$if trace_tcp ? {
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
}
if res > 0 {
return res
}
@ -192,7 +198,7 @@ pub fn listen_tcp(port int) ?TcpListener {
pub fn (l TcpListener) accept() ?TcpConn {
addr := C.sockaddr_storage{}
unsafe {C.memset(&addr, 0, sizeof(C.sockaddr_storage))}
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_storage)) }
size := sizeof(C.sockaddr_storage)
// cast to correct type
sock_addr := &C.sockaddr(&addr)