mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net: add tcp_default_read_timeout and tcp_default_write_timeout and use them consistently
This commit is contained in:
parent
560c21629e
commit
e3a1756b11
@ -175,8 +175,6 @@ fn (mut cfg DocConfig) serve_html() {
|
||||
server.close() or { }
|
||||
panic(err)
|
||||
}
|
||||
conn.set_read_timeout(5 * time.second)
|
||||
conn.set_write_timeout(5 * time.second)
|
||||
handle_http_connection(mut conn, server_context)
|
||||
conn.close() or { eprintln('error closing the connection: $err') }
|
||||
}
|
||||
|
@ -3,19 +3,19 @@ import net
|
||||
import time
|
||||
import io
|
||||
|
||||
// Make a new connection
|
||||
mut conn := net.dial_tcp('google.com:80')?
|
||||
// Simple http HEAD request for a file
|
||||
conn.write_str('GET /index.html HTTP/1.0\r\n\r\n')?
|
||||
// Make sure to set a timeout so we can wait for a response!
|
||||
conn.set_read_timeout(10 * time.second)
|
||||
// Wrap in a buffered reader
|
||||
mut r := io.new_buffered_reader(reader: io.make_reader(conn))
|
||||
for {
|
||||
l := r.read_line() or {
|
||||
break
|
||||
fn main() {
|
||||
// Make a new connection
|
||||
mut conn := net.dial_tcp('google.com:80')?
|
||||
// Simple http HEAD request for a file
|
||||
conn.write_str('GET /index.html HTTP/1.0\r\n\r\n')?
|
||||
// Wrap in a buffered reader
|
||||
mut r := io.new_buffered_reader(reader: io.make_reader(conn))
|
||||
for {
|
||||
l := r.read_line() or {
|
||||
break
|
||||
}
|
||||
println('$l')
|
||||
// Make it nice and obvious that we are doing this line by line
|
||||
time.sleep_ms(10)
|
||||
}
|
||||
println('$l')
|
||||
// Make it nice and obvious that we are doing this line by line
|
||||
time.sleep_ms(10)
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
// Simple raw HTTP head request
|
||||
import net
|
||||
import io
|
||||
|
||||
// Make a new connection
|
||||
mut conn := net.dial_tcp('google.com:80')?
|
||||
defer { conn.close() }
|
||||
// Simple http HEAD request for a file
|
||||
conn.write_str('HEAD /index.html HTTP/1.0\r\n\r\n')?
|
||||
// Make sure to set a timeout so we can wait for a response!
|
||||
conn.set_read_timeout(net.infinite_timeout)
|
||||
// Read all the data that is waiting
|
||||
result := io.read_all(conn)?
|
||||
// Cast to string and print result
|
||||
println(result.bytestr())
|
||||
fn main() {
|
||||
// Make a new connection
|
||||
mut conn := net.dial_tcp('google.com:80')?
|
||||
defer { conn.close() }
|
||||
// Simple http HEAD request for a file
|
||||
conn.write_str('HEAD /index.html HTTP/1.0\r\n\r\n')?
|
||||
// Read all the data that is waiting
|
||||
result := io.read_all(conn)?
|
||||
// Cast to string and print result
|
||||
println(result.bytestr())
|
||||
}
|
||||
|
@ -49,7 +49,9 @@ fn (mut r BufferedReader) fill_buffer() ? {
|
||||
// trying to call this
|
||||
r.offset = 0
|
||||
new_len := r.reader.read(mut r.buf) or {
|
||||
eprintln('>> BufferedReader.reader.read err: $err')
|
||||
if errcode != 0 || err.len != 0 {
|
||||
eprintln('>> BufferedReader.reader.read err: $err | errcode: $errcode')
|
||||
}
|
||||
0
|
||||
}
|
||||
r.len = new_len
|
||||
|
@ -6,7 +6,6 @@ module http
|
||||
import net.urllib
|
||||
import net.http.chunked
|
||||
import net
|
||||
import time
|
||||
import io
|
||||
|
||||
const (
|
||||
@ -398,7 +397,6 @@ fn (req &Request) http_do(host string, method Method, path string) ?Response {
|
||||
s := req.build_request_headers(method, host_name, path)
|
||||
mut client := net.dial_tcp(host)?
|
||||
// TODO this really needs to be exposed somehow
|
||||
client.set_read_timeout(time.second * 30)
|
||||
client.write(s.bytes())?
|
||||
mut bytes := io.read_all(client)?
|
||||
client.close()
|
||||
|
127
vlib/net/tcp.v
127
vlib/net/tcp.v
@ -2,32 +2,33 @@ module net
|
||||
|
||||
import time
|
||||
|
||||
const (
|
||||
tcp_default_read_timeout = 30 * time.second
|
||||
tcp_default_write_timeout = 30 * time.second
|
||||
)
|
||||
|
||||
pub struct TcpConn {
|
||||
pub:
|
||||
sock TcpSocket
|
||||
|
||||
sock TcpSocket
|
||||
mut:
|
||||
write_deadline time.Time
|
||||
read_deadline time.Time
|
||||
|
||||
read_timeout time.Duration
|
||||
write_timeout time.Duration
|
||||
read_deadline time.Time
|
||||
read_timeout time.Duration
|
||||
write_timeout time.Duration
|
||||
}
|
||||
|
||||
pub fn dial_tcp(address string) ?TcpConn {
|
||||
s := new_tcp_socket()?
|
||||
s.connect(address)?
|
||||
|
||||
return TcpConn {
|
||||
s := new_tcp_socket() ?
|
||||
s.connect(address) ?
|
||||
return TcpConn{
|
||||
sock: s
|
||||
|
||||
read_timeout: 30 * time.second
|
||||
write_timeout: 30 * time.second
|
||||
read_timeout: tcp_default_read_timeout
|
||||
write_timeout: tcp_default_write_timeout
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (c TcpConn) close() ? {
|
||||
c.sock.close()?
|
||||
c.sock.close() ?
|
||||
return none
|
||||
}
|
||||
|
||||
@ -36,12 +37,10 @@ pub fn (c TcpConn) write_ptr(b byteptr, len int) ? {
|
||||
unsafe {
|
||||
mut ptr_base := byteptr(b)
|
||||
mut total_sent := 0
|
||||
|
||||
for total_sent < len {
|
||||
ptr := ptr_base + total_sent
|
||||
remaining := len - total_sent
|
||||
mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal)
|
||||
|
||||
if sent < 0 {
|
||||
code := error_code()
|
||||
match code {
|
||||
@ -50,7 +49,7 @@ pub fn (c TcpConn) write_ptr(b byteptr, len int) ? {
|
||||
continue
|
||||
}
|
||||
else {
|
||||
wrap_error(code)?
|
||||
wrap_error(code) ?
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -71,21 +70,19 @@ 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))?
|
||||
|
||||
mut res := wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
|
||||
if res > 0 {
|
||||
return res
|
||||
}
|
||||
|
||||
code := error_code()
|
||||
match code {
|
||||
error_ewouldblock {
|
||||
c.wait_for_read()?
|
||||
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0))?
|
||||
c.wait_for_read() ?
|
||||
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
|
||||
return socket_error(res)
|
||||
}
|
||||
else {
|
||||
wrap_error(code)?
|
||||
wrap_error(code) ?
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,7 +117,7 @@ pub fn (c TcpConn) read_timeout() time.Duration {
|
||||
return c.read_timeout
|
||||
}
|
||||
|
||||
pub fn(mut c TcpConn) set_read_timeout(t time.Duration) {
|
||||
pub fn (mut c TcpConn) set_read_timeout(t time.Duration) {
|
||||
c.read_timeout = t
|
||||
}
|
||||
|
||||
@ -145,9 +142,7 @@ pub fn (c TcpConn) wait_for_write() ? {
|
||||
pub fn (c TcpConn) peer_addr() ?Addr {
|
||||
mut addr := C.sockaddr{}
|
||||
len := sizeof(C.sockaddr)
|
||||
|
||||
socket_error(C.getpeername(c.sock.handle, &addr, &len))?
|
||||
|
||||
socket_error(C.getpeername(c.sock.handle, &addr, &len)) ?
|
||||
return new_addr(addr)
|
||||
}
|
||||
|
||||
@ -155,7 +150,7 @@ pub fn (c TcpConn) peer_ip() ?string {
|
||||
buf := [44]byte{}
|
||||
peeraddr := C.sockaddr_in{}
|
||||
speeraddr := sizeof(peeraddr)
|
||||
socket_error(C.getpeername(c.sock.handle, &C.sockaddr(&peeraddr), &speeraddr))?
|
||||
socket_error(C.getpeername(c.sock.handle, &C.sockaddr(&peeraddr), &speeraddr)) ?
|
||||
cstr := C.inet_ntop(C.AF_INET, &peeraddr.sin_addr, buf, sizeof(buf))
|
||||
if cstr == 0 {
|
||||
return error('net.peer_ip: inet_ntop failed')
|
||||
@ -170,31 +165,25 @@ pub fn (c TcpConn) str() string {
|
||||
}
|
||||
|
||||
pub struct TcpListener {
|
||||
sock TcpSocket
|
||||
|
||||
sock TcpSocket
|
||||
mut:
|
||||
accept_timeout time.Duration
|
||||
accept_timeout time.Duration
|
||||
accept_deadline time.Time
|
||||
}
|
||||
|
||||
pub fn listen_tcp(port int) ?TcpListener {
|
||||
s := new_tcp_socket()?
|
||||
|
||||
validate_port(port)?
|
||||
|
||||
s := new_tcp_socket() ?
|
||||
validate_port(port) ?
|
||||
mut addr := C.sockaddr_in{}
|
||||
addr.sin_family = SocketFamily.inet
|
||||
addr.sin_port = C.htons(port)
|
||||
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
|
||||
size := sizeof(C.sockaddr_in)
|
||||
|
||||
// cast to the correct type
|
||||
sockaddr := &C.sockaddr(&addr)
|
||||
|
||||
socket_error(C.bind(s.handle, sockaddr, size))?
|
||||
socket_error(C.listen(s.handle, 128))?
|
||||
|
||||
return TcpListener {
|
||||
socket_error(C.bind(s.handle, sockaddr, size)) ?
|
||||
socket_error(C.listen(s.handle, 128)) ?
|
||||
return TcpListener{
|
||||
sock: s
|
||||
accept_deadline: no_deadline
|
||||
accept_timeout: infinite_timeout
|
||||
@ -203,29 +192,23 @@ 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)
|
||||
mut new_handle := C.accept(l.sock.handle, sock_addr, &size)
|
||||
|
||||
if new_handle <= 0 {
|
||||
l.wait_for_accept()?
|
||||
|
||||
l.wait_for_accept() ?
|
||||
new_handle = C.accept(l.sock.handle, sock_addr, &size)
|
||||
|
||||
if new_handle == -1 || new_handle == 0 {
|
||||
return none
|
||||
}
|
||||
}
|
||||
|
||||
new_sock := tcp_socket_from_handle(new_handle)?
|
||||
|
||||
new_sock := tcp_socket_from_handle(new_handle) ?
|
||||
return TcpConn{
|
||||
sock: new_sock
|
||||
read_timeout: tcp_default_read_timeout
|
||||
write_timeout: tcp_default_write_timeout
|
||||
}
|
||||
}
|
||||
|
||||
@ -244,7 +227,7 @@ pub fn (c TcpListener) accept_timeout() time.Duration {
|
||||
return c.accept_timeout
|
||||
}
|
||||
|
||||
pub fn(mut c TcpListener) set_accept_timeout(t time.Duration) {
|
||||
pub fn (mut c TcpListener) set_accept_timeout(t time.Duration) {
|
||||
c.accept_timeout = t
|
||||
}
|
||||
|
||||
@ -253,7 +236,7 @@ pub fn (c TcpListener) wait_for_accept() ? {
|
||||
}
|
||||
|
||||
pub fn (c TcpListener) close() ? {
|
||||
c.sock.close()?
|
||||
c.sock.close() ?
|
||||
return none
|
||||
}
|
||||
|
||||
@ -267,15 +250,15 @@ pub:
|
||||
}
|
||||
|
||||
fn new_tcp_socket() ?TcpSocket {
|
||||
sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.tcp, 0))?
|
||||
s := TcpSocket {
|
||||
sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.tcp, 0)) ?
|
||||
s := TcpSocket{
|
||||
handle: sockfd
|
||||
}
|
||||
//s.set_option_bool(.reuse_addr, true)?
|
||||
s.set_option_int(.reuse_addr, 1)?
|
||||
// s.set_option_bool(.reuse_addr, true)?
|
||||
s.set_option_int(.reuse_addr, 1) ?
|
||||
$if windows {
|
||||
t := true
|
||||
socket_error(C.ioctlsocket(sockfd, fionbio, &t))?
|
||||
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
|
||||
} $else {
|
||||
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK))
|
||||
}
|
||||
@ -283,14 +266,14 @@ fn new_tcp_socket() ?TcpSocket {
|
||||
}
|
||||
|
||||
fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
|
||||
s := TcpSocket {
|
||||
s := TcpSocket{
|
||||
handle: sockfd
|
||||
}
|
||||
//s.set_option_bool(.reuse_addr, true)?
|
||||
s.set_option_int(.reuse_addr, 1)?
|
||||
// s.set_option_bool(.reuse_addr, true)?
|
||||
s.set_option_int(.reuse_addr, 1) ?
|
||||
$if windows {
|
||||
t := true
|
||||
socket_error(C.ioctlsocket(sockfd, fionbio, &t))?
|
||||
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
|
||||
} $else {
|
||||
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK))
|
||||
}
|
||||
@ -302,19 +285,15 @@ pub fn (s TcpSocket) set_option_bool(opt SocketOption, value bool) ? {
|
||||
// if opt !in opts_can_set {
|
||||
// return err_option_not_settable
|
||||
// }
|
||||
|
||||
// if opt !in opts_bool {
|
||||
// return err_option_wrong_type
|
||||
// }
|
||||
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool)))?
|
||||
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ?
|
||||
return none
|
||||
}
|
||||
|
||||
pub fn (s TcpSocket) set_option_int(opt SocketOption, value int) ? {
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int)))?
|
||||
|
||||
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int))) ?
|
||||
return none
|
||||
}
|
||||
|
||||
@ -331,22 +310,18 @@ const (
|
||||
)
|
||||
|
||||
fn (s TcpSocket) connect(a string) ? {
|
||||
addr := resolve_addr(a, .inet, .tcp)?
|
||||
|
||||
addr := resolve_addr(a, .inet, .tcp) ?
|
||||
res := C.connect(s.handle, &addr.addr, addr.len)
|
||||
|
||||
if res == 0 {
|
||||
return none
|
||||
}
|
||||
|
||||
_ := error_code()
|
||||
|
||||
write_result := s.@select(.write, connect_timeout)?
|
||||
write_result := s.@select(.write, connect_timeout) ?
|
||||
if write_result {
|
||||
// succeeded
|
||||
return none
|
||||
}
|
||||
except_result := s.@select(.except, connect_timeout)?
|
||||
except_result := s.@select(.except, connect_timeout) ?
|
||||
if except_result {
|
||||
return err_connect_failed
|
||||
}
|
||||
|
@ -14,13 +14,9 @@ fn setup() (net.TcpListener, net.TcpConn, net.TcpConn) {
|
||||
mut client := net.dial_tcp('127.0.0.1:$server_port') or {
|
||||
panic(err)
|
||||
}
|
||||
client.set_read_timeout(3 * time.second)
|
||||
client.set_write_timeout(3 * time.second)
|
||||
mut socket := server.accept() or {
|
||||
panic(err)
|
||||
}
|
||||
socket.set_read_timeout(3 * time.second)
|
||||
socket.set_write_timeout(3 * time.second)
|
||||
$if debug_peer_ip ? {
|
||||
ip := con.peer_ip() or {
|
||||
'$err'
|
||||
|
@ -7,10 +7,6 @@ const (
|
||||
|
||||
fn handle_conn(_c net.TcpConn) {
|
||||
mut c := _c
|
||||
// arbitrary timeouts to ensure that it doesnt
|
||||
// instantly throw its hands in the air and give up
|
||||
c.set_read_timeout(10 * time.second)
|
||||
c.set_write_timeout(10 * time.second)
|
||||
for {
|
||||
mut buf := []byte{len: 100, init: 0}
|
||||
read := c.read(mut buf) or {
|
||||
@ -39,10 +35,6 @@ fn echo() ? {
|
||||
defer {
|
||||
c.close() or { }
|
||||
}
|
||||
// arbitrary timeouts to ensure that it doesnt
|
||||
// instantly throw its hands in the air and give up
|
||||
c.set_read_timeout(10 * time.second)
|
||||
c.set_write_timeout(10 * time.second)
|
||||
data := 'Hello from vlib/net!'
|
||||
c.write_str(data)?
|
||||
mut buf := []byte{len: 4096}
|
||||
|
@ -3,10 +3,6 @@ import time
|
||||
|
||||
fn echo_server(_c net.UdpConn) {
|
||||
mut c := _c
|
||||
// arbitrary timeouts to ensure that it doesnt
|
||||
// instantly throw its hands in the air and give up
|
||||
c.set_read_timeout(10 * time.second)
|
||||
c.set_write_timeout(10 * time.second)
|
||||
for {
|
||||
mut buf := []byte{ len: 100, init: 0 }
|
||||
read, addr := c.read(mut buf) or {
|
||||
@ -23,12 +19,6 @@ fn echo_server(_c net.UdpConn) {
|
||||
fn echo() ? {
|
||||
mut c := net.dial_udp('127.0.0.1:40003', '127.0.0.1:40001')?
|
||||
defer { c.close() or { } }
|
||||
|
||||
// arbitrary timeouts to ensure that it doesnt
|
||||
// instantly throw its hands in the air and give up
|
||||
c.set_read_timeout(10 * time.second)
|
||||
c.set_write_timeout(10 * time.second)
|
||||
|
||||
data := 'Hello from vlib/net!'
|
||||
|
||||
c.write_str(data)?
|
||||
|
Loading…
Reference in New Issue
Block a user