fix crashes in server mode
This commit is contained in:
parent
44fd55871a
commit
cfe24f70dc
26
quicp2p.nim
26
quicp2p.nim
|
@ -40,8 +40,10 @@ proc PEM_read_PrivateKey(fp: File, x: ptr EVP_PKEY,
|
|||
|
||||
proc EVP_PKEY_free(key: EVP_PKEY) {.importc, dynlib: DLLSSLName, cdecl.}
|
||||
|
||||
proc getRelativeTimeout(ctx: QuicP2PContext): int =
|
||||
result = int.high
|
||||
proc getRelativeTimeout(ctx: QuicP2PContext): int32 =
|
||||
## Obtain the absolute int64 timeout (in microseconds) from quicly and convert
|
||||
## it to the relative int32 timeout (in milliseconds) expected by poll.
|
||||
result = 0
|
||||
var nextTimeout = int64.high
|
||||
var now = ctx.quiclyCtx.now.cb(ctx.quiclyCtx.now)
|
||||
for c in ctx.connections:
|
||||
|
@ -49,9 +51,9 @@ proc getRelativeTimeout(ctx: QuicP2PContext): int =
|
|||
if connTimeout < nextTimeout:
|
||||
nextTimeout = connTimeout
|
||||
if now < nextTimeout:
|
||||
let delta = nextTimeout - now
|
||||
# convert from microseconds to milliseconds
|
||||
result = int(delta div 1_000)
|
||||
# calculate time until nexTimeout convert from microseconds to milliseconds
|
||||
let delta = (nextTimeout - now) div 1_000
|
||||
result = min(delta, int32.high).int32
|
||||
|
||||
proc onStopSending(stream: ptr quicly_stream_t, err: cint) {.cdecl.} =
|
||||
echo "onStopSending"
|
||||
|
@ -142,14 +144,14 @@ proc handleMsg(ctx: QuicP2PContext, msg: string) {.async.} =
|
|||
proc initContext(sock: AsyncSocket,
|
||||
streamOpenCb: typeof(quicly_stream_open_t.cb)):
|
||||
QuicP2PContext =
|
||||
result.sock = sock
|
||||
result.streamOpen = quicly_stream_open_t(cb: streamOpenCb)
|
||||
result.tlsCtx = ptls_context_t(randomBytes: ptlsOpensslRandomBytes,
|
||||
var tlsCtx = ptls_context_t(randomBytes: ptlsOpensslRandomBytes,
|
||||
getTime: addr ptlsGetTime,
|
||||
keyExchanges: ptlsOpensslKeyExchanges,
|
||||
cipherSuites: ptlsOpensslCipherSuites)
|
||||
quicly_amend_ptls_context(addr result.tlsCtx)
|
||||
result.quiclyCtx = quicly_spec_context
|
||||
quicly_amend_ptls_context(addr tlsCtx)
|
||||
result = QuicP2PContext(sock: sock,
|
||||
streamOpen: quicly_stream_open_t(cb: streamOpenCb),
|
||||
tlsCtx: tlsCtx, quiclyCtx: quicly_spec_context)
|
||||
result.quiclyCtx.tls = addr result.tlsCtx
|
||||
result.quiclyCtx.stream_open = addr result.streamOpen
|
||||
if ptls_load_certificates(addr result.tlsCtx, certChainPath.cstring) != 0:
|
||||
|
@ -234,7 +236,9 @@ proc main() =
|
|||
quit(1)
|
||||
|
||||
while true:
|
||||
poll(ctx.getRelativeTimeout())
|
||||
let nextTimeout = ctx.getRelativeTimeout()
|
||||
echo "nextTimeout: ", nextTimeout
|
||||
poll(nextTimeout)
|
||||
ctx.sendPackets()
|
||||
|
||||
when isMainModule:
|
||||
|
|
Loading…
Reference in New Issue