net: make socket.recv return the allocated buffer and the message length
							parent
							
								
									bf1ee28194
								
							
						
					
					
						commit
						0e39df24d4
					
				| 
						 | 
					@ -206,22 +206,19 @@ pub fn dial(address string, port int) ?Socket {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// send string data to socket
 | 
					// send string data to socket
 | 
				
			||||||
pub fn (s Socket) send(buf byteptr, len int) int {
 | 
					pub fn (s Socket) send(buf byteptr, len int) int? {
 | 
				
			||||||
	res := C.send(s.sockfd, buf, len, 0)
 | 
						res := int( C.send(s.sockfd, buf, len, 0) )
 | 
				
			||||||
//	if res < 0 {
 | 
						if res < 0 {
 | 
				
			||||||
//		return error('socket: send failed')
 | 
							return error('socket: send failed')
 | 
				
			||||||
//	}
 | 
						}
 | 
				
			||||||
	return res
 | 
						return res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// receive string data from socket
 | 
					// 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)
 | 
						buf := malloc(bufsize)
 | 
				
			||||||
	res := C.recv(s.sockfd, buf, bufsize, 0)
 | 
						res := int( C.recv(s.sockfd, buf, bufsize, 0) )
 | 
				
			||||||
//	if res < 0 {
 | 
						return buf, res
 | 
				
			||||||
//		return error('socket: recv failed')
 | 
					 | 
				
			||||||
//	}
 | 
					 | 
				
			||||||
	return buf
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: remove cread/2 and crecv/2 when the Go net interface is done
 | 
					// TODO: remove cread/2 and crecv/2 when the Go net interface is done
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,9 +17,13 @@ fn test_socket() {
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	message := 'Hello World'
 | 
						message := 'Hello World'
 | 
				
			||||||
	socket.send(message.str, message.len)	
 | 
						socket.send(message.str, message.len)	
 | 
				
			||||||
 | 
						$if debug {	println('message send: $message')	}
 | 
				
			||||||
 | 
						$if debug {	println('send socket: $socket.sockfd')	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bytes := client.recv(1024)
 | 
						bytes, blen := client.recv(1024) 
 | 
				
			||||||
	received := tos(bytes, message.len)
 | 
						received := tos(bytes, blen)
 | 
				
			||||||
 | 
						$if debug {	println('message received: $received')	}
 | 
				
			||||||
 | 
						$if debug {	println('client: $client.sockfd')	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert message == received
 | 
						assert message == received
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue