|
| 1 | +module net |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +// Shutdown shutsdown a socket and closes it |
| 6 | +fn shutdown(handle int) ? { |
| 7 | + $if windows { |
| 8 | + C.shutdown(handle, C.SD_BOTH) |
| 9 | + socket_error(C.closesocket(handle))? |
| 10 | + } $else { |
| 11 | + C.shutdown(handle, C.SHUT_RDWR) |
| 12 | + socket_error(C.close(handle))? |
| 13 | + } |
| 14 | + |
| 15 | + return none |
| 16 | +} |
| 17 | + |
| 18 | +// Select waits for an io operation (specified by parameter `test`) to be available |
| 19 | +fn @select(handle int, test Select, timeout time.Duration) ?bool { |
| 20 | + set := C.fd_set{} |
| 21 | + |
| 22 | + C.FD_ZERO(&set) |
| 23 | + C.FD_SET(handle, &set) |
| 24 | + |
| 25 | + seconds := timeout.milliseconds() / 1000 |
| 26 | + microseconds := timeout - (seconds * time.second) |
| 27 | + |
| 28 | + mut tt := C.timeval{ |
| 29 | + tv_sec: u64(seconds) |
| 30 | + tv_usec: u64(microseconds) |
| 31 | + } |
| 32 | + |
| 33 | + mut timeval_timeout := &tt |
| 34 | + |
| 35 | + // infinite timeout is signaled by passing null as the timeout to |
| 36 | + // select |
| 37 | + if timeout == infinite_timeout { |
| 38 | + timeval_timeout = &C.timeval(0) |
| 39 | + } |
| 40 | + |
| 41 | + match test { |
| 42 | + .read { |
| 43 | + socket_error(C.@select(handle+1, &set, C.NULL, C.NULL, timeval_timeout))? |
| 44 | + } |
| 45 | + .write { |
| 46 | + socket_error(C.@select(handle+1, C.NULL, &set, C.NULL, timeval_timeout))? |
| 47 | + } |
| 48 | + .except { |
| 49 | + socket_error(C.@select(handle+1, C.NULL, C.NULL, &set, timeval_timeout))? |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return C.FD_ISSET(handle, &set) |
| 54 | +} |
| 55 | + |
| 56 | +// wait_for_common wraps the common wait code |
| 57 | +fn wait_for_common( |
| 58 | + handle int, |
| 59 | + deadline time.Time, |
| 60 | + timeout time.Duration, |
| 61 | + test Select) ? { |
| 62 | + if deadline.unix == 0 { |
| 63 | + // only accept infinite_timeout as a valid |
| 64 | + // negative timeout - it is handled in @select however |
| 65 | + if timeout < 0 && timeout != infinite_timeout { |
| 66 | + return err_timed_out |
| 67 | + } |
| 68 | + ready := @select(handle, test, timeout)? |
| 69 | + if ready { |
| 70 | + return none |
| 71 | + } |
| 72 | + return err_timed_out |
| 73 | + } |
| 74 | + // Convert the deadline into a timeout |
| 75 | + // and use that |
| 76 | + d_timeout := deadline.unix - time.now().unix |
| 77 | + if d_timeout < 0 { |
| 78 | + // deadline is in the past so this has already |
| 79 | + // timed out |
| 80 | + return err_timed_out |
| 81 | + } |
| 82 | + |
| 83 | + ready := @select(handle, test, d_timeout)? |
| 84 | + if ready { |
| 85 | + return none |
| 86 | + } |
| 87 | + return err_timed_out |
| 88 | +} |
| 89 | + |
| 90 | +// wait_for_write waits for a write io operation to be available |
| 91 | +fn wait_for_write( |
| 92 | + handle int, |
| 93 | + deadline time.Time, |
| 94 | + timeout time.Duration) ? { |
| 95 | + return wait_for_common(handle, deadline, timeout, .write) |
| 96 | +} |
| 97 | + |
| 98 | +// wait_for_read waits for a read io operation to be available |
| 99 | +fn wait_for_read( |
| 100 | + handle int, |
| 101 | + deadline time.Time, |
| 102 | + timeout time.Duration) ? { |
| 103 | + return wait_for_common(handle, deadline, timeout, .read) |
| 104 | +} |
| 105 | + |
| 106 | +// no_deadline should be given to functions when no deadline is wanted (i.e. all functions |
| 107 | +// return instantly) |
| 108 | +const ( |
| 109 | + no_deadline = time.Time{unix: 0} |
| 110 | +) |
| 111 | + |
| 112 | +// no_timeout should be given to functions when no timeout is wanted (i.e. all functions |
| 113 | +// return instantly) |
| 114 | +const ( |
| 115 | + no_timeout = time.Duration(0) |
| 116 | +) |
| 117 | + |
| 118 | +// infinite_timeout should be given to functions when an infinite_timeout is wanted (i.e. functions |
| 119 | +// only ever return with data) |
| 120 | +const ( |
| 121 | + infinite_timeout = time.Duration(-1) |
| 122 | +) |
0 commit comments