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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# Write data to streams.
import std/options
import std/sets
import std/tables
import io/dynstream
import types/blob
import types/formdata
import types/opt
import types/url
type BufferedReader* = object
buffer: seq[uint8]
bufIdx: int
proc initReader*(stream: DynStream; len: int): BufferedReader =
assert len != 0
var reader = BufferedReader(
buffer: newSeqUninitialized[uint8](len),
bufIdx: 0
)
var n = 0
while true:
n += stream.recvData(addr reader.buffer[n], len - n)
if n == len:
break
return reader
proc initPacketReader*(stream: DynStream): BufferedReader =
var len: int
stream.recvDataLoop(addr len, sizeof(len))
return stream.initReader(len)
template withPacketReader*(stream: DynStream; r, body: untyped) =
block:
var r = stream.initPacketReader()
body
proc sread*(reader: var BufferedReader; n: var SomeNumber)
proc sread*[T](reader: var BufferedReader; s: var set[T])
proc sread*[T: enum](reader: var BufferedReader; x: var T)
proc sread*(reader: var BufferedReader; s: var string)
proc sread*(reader: var BufferedReader; b: var bool)
proc sread*(reader: var BufferedReader; url: var URL)
proc sread*(reader: var BufferedReader; tup: var tuple)
proc sread*[I, T](reader: var BufferedReader; a: var array[I, T])
proc sread*(reader: var BufferedReader; s: var seq)
proc sread*[U, V](reader: var BufferedReader; t: var Table[U, V])
proc sread*(reader: var BufferedReader; obj: var object)
proc sread*(reader: var BufferedReader; obj: var ref object)
proc sread*(reader: var BufferedReader; part: var FormDataEntry)
proc sread*(reader: var BufferedReader; blob: var Blob)
proc sread*[T](reader: var BufferedReader; o: var Option[T])
proc sread*[T, E](reader: var BufferedReader; o: var Result[T, E])
proc readData(reader: var BufferedReader; buffer: pointer; len: int) =
assert reader.bufIdx + len <= reader.buffer.len
copyMem(buffer, addr reader.buffer[reader.bufIdx], len)
reader.bufIdx += len
proc sread*(reader: var BufferedReader; n: var SomeNumber) =
reader.readData(addr n, sizeof(n))
proc sread*[T: enum](reader: var BufferedReader; x: var T) =
var i: int
reader.sread(i)
x = cast[T](i)
proc sread*[T](reader: var BufferedReader; s: var set[T]) =
var len: int
reader.sread(len)
for i in 0 ..< len:
var x: T
reader.sread(x)
s.incl(x)
proc sread*(reader: var BufferedReader; s: var string) =
var len: int
reader.sread(len)
s = newString(len)
if len > 0:
reader.readData(addr s[0], len)
proc sread*(reader: var BufferedReader; b: var bool) =
var n: uint8
reader.sread(n)
if n == 1u8:
b = true
else:
assert n == 0u8
b = false
proc sread*(reader: var BufferedReader; url: var URL) =
var s: string
reader.sread(s)
if s == "":
url = nil
else:
let x = newURL(s)
if x.isSome:
url = x.get
else:
url = nil
proc sread*(reader: var BufferedReader; tup: var tuple) =
for f in tup.fields:
reader.sread(f)
proc sread*[I; T](reader: var BufferedReader; a: var array[I, T]) =
for x in a.mitems:
reader.sread(x)
proc sread*(reader: var BufferedReader; s: var seq) =
var len: int
reader.sread(len)
s.setLen(len)
for x in s.mitems:
reader.sread(x)
proc sread*[U; V](reader: var BufferedReader; t: var Table[U, V]) =
var len: int
reader.sread(len)
for i in 0..<len:
var k: U
reader.sread(k)
var v: V
reader.sread(v)
t[k] = v
proc sread*(reader: var BufferedReader; obj: var object) =
for f in obj.fields:
reader.sread(f)
proc sread*(reader: var BufferedReader; obj: var ref object) =
var n: bool
reader.sread(n)
if n:
new(obj)
reader.sread(obj[])
proc sread*(reader: var BufferedReader; part: var FormDataEntry) =
var isstr: bool
reader.sread(isstr)
if isstr:
part = FormDataEntry(isstr: true)
else:
part = FormDataEntry(isstr: false)
reader.sread(part.name)
reader.sread(part.filename)
if part.isstr:
reader.sread(part.svalue)
else:
reader.sread(part.value)
proc sread*(reader: var BufferedReader; blob: var Blob) =
var isfile: bool
reader.sread(isfile)
if isfile:
var file = new WebFile
file.isfile = true
reader.sread(file.path)
blob = file
else:
blob = Blob()
reader.sread(blob.ctype)
reader.sread(blob.size)
let buffer = alloc(blob.size)
blob.buffer = buffer
blob.deallocFun = proc() = dealloc(buffer)
if blob.size > 0:
reader.readData(blob.buffer, int(blob.size))
proc sread*[T](reader: var BufferedReader; o: var Option[T]) =
var x: bool
reader.sread(x)
if x:
var m: T
reader.sread(m)
o = some(m)
else:
o = none(T)
proc sread*[T, E](reader: var BufferedReader; o: var Result[T, E]) =
var x: bool
reader.sread(x)
if x:
when T isnot void:
var m: T
reader.sread(m)
o.ok(m)
else:
o.ok()
else:
when E isnot void:
var e: E
reader.sread(e)
o.err(e)
else:
o.err()
|