diff options
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/posixstream.nim | 10 | ||||
-rw-r--r-- | src/io/socketstream.nim | 11 |
2 files changed, 18 insertions, 3 deletions
diff --git a/src/io/posixstream.nim b/src/io/posixstream.nim index 73a957f8..04fe0e5c 100644 --- a/src/io/posixstream.nim +++ b/src/io/posixstream.nim @@ -62,11 +62,19 @@ proc psReadData(s: Stream, buffer: pointer, len: int): int = if result == -1: raisePosixIOError() +method sendData*(s: PosixStream, buffer: pointer, len: int): int {.base.} = + #TODO use sendData instead + let n = write(s.fd, buffer, len) + if n < 0: + raisePosixIOError() + return n + proc psWriteData(s: Stream, buffer: pointer, len: int) = + #TODO use sendData instead let s = cast[PosixStream](s) let res = write(s.fd, buffer, len) if res == -1: - raise newException(IOError, $strerror(errno)) + raisePosixIOError() proc psAtEnd(s: Stream): bool = return cast[PosixStream](s).isend diff --git a/src/io/socketstream.nim b/src/io/socketstream.nim index 9426f7a7..fb378083 100644 --- a/src/io/socketstream.nim +++ b/src/io/socketstream.nim @@ -9,10 +9,9 @@ when defined(posix): import io/posixstream import io/serversocket -type SocketStream* = ref object of Stream +type SocketStream* = ref object of PosixStream source*: Socket blk*: bool - isend: bool proc sockReadData(s: Stream, buffer: pointer, len: int): int = assert len != 0 @@ -50,6 +49,12 @@ proc sockWriteData(s: Stream, buffer: pointer, len: int) = raisePosixIOError() i += n +method sendData*(s: SocketStream, buffer: pointer, len: int): int = + let n = s.source.send(buffer, len) + if n < 0: + raisePosixIOError() + return n + proc sockAtEnd(s: Stream): bool = SocketStream(s).isend @@ -124,3 +129,5 @@ proc acceptSocketStream*(ssock: ServerSocket, blocking = true): SocketStream = var sock: Socket ssock.sock.accept(sock, inheritable = true) result.source = sock + if not blocking: + sock.getFd().setBlocking(false) |