fix server side sending
This commit is contained in:
parent
87239d45e7
commit
e936bc49d9
28
quicp2p.nim
28
quicp2p.nim
|
@ -125,7 +125,8 @@ proc onClientStreamOpen(self: ptr quicly_stream_open_t,
|
|||
let msg = "hello server"
|
||||
discard quicly_streambuf_egress_write(stream, msg.cstring, msg.len().csize_t)
|
||||
|
||||
proc handleMsg(ctx: QuicP2PContext, msg: string, isServer: bool) =
|
||||
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
|
||||
|
@ -134,21 +135,17 @@ proc handleMsg(ctx: QuicP2PContext, msg: string, isServer: bool) =
|
|||
msg.len().csize_t, addr offset)
|
||||
if decode_result == csize_t.high:
|
||||
return
|
||||
let (myAddress, myPort) = ctx.sock.getLocalAddr()
|
||||
var sockAddr: Sockaddr_storage
|
||||
var sockLen: SockLen
|
||||
toSockAddr(parseIpAddress(myAddress), myPort, sockAddr, sockLen)
|
||||
var conn: ptr quicly_conn_t = nil
|
||||
for c in ctx.connections:
|
||||
if quicly_is_destination(c, nil, addr sockAddr, addr decoded) != 0:
|
||||
if quicly_is_destination(c, nil, peerAddr, addr decoded) != 0:
|
||||
conn = c
|
||||
break
|
||||
if conn != nil:
|
||||
echo "quicly_receive"
|
||||
discard quicly_receive(conn, nil, addr sockAddr, addr decoded)
|
||||
discard quicly_receive(conn, nil, peerAddr, addr decoded)
|
||||
elif isServer:
|
||||
echo "quicly_accept"
|
||||
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, addr sockAddr,
|
||||
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, peerAddr,
|
||||
addr decoded, nil, addr ctx.nextCid, nil)
|
||||
ctx.connections.add(conn)
|
||||
|
||||
|
@ -190,8 +187,8 @@ proc sendPackets(ctx: QuicP2PContext) =
|
|||
if dgramCount > 0:
|
||||
echo &"sending {dgramCount} datagrams"
|
||||
for i in 0 .. dgramCount - 1:
|
||||
#echo "dstAddr.sa: ", dstAddr.sa
|
||||
var sockLen = quicly_get_socklen(addr dstAddr.sa)
|
||||
# TODO: replace asyncdispatch.sendTo with asyncnet.sendTo (Nim 1.4 required)
|
||||
asyncCheck sendTo(ctx.sock.getFd().AsyncFD, dgrams[i].iov_base,
|
||||
dgrams[i].iov_len.int, addr dstAddr.sa, sockLen)
|
||||
of QUICLY_ERROR_FREE_CONNECTION:
|
||||
|
@ -202,10 +199,17 @@ proc sendPackets(ctx: QuicP2PContext) =
|
|||
|
||||
proc receive(ctx: QuicP2PContext, isServer: bool) {.async.} =
|
||||
while true:
|
||||
let msg = await ctx.sock.recv(BufferSize)
|
||||
echo &"received {msg.len()} bytes"
|
||||
# TODO: replace asyncdispatch.recvFromInto with asyncnet.recvFrom (Nim 1.4 required)
|
||||
var msg = newString(BufferSize)
|
||||
var peerAddr: Sockaddr_storage
|
||||
var peerAddrLen = SockLen(sizeof(peerAddr))
|
||||
let msgLen = await recvFromInto(ctx.sock.getFd().AsyncFD, msg.cstring,
|
||||
msg.len, cast[ptr SockAddr](addr peerAddr),
|
||||
addr peerAddrLen)
|
||||
echo &"received {msgLen} bytes"
|
||||
msg.setLen(msgLen)
|
||||
if msg.len > 0:
|
||||
handleMsg(ctx, msg, isServer)
|
||||
handleMsg(ctx, msg, cast[ptr SockAddr](addr peerAddr), isServer)
|
||||
|
||||
proc main() =
|
||||
var ctx: QuicP2PContext
|
||||
|
|
Loading…
Reference in New Issue