| 1 | module net |
| 2 | |
| 3 | pub struct Socket { |
| 4 | pub: |
| 5 | handle int |
| 6 | } |
| 7 | |
| 8 | // address gets the address of a socket |
| 9 | pub 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). |
| 15 | pub 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 | |