From 0e39df24d43476c224aa623784397c555e8ab9ef Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 23 Sep 2019 19:48:18 +0300 Subject: [PATCH] net: make socket.recv return the allocated buffer and the message length --- vlib/net/socket.v | 19 ++++++++----------- vlib/net/socket_test.v | 8 ++++++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/vlib/net/socket.v b/vlib/net/socket.v index f2a02f7997..02c81693a0 100644 --- a/vlib/net/socket.v +++ b/vlib/net/socket.v @@ -206,22 +206,19 @@ pub fn dial(address string, port int) ?Socket { } // send string data to socket -pub fn (s Socket) send(buf byteptr, len int) int { - res := C.send(s.sockfd, buf, len, 0) -// if res < 0 { -// return error('socket: send failed') -// } +pub fn (s Socket) send(buf byteptr, len int) int? { + res := int( C.send(s.sockfd, buf, len, 0) ) + if res < 0 { + return error('socket: send failed') + } return res } // receive string data from socket -pub fn (s Socket) recv(bufsize int) byteptr { +pub fn (s Socket) recv(bufsize int) (byteptr, int) { buf := malloc(bufsize) - res := C.recv(s.sockfd, buf, bufsize, 0) -// if res < 0 { -// return error('socket: recv failed') -// } - return buf + res := int( C.recv(s.sockfd, buf, bufsize, 0) ) + return buf, res } // TODO: remove cread/2 and crecv/2 when the Go net interface is done diff --git a/vlib/net/socket_test.v b/vlib/net/socket_test.v index 4c23c588a7..f968054944 100644 --- a/vlib/net/socket_test.v +++ b/vlib/net/socket_test.v @@ -17,9 +17,13 @@ fn test_socket() { message := 'Hello World' socket.send(message.str, message.len) + $if debug { println('message send: $message') } + $if debug { println('send socket: $socket.sockfd') } - bytes := client.recv(1024) - received := tos(bytes, message.len) + bytes, blen := client.recv(1024) + received := tos(bytes, blen) + $if debug { println('message received: $received') } + $if debug { println('client: $client.sockfd') } assert message == received