cosmetic: move proc handleMsg

This commit is contained in:
Christian Ulrich 2020-11-08 13:16:03 +01:00
parent c39ecc55ac
commit 490664b1f5
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 22 additions and 22 deletions

View File

@ -122,28 +122,6 @@ proc onClientStreamOpen(self: ptr quicly_stream_open_t,
let msg = "hello server" let msg = "hello server"
discard quicly_streambuf_egress_write(stream, msg.cstring, msg.len().csize_t) discard quicly_streambuf_egress_write(stream, msg.cstring, msg.len().csize_t)
proc handleMsg(ctx: QuicP2PContext, msg: string, peerAddr: ptr SockAddr,
isServer: bool) =
var offset: csize_t = 0
while offset < msg.len().csize_t:
var decoded: quicly_decoded_packet_t
let decodeResult = quicly_decode_packet(addr ctx.quiclyCtx, addr decoded,
cast[ptr uint8](msg.cstring),
msg.len().csize_t, addr offset)
if decode_result == csize_t.high:
return
var conn: ptr quicly_conn_t = nil
for c in ctx.connections:
if quicly_is_destination(c, nil, peerAddr, addr decoded) != 0:
conn = c
break
if conn != nil:
discard quicly_receive(conn, nil, peerAddr, addr decoded)
elif isServer:
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, peerAddr,
addr decoded, nil, addr ctx.nextCid, nil)
ctx.connections.add(conn)
proc initContext(sock: AsyncSocket, certChainPath: string, keyPath: string, proc initContext(sock: AsyncSocket, certChainPath: string, keyPath: string,
streamOpenCb: typeof(quicly_stream_open_t.cb)): streamOpenCb: typeof(quicly_stream_open_t.cb)):
QuicP2PContext = QuicP2PContext =
@ -196,6 +174,28 @@ proc sendPackets(ctx: QuicP2PContext) =
else: else:
raise newException(ValueError, &"quicly_send returned {sendResult}") raise newException(ValueError, &"quicly_send returned {sendResult}")
proc handleMsg(ctx: QuicP2PContext, msg: string, peerAddr: ptr SockAddr,
isServer: bool) =
var offset: csize_t = 0
while offset < msg.len().csize_t:
var decoded: quicly_decoded_packet_t
let decodeResult = quicly_decode_packet(addr ctx.quiclyCtx, addr decoded,
cast[ptr uint8](msg.cstring),
msg.len().csize_t, addr offset)
if decode_result == csize_t.high:
return
var conn: ptr quicly_conn_t = nil
for c in ctx.connections:
if quicly_is_destination(c, nil, peerAddr, addr decoded) != 0:
conn = c
break
if conn != nil:
discard quicly_receive(conn, nil, peerAddr, addr decoded)
elif isServer:
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, peerAddr,
addr decoded, nil, addr ctx.nextCid, nil)
ctx.connections.add(conn)
proc receive(ctx: QuicP2PContext, isServer: bool) {.async.} = proc receive(ctx: QuicP2PContext, isServer: bool) {.async.} =
while true: while true:
# TODO: replace asyncdispatch.recvFromInto with asyncnet.recvFrom (Nim 1.4 required) # TODO: replace asyncdispatch.recvFromInto with asyncnet.recvFrom (Nim 1.4 required)