mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net: make socket.recv return the allocated buffer and the message length
This commit is contained in:
parent
bf1ee28194
commit
0e39df24d4
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user