v / vlib / net / socket.c.v
28 lines · 25 sloc · 643 bytes · dbc6b50cda7524c70160db55fb3201ce0f776d83
Raw
1module net
2
3pub struct Socket {
4pub:
5 handle int
6}
7
8// address gets the address of a socket
9pub fn (s &Socket) address() !Addr {
10 return addr_from_socket_handle(s.handle)
11}
12
13// set_blocking will change the state of the socket to either blocking,
14// when state is true, or non blocking (false).
15pub fn set_blocking(handle int, state bool) ! {
16 $if windows {
17 t := if state { u32(0) } else { u32(1) }
18 socket_error(C.ioctlsocket(handle, fionbio, &t))!
19 } $else {
20 mut flags := C.fcntl(handle, C.F_GETFL, 0)
21 if state {
22 flags &= ~C.O_NONBLOCK
23 } else {
24 flags |= C.O_NONBLOCK
25 }
26 socket_error(C.fcntl(handle, C.F_SETFL, flags))!
27 }
28}
29