aboutsummaryrefslogtreecommitdiffstats
path: root/src/io/socketstream.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/socketstream.nim')
-rw-r--r--src/io/socketstream.nim37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/io/socketstream.nim b/src/io/socketstream.nim
new file mode 100644
index 00000000..73a00aa2
--- /dev/null
+++ b/src/io/socketstream.nim
@@ -0,0 +1,37 @@
+import net
+import streams
+
+type SocketStream* = ref object of Stream
+ isource: Socket
+ osource: Socket
+ isend: bool
+
+proc sockReadData(s: Stream, buffer: pointer, len: int): int =
+ let s = SocketStream(s)
+ result = s.isource.recv(buffer, len)
+ if result < 0:
+ raise newException(Defect, "Failed to read data")
+ elif result < len:
+ s.isend = true
+
+proc sockWriteData(s: Stream, buffer: pointer, len: int) =
+ discard SocketStream(s).osource.send(buffer, len)
+
+proc sockAtEnd(s: Stream): bool =
+ SocketStream(s).isend
+
+proc sockClose(s: Stream) = {.cast(tags: []).}: #...sigh
+ let s = SocketStream(s)
+ if s.isource != nil:
+ s.isource.close()
+ if s.osource != nil and s.isource != s.osource:
+ s.osource.close()
+
+func newSocketStream*(isource, osource: Socket): SocketStream =
+ new(result)
+ result.isource = isource
+ result.osource = osource
+ result.readDataImpl = sockReadData
+ result.writeDataImpl = sockWriteData
+ result.atEndImpl = sockAtEnd
+ result.closeImpl = sockClose