receive as client too
This commit is contained in:
parent
3fc54cb730
commit
db92c3f48f
11
quicp2p.nim
11
quicp2p.nim
|
@ -117,7 +117,7 @@ proc onClientStreamOpen(self: ptr quicly_stream_open_t,
|
|||
result = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t).csize_t)
|
||||
stream.callbacks = addr streamCallbacksClient
|
||||
|
||||
proc handleMsg(ctx: QuicP2PContext, msg: string) {.async.} =
|
||||
proc handleMsg(ctx: QuicP2PContext, msg: string, isServer: bool) =
|
||||
var offset: csize_t = 0
|
||||
while offset < msg.len().csize_t:
|
||||
var decoded: quicly_decoded_packet_t
|
||||
|
@ -137,7 +137,7 @@ proc handleMsg(ctx: QuicP2PContext, msg: string) {.async.} =
|
|||
break
|
||||
if conn != nil:
|
||||
discard quicly_receive(conn, nil, addr sockAddr, addr decoded)
|
||||
else:
|
||||
elif isServer:
|
||||
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, addr sockAddr,
|
||||
addr decoded, nil, addr ctx.nextCid, nil)
|
||||
ctx.connections.add(conn)
|
||||
|
@ -187,10 +187,10 @@ proc sendPackets(ctx: QuicP2PContext) =
|
|||
else:
|
||||
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:
|
||||
let msg = await sock.recv(BufferSize)
|
||||
asyncCheck handleMsg(ctx, msg)
|
||||
handleMsg(ctx, msg, isServer)
|
||||
|
||||
proc main() =
|
||||
var ctx: QuicP2PContext
|
||||
|
@ -203,7 +203,7 @@ proc main() =
|
|||
quit(1)
|
||||
ctx = initContext(sock, onServerStreamOpen)
|
||||
sock.bindAddr(Port(portNumber))
|
||||
asyncCheck handleClients(ctx, sock)
|
||||
asyncCheck receive(ctx, sock, true)
|
||||
|
||||
of 2:
|
||||
let hostname = paramStr(1)
|
||||
|
@ -231,6 +231,7 @@ proc main() =
|
|||
ctx.connections.add(conn)
|
||||
var stream: ptr quicly_stream_t
|
||||
discard quicly_open_stream(conn, addr stream, 0)
|
||||
asyncCheck receive(ctx, sock, false)
|
||||
|
||||
else:
|
||||
usage()
|
||||
|
|
Loading…
Reference in New Issue