receive as client too

This commit is contained in:
Christian Ulrich 2020-11-07 12:51:31 +01:00
parent 3fc54cb730
commit db92c3f48f
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 6 additions and 5 deletions

View File

@ -117,7 +117,7 @@ proc onClientStreamOpen(self: ptr quicly_stream_open_t,
result = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t).csize_t) result = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t).csize_t)
stream.callbacks = addr streamCallbacksClient stream.callbacks = addr streamCallbacksClient
proc handleMsg(ctx: QuicP2PContext, msg: string) {.async.} = proc handleMsg(ctx: QuicP2PContext, msg: string, isServer: bool) =
var offset: csize_t = 0 var offset: csize_t = 0
while offset < msg.len().csize_t: while offset < msg.len().csize_t:
var decoded: quicly_decoded_packet_t var decoded: quicly_decoded_packet_t
@ -137,7 +137,7 @@ proc handleMsg(ctx: QuicP2PContext, msg: string) {.async.} =
break break
if conn != nil: if conn != nil:
discard quicly_receive(conn, nil, addr sockAddr, addr decoded) discard quicly_receive(conn, nil, addr sockAddr, addr decoded)
else: elif isServer:
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, addr sockAddr, discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, addr sockAddr,
addr decoded, nil, addr ctx.nextCid, nil) addr decoded, nil, addr ctx.nextCid, nil)
ctx.connections.add(conn) ctx.connections.add(conn)
@ -187,10 +187,10 @@ proc sendPackets(ctx: QuicP2PContext) =
else: else:
raise newException(ValueError, &"quicly_send returned {sendResult}") raise newException(ValueError, &"quicly_send returned {sendResult}")
proc handleClients(ctx: QuicP2PContext, sock: AsyncSocket) {.async.} = proc receive(ctx: QuicP2PContext, sock: AsyncSocket, isServer: bool) {.async.} =
while true: while true:
let msg = await sock.recv(BufferSize) let msg = await sock.recv(BufferSize)
asyncCheck handleMsg(ctx, msg) handleMsg(ctx, msg, isServer)
proc main() = proc main() =
var ctx: QuicP2PContext var ctx: QuicP2PContext
@ -203,7 +203,7 @@ proc main() =
quit(1) quit(1)
ctx = initContext(sock, onServerStreamOpen) ctx = initContext(sock, onServerStreamOpen)
sock.bindAddr(Port(portNumber)) sock.bindAddr(Port(portNumber))
asyncCheck handleClients(ctx, sock) asyncCheck receive(ctx, sock, true)
of 2: of 2:
let hostname = paramStr(1) let hostname = paramStr(1)
@ -231,6 +231,7 @@ proc main() =
ctx.connections.add(conn) ctx.connections.add(conn)
var stream: ptr quicly_stream_t var stream: ptr quicly_stream_t
discard quicly_open_stream(conn, addr stream, 0) discard quicly_open_stream(conn, addr stream, 0)
asyncCheck receive(ctx, sock, false)
else: else:
usage() usage()