cosmetic: move proc handleMsg
This commit is contained in:
parent
c39ecc55ac
commit
490664b1f5
44
quicp2p.nim
44
quicp2p.nim
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue