about summary refs log tree commit diff stats
path: root/src/io/multistream.nim
blob: 33038ad68ca40987e476e025aa3571aff3fe89ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# MultiStream: write to several streams at once when writing to a single
# stream.
# See TeeStream for a pull version.

import streams

type MultiStream = ref object of Stream
  s1: Stream
  s2: Stream

proc tsClose(s: Stream) =
  let s = cast[MultiStream](s)
  s.s1.close()
  s.s2.close()

proc msWriteData(s: Stream, buffer: pointer, bufLen: int) =
  let s = cast[MultiStream](s)
  s.s1.writeData(buffer, bufLen)
  s.s2.writeData(buffer, bufLen)

proc newMultiStream*(s1, s2: Stream, closedest = true): MultiStream =
  return MultiStream(
    s1: s1,
    s2: s2,
    closeImpl: tsClose,
    writeDataImpl: msWriteData
  )