-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamClient.nim
73 lines (66 loc) · 1.86 KB
/
streamClient.nim
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
{.define: ssl.}
import std/strutils
import std/asyncdispatch
import ../src/hyperx/client
from ./localServer import localHost, localPort
when isMainModule:
var completed = newSeq[string]()
proc send(
strm: ClientStream, path: string, chunks: seq[string]
) {.async.} =
var contentLen = 0
for chunk in chunks:
contentLen += chunk.len
await strm.sendHeaders(
hmPost, path,
contentLen = contentLen
)
var data = new string
var i = 0
for chunk in chunks:
data[].setLen 0
data[].add chunk
await strm.sendBody(data, finish = i == chunks.high)
inc i
proc recv(
strm: ClientStream, data: ref string
) {.async.} =
await strm.recvHeaders(data)
doAssert ":status:" in data[]
data[].setLen 0
while not strm.recvEnded:
await strm.recvBody(data)
proc streamChunks(
client: ClientContext, path: string, chunks: seq[string]
) {.async.} =
let strm = client.newClientStream()
with strm:
# send and recv concurrently
var data = new string
let recvFut = strm.recv(data)
let sendFut = strm.send(path, chunks)
await recvFut
await sendFut
var expectedData = ""
for chunk in chunks:
expectedData.add chunk
doAssert expectedData == data[]
echo "Path done: " & path
echo "Received: " & data[]
completed.add path
proc main() {.async.} =
var client = newClient(localHost, localPort)
with client:
# send chunks concurrently
let chunkFut1 = streamChunks(client, "/foo", @["foo", "bar"])
let chunkFut2 = streamChunks(client, "/bar", @["baz", "qux"])
await chunkFut1
await chunkFut2
waitFor main()
doAssert completed.len == 2
doAssert "/foo" in completed
doAssert "/bar" in completed
doAssert not hasPendingOperations()
#setGlobalDispatcher(nil)
#GC_fullCollect()
echo "ok"