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,12 +3,11 @@ import net
|
||||
import time
|
||||
import io
|
||||
|
||||
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')?
|
||||
// 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 {
|
||||
@ -19,3 +18,4 @@ for {
|
||||
// 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
|
||||
|
||||
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')?
|
||||
// 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())
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -2,14 +2,17 @@ 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
|
||||
|
||||
mut:
|
||||
write_deadline time.Time
|
||||
read_deadline time.Time
|
||||
|
||||
read_timeout time.Duration
|
||||
write_timeout time.Duration
|
||||
}
|
||||
@ -17,12 +20,10 @@ mut:
|
||||
pub fn dial_tcp(address string) ?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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
@ -72,11 +71,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 res > 0 {
|
||||
return res
|
||||
}
|
||||
|
||||
code := error_code()
|
||||
match code {
|
||||
error_ewouldblock {
|
||||
@ -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)) ?
|
||||
|
||||
return new_addr(addr)
|
||||
}
|
||||
|
||||
@ -171,7 +166,6 @@ pub fn (c TcpConn) str() string {
|
||||
|
||||
pub struct TcpListener {
|
||||
sock TcpSocket
|
||||
|
||||
mut:
|
||||
accept_timeout time.Duration
|
||||
accept_deadline time.Time
|
||||
@ -179,21 +173,16 @@ mut:
|
||||
|
||||
pub fn listen_tcp(port int) ?TcpListener {
|
||||
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{
|
||||
sock: s
|
||||
accept_deadline: no_deadline
|
||||
@ -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() ?
|
||||
|
||||
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) ?
|
||||
|
||||
return TcpConn{
|
||||
sock: new_sock
|
||||
read_timeout: tcp_default_read_timeout
|
||||
write_timeout: tcp_default_write_timeout
|
||||
}
|
||||
}
|
||||
|
||||
@ -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))) ?
|
||||
|
||||
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))) ?
|
||||
|
||||
return none
|
||||
}
|
||||
|
||||
@ -332,15 +311,11 @@ const (
|
||||
|
||||
fn (s TcpSocket) connect(a string) ? {
|
||||
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) ?
|
||||
if write_result {
|
||||
// succeeded
|
||||
|
@ -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