net #

Constants #

const no_timeout = time.Duration(0)
const infinite_timeout = time.infinite
const (
	err_new_socket_failed   = error_with_code('net: new_socket failed to create socket',
		errors_base + 1)
	err_option_not_settable = error_with_code('net: set_option_xxx option not settable',
		errors_base + 2)
	err_option_wrong_type   = error_with_code('net: set_option_xxx option wrong type',
		errors_base + 3)
	err_port_out_of_range   = error_with_code('', errors_base + 5)
	err_no_udp_remote       = error_with_code('', errors_base + 6)
	err_connect_failed      = error_with_code('net: connect failed', errors_base + 7)
	err_connect_timed_out   = error_with_code('net: connect timed out', errors_base + 8)
	err_timed_out           = error_with_code('net: op timed out', errors_base + 9)
	err_timed_out_code      = errors_base + 9
)

Well defined errors that are returned from socket functions

const (
	msg_nosignal = 0x4000
)

fn addr_from_socket_handle #

fn addr_from_socket_handle(handle int) Addr

fn dial_tcp #

fn dial_tcp(address string) ?&TcpConn

fn dial_udp #

fn dial_udp(raddr string) ?&UdpConn

fn listen_tcp #

fn listen_tcp(family AddrFamily, saddr string) ?&TcpListener

fn listen_udp #

fn listen_udp(laddr string) ?&UdpConn

fn resolve_addrs #

fn resolve_addrs(addr string, family AddrFamily, @type SocketType) ?[]Addr

fn resolve_addrs_fuzzy #

fn resolve_addrs_fuzzy(addr string, @type SocketType) ?[]Addr

fn resolve_ipaddrs #

fn resolve_ipaddrs(addr string, family AddrFamily, typ SocketType) ?[]Addr

fn socket_error #

fn socket_error(potential_code int) ?int

fn split_address #

fn split_address(addr string) ?(string, u16)

split address splits an address into its host name and its port

fn validate_port #

fn validate_port(port int) ?u16

validate_port checks whether a port is valid and returns the port or an error

fn wrap_error #

fn wrap_error(error_code int) ?

fn (Addr) family #

fn (a Addr) family() AddrFamily

fn (TcpSocket) set_option_bool #

fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ?

fn (TcpSocket) set_dualstack #

fn (mut s TcpSocket) set_dualstack(on bool) ?

fn (TcpSocket) set_option_int #

fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ?

fn (TcpSocket) address #

fn (s &TcpSocket) address() ?Addr

address gets the address of a socket

fn (UdpSocket) set_option_bool #

fn (mut s UdpSocket) set_option_bool(opt SocketOption, value bool) ?

fn (UdpSocket) set_dualstack #

fn (mut s UdpSocket) set_dualstack(on bool) ?

enum AddrFamily #

enum AddrFamily {
	unix = C.AF_UNIX
	ip = C.AF_INET
	ip6 = C.AF_INET6
	unspec = C.AF_UNSPEC
}

AddrFamily are the available address families

enum SocketOption #

enum SocketOption {
	broadcast = C.SO_BROADCAST
	debug = C.SO_DEBUG
	dont_route = C.SO_DONTROUTE
	error = C.SO_ERROR
	keep_alive = C.SO_KEEPALIVE
	linger = C.SO_LINGER
	oob_inline = C.SO_OOBINLINE
	reuse_addr = C.SO_REUSEADDR
	recieve_buf_size = C.SO_RCVBUF
	recieve_low_size = C.SO_RCVLOWAT
	recieve_timeout = C.SO_RCVTIMEO
	send_buf_size = C.SO_SNDBUF
	send_low_size = C.SO_SNDLOWAT
	send_timeout = C.SO_SNDTIMEO
	socket_type = C.SO_TYPE
	ipv6_only = C.IPV6_V6ONLY
}

enum SocketType #

enum SocketType {
	udp = C.SOCK_DGRAM
	tcp = C.SOCK_STREAM
	seqpacket = C.SOCK_SEQPACKET
}

SocketType are the available sockets

struct C.fd_set #

struct C.fd_set {}

struct TcpConn #

struct TcpConn {
pub mut:
	sock TcpSocket
mut:
	write_deadline time.Time
	read_deadline  time.Time
	read_timeout   time.Duration
	write_timeout  time.Duration
	is_blocking    bool
}

fn (TcpConn) addr #

fn (c &TcpConn) addr() ?Addr

fn (TcpConn) close #

fn (mut c TcpConn) close() ?

fn (TcpConn) get_blocking #

fn (mut con TcpConn) get_blocking() bool

get_blocking returns whether the connection is in a blocking state, that is calls to .read_line, C.recv etc will block till there is new data arrived, instead of returning immediately.

fn (TcpConn) peer_addr #

fn (c &TcpConn) peer_addr() ?Addr

fn (TcpConn) peer_ip #

fn (c &TcpConn) peer_ip() ?string

fn (TcpConn) read #

fn (c TcpConn) read(mut buf []byte) ?int

fn (TcpConn) read_deadline #

fn (mut c TcpConn) read_deadline() ?time.Time

fn (TcpConn) read_line #

fn (mut con TcpConn) read_line() string

read_line is a simple, non customizable, blocking line reader.
It will always return a line, ending with CRLF, or just '', on EOF.
NB: if you want more control over the buffer, please use a buffered IO reader instead: io.new_buffered_reader({reader: io.make_reader(con)})

fn (TcpConn) read_ptr #

fn (c TcpConn) read_ptr(buf_ptr &byte, len int) ?int

fn (TcpConn) read_timeout #

fn (c &TcpConn) read_timeout() time.Duration

fn (TcpConn) set_blocking #

fn (mut con TcpConn) set_blocking(state bool) ?

set_blocking will change the state of the connection to either blocking, when state is true, or non blocking (false).
The default for net tcp connections is the non blocking mode.
Calling .read_line will set the connection to blocking mode.

fn (TcpConn) set_read_deadline #

fn (mut c TcpConn) set_read_deadline(deadline time.Time)

fn (TcpConn) set_read_timeout #

fn (mut c TcpConn) set_read_timeout(t time.Duration)

fn (TcpConn) set_write_deadline #

fn (mut c TcpConn) set_write_deadline(deadline time.Time)

fn (TcpConn) set_write_timeout #

fn (mut c TcpConn) set_write_timeout(t time.Duration)

fn (TcpConn) str #

fn (c TcpConn) str() string

fn (TcpConn) wait_for_read #

fn (c TcpConn) wait_for_read() ?

fn (TcpConn) wait_for_write #

fn (mut c TcpConn) wait_for_write() ?

fn (TcpConn) write #

fn (mut c TcpConn) write(bytes []byte) ?int

write blocks and attempts to write all data

fn (TcpConn) write_deadline #

fn (mut c TcpConn) write_deadline() ?time.Time

fn (TcpConn) write_ptr #

fn (mut c TcpConn) write_ptr(b &byte, len int) ?int

write_ptr blocks and attempts to write all data

fn (TcpConn) write_string #

fn (mut c TcpConn) write_string(s string) ?int

write_string blocks and attempts to write all data

fn (TcpConn) write_timeout #

fn (c &TcpConn) write_timeout() time.Duration

struct TcpListener #

struct TcpListener {
pub mut:
	sock TcpSocket
mut:
	accept_timeout  time.Duration
	accept_deadline time.Time
}

fn (TcpListener) accept #

fn (mut l TcpListener) accept() ?&TcpConn

fn (TcpListener) accept_deadline #

fn (c &TcpListener) accept_deadline() ?time.Time

fn (TcpListener) set_accept_deadline #

fn (mut c TcpListener) set_accept_deadline(deadline time.Time)

fn (TcpListener) accept_timeout #

fn (c &TcpListener) accept_timeout() time.Duration

fn (TcpListener) set_accept_timeout #

fn (mut c TcpListener) set_accept_timeout(t time.Duration)

fn (TcpListener) wait_for_accept #

fn (mut c TcpListener) wait_for_accept() ?

fn (TcpListener) close #

fn (mut c TcpListener) close() ?

fn (TcpListener) addr #

fn (c &TcpListener) addr() ?Addr

struct UdpConn #

struct UdpConn {
pub mut:
	sock UdpSocket
mut:
	write_deadline time.Time
	read_deadline  time.Time
	read_timeout   time.Duration
	write_timeout  time.Duration
}

fn (UdpConn) write_ptr #

fn (mut c UdpConn) write_ptr(b &byte, len int) ?int
sock := UdpSocket{ 		handle: sbase.handle 		l: local 		r: resolve_wrapper(raddr) 	} }

fn (UdpConn) write #

fn (mut c UdpConn) write(buf []byte) ?int

fn (UdpConn) write_string #

fn (mut c UdpConn) write_string(s string) ?int

fn (UdpConn) write_to_ptr #

fn (mut c UdpConn) write_to_ptr(addr Addr, b &byte, len int) ?int

fn (UdpConn) write_to #

fn (mut c UdpConn) write_to(addr Addr, buf []byte) ?int

write_to blocks and writes the buf to the remote addr specified

fn (UdpConn) write_to_string #

fn (mut c UdpConn) write_to_string(addr Addr, s string) ?int

write_to_string blocks and writes the buf to the remote addr specified

fn (UdpConn) read #

fn (mut c UdpConn) read(mut buf []byte) ?(int, Addr)

read reads from the socket into buf up to buf.len returning the number of bytes read

fn (UdpConn) read_deadline #

fn (c &UdpConn) read_deadline() ?time.Time

fn (UdpConn) set_read_deadline #

fn (mut c UdpConn) set_read_deadline(deadline time.Time)

fn (UdpConn) write_deadline #

fn (c &UdpConn) write_deadline() ?time.Time

fn (UdpConn) set_write_deadline #

fn (mut c UdpConn) set_write_deadline(deadline time.Time)

fn (UdpConn) read_timeout #

fn (c &UdpConn) read_timeout() time.Duration

fn (UdpConn) set_read_timeout #

fn (mut c UdpConn) set_read_timeout(t time.Duration)

fn (UdpConn) write_timeout #

fn (c &UdpConn) write_timeout() time.Duration

fn (UdpConn) set_write_timeout #

fn (mut c UdpConn) set_write_timeout(t time.Duration)

fn (UdpConn) wait_for_read #

fn (mut c UdpConn) wait_for_read() ?

fn (UdpConn) wait_for_write #

fn (mut c UdpConn) wait_for_write() ?

fn (UdpConn) str #

fn (c &UdpConn) str() string

fn (UdpConn) close #

fn (mut c UdpConn) close() ?