diff options
author | bptato <nincsnevem662@gmail.com> | 2022-12-31 14:05:41 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-12-31 14:05:41 +0100 |
commit | 7b6a2c6d4d1d055953d1e72ca6096490f368f922 (patch) | |
tree | b62a763e349df0e77b2ed508bcc50b4c08036518 /src | |
parent | e9cc4edbb3695651b0abd7e33e40d942ad98ab2a (diff) | |
download | chawan-7b6a2c6d4d1d055953d1e72ca6096490f368f922.tar.gz |
posixstream/socketstream: fix cross-platform compilation
Diffstat (limited to 'src')
-rw-r--r-- | src/io/posixstream.nim | 25 | ||||
-rw-r--r-- | src/ips/socketstream.nim | 21 |
2 files changed, 26 insertions, 20 deletions
diff --git a/src/io/posixstream.nim b/src/io/posixstream.nim index b686be9b..8378c194 100644 --- a/src/io/posixstream.nim +++ b/src/io/posixstream.nim @@ -14,6 +14,22 @@ type ErrorInterrupted* = object of IOError ErrorInvalid* = object of IOError +proc raisePosixIOError*() = + # In the nim stdlib, these are only constants on linux amd64, so we + # can't use a switch. + if errno == EAGAIN: + raise newException(ErrorAgain, "eagain") + elif errno == EWOULDBLOCK: + raise newException(ErrorWouldBlock, "would block") + elif errno == EBADF: + raise newException(ErrorBadFD, "bad fd") + elif errno == EFAULT: + raise newException(ErrorFault, "fault") + elif errno == EINVAL: + raise newException(ErrorInvalid, "invalid") + else: + raise newException(IOError, $strerror(errno)) + proc psReadData(s: Stream, buffer: pointer, len: int): int = assert len != 0 let s = cast[PosixStream](s) @@ -30,14 +46,7 @@ proc psReadData(s: Stream, buffer: pointer, len: int): int = if result == 0: raise newException(EOFError, "eof") if result == -1: - if errno == EAGAIN: - raise newException(ErrorAgain, "eagain") - case errno - of EWOULDBLOCK: raise newException(ErrorWouldBlock, "would block") - of EBADF: raise newException(ErrorBadFD, "bad fd") - of EFAULT: raise newException(ErrorFault, "fault") - of EINVAL: raise newException(ErrorInvalid, "invalid") - else: raise newException(IOError, $strerror(errno) & " (" & $errno & ")") + raisePosixIOError() proc psWriteData(s: Stream, buffer: pointer, len: int) = let s = cast[PosixStream](s) diff --git a/src/ips/socketstream.nim b/src/ips/socketstream.nim index e934e7fc..87fcb4e4 100644 --- a/src/ips/socketstream.nim +++ b/src/ips/socketstream.nim @@ -34,14 +34,7 @@ proc sockReadData(s: Stream, buffer: pointer, len: int): int = s.isend = true raise newException(EOFError, "eof") if result < 0: - if errno == EAGAIN: - raise newException(ErrorAgain, "eagain") - case errno - of EWOULDBLOCK: raise newException(ErrorWouldBlock, "would block") - of EBADF: raise newException(ErrorBadFD, "bad fd") - of EFAULT: raise newException(ErrorFault, "fault") - of EINVAL: raise newException(ErrorInvalid, "invalid") - else: raise newException(IOError, $strerror(errno)) + raisePosixIOError() elif result == 0: s.isend = true @@ -73,10 +66,13 @@ proc sendFileHandle*(s: SocketStream, fd: FileHandle) = hdr.msg_iov = addr iov hdr.msg_iovlen = 1 hdr.msg_control = cmsgbuf - hdr.msg_controllen = CMSG_LEN(csize_t(sizeof(FileHandle))) let cmsg = CMSG_FIRSTHDR(addr hdr) # ...sigh + # FileHandle is cint, so sizeof(FileHandle) in c is sizeof(int). + when sizeof(FileHandle) != sizeof(cint): + error("Or not...") {.emit: [ + hdr.msg_controllen, """ = CMSG_LEN(sizeof(int));""", cmsg.cmsg_len, """ = CMSG_LEN(sizeof(int));""" ].} cmsg.cmsg_level = SOL_SOCKET @@ -89,16 +85,17 @@ proc sendFileHandle*(s: SocketStream, fd: FileHandle) = proc recvFileHandle*(s: SocketStream): FileHandle = var iov: IOVec var hdr: Tmsghdr - let space = CMSG_SPACE(csize_t(sizeof(FileHandle))) var buf: char - var cmsgbuf = alloc(space) + var cmsgbuf = alloc(CMSG_SPACE(csize_t(sizeof(FileHandle)))) iov.iov_base = addr buf iov.iov_len = 1 zeroMem(addr hdr, sizeof(hdr)) hdr.msg_iov = addr iov hdr.msg_iovlen = 1 hdr.msg_control = cmsgbuf - hdr.msg_controllen = space + {.emit: [ + hdr.msg_controllen, """ = CMSG_SPACE(sizeof(int));""" + ].} let n = recvmsg(s.source.getFd(), addr hdr, 0) assert n != 0, "Unexpected EOF" #TODO remove this assert n > 0, "Failed to receive message " & $osLastError() #TODO remove this |