| ... | ... | @@ -7,8 +7,10 @@ pub error Unexpected; |
| 7 | 7 | pub error Io; |
| 8 | 8 | pub error TimedOut; |
| 9 | 9 | pub error ConnectionReset; |
| 10 | pub error ConnectionRefused; |
| 10 | 11 | pub error NoMem; |
| 11 | 12 | pub error NotSocket; |
| 13 | pub error BadFd; |
| 12 | 14 | |
| 13 | 15 | struct Connection { |
| 14 | 16 | socket_fd: i32, |
| ... | ... | @@ -27,16 +29,18 @@ struct Connection { |
| 27 | 29 | } |
| 28 | 30 | } |
| 29 | 31 | |
| 30 | | pub fn recv(c: Connection, buf: []u8) -> %isize { |
| 32 | pub fn recv(c: Connection, buf: []u8) -> %[]u8 { |
| 31 | 33 | const recv_ret = linux.recvfrom(c.socket_fd, buf.ptr, buf.len, 0, null, null); |
| 32 | 34 | const recv_err = linux.get_errno(recv_ret); |
| 33 | 35 | switch (recv_err) { |
| 34 | | 0 => return recv_ret, |
| 36 | 0 => return buf[0...recv_ret], |
| 35 | 37 | errno.EINVAL => unreachable{}, |
| 36 | 38 | errno.EFAULT => unreachable{}, |
| 37 | 39 | errno.ENOTSOCK => return error.NotSocket, |
| 38 | 40 | errno.EINTR => return error.SigInterrupt, |
| 39 | 41 | errno.ENOMEM => return error.NoMem, |
| 42 | errno.ECONNREFUSED => return error.ConnectionRefused, |
| 43 | errno.EBADF => return error.BadFd, |
| 40 | 44 | // TODO more error values |
| 41 | 45 | else => return error.Unexpected, |
| 42 | 46 | } |