fix crashes in server mode

This commit is contained in:
Christian Ulrich 2020-11-07 12:29:08 +01:00
parent 44fd55871a
commit cfe24f70dc
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 15 additions and 11 deletions

View File

@ -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 EVP_PKEY_free(key: EVP_PKEY) {.importc, dynlib: DLLSSLName, cdecl.}
proc getRelativeTimeout(ctx: QuicP2PContext): int = proc getRelativeTimeout(ctx: QuicP2PContext): int32 =
result = int.high ## 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 nextTimeout = int64.high
var now = ctx.quiclyCtx.now.cb(ctx.quiclyCtx.now) var now = ctx.quiclyCtx.now.cb(ctx.quiclyCtx.now)
for c in ctx.connections: for c in ctx.connections:
@ -49,9 +51,9 @@ proc getRelativeTimeout(ctx: QuicP2PContext): int =
if connTimeout < nextTimeout: if connTimeout < nextTimeout:
nextTimeout = connTimeout nextTimeout = connTimeout
if now < nextTimeout: if now < nextTimeout:
let delta = nextTimeout - now # calculate time until nexTimeout convert from microseconds to milliseconds
# convert from microseconds to milliseconds let delta = (nextTimeout - now) div 1_000
result = int(delta div 1_000) result = min(delta, int32.high).int32
proc onStopSending(stream: ptr quicly_stream_t, err: cint) {.cdecl.} = proc onStopSending(stream: ptr quicly_stream_t, err: cint) {.cdecl.} =
echo "onStopSending" echo "onStopSending"
@ -142,14 +144,14 @@ proc handleMsg(ctx: QuicP2PContext, msg: string) {.async.} =
proc initContext(sock: AsyncSocket, proc initContext(sock: AsyncSocket,
streamOpenCb: typeof(quicly_stream_open_t.cb)): streamOpenCb: typeof(quicly_stream_open_t.cb)):
QuicP2PContext = QuicP2PContext =
result.sock = sock var tlsCtx = ptls_context_t(randomBytes: ptlsOpensslRandomBytes,
result.streamOpen = quicly_stream_open_t(cb: streamOpenCb)
result.tlsCtx = ptls_context_t(randomBytes: ptlsOpensslRandomBytes,
getTime: addr ptlsGetTime, getTime: addr ptlsGetTime,
keyExchanges: ptlsOpensslKeyExchanges, keyExchanges: ptlsOpensslKeyExchanges,
cipherSuites: ptlsOpensslCipherSuites) cipherSuites: ptlsOpensslCipherSuites)
quicly_amend_ptls_context(addr result.tlsCtx) quicly_amend_ptls_context(addr tlsCtx)
result.quiclyCtx = quicly_spec_context 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.tls = addr result.tlsCtx
result.quiclyCtx.stream_open = addr result.streamOpen result.quiclyCtx.stream_open = addr result.streamOpen
if ptls_load_certificates(addr result.tlsCtx, certChainPath.cstring) != 0: if ptls_load_certificates(addr result.tlsCtx, certChainPath.cstring) != 0:
@ -234,7 +236,9 @@ proc main() =
quit(1) quit(1)
while true: while true:
poll(ctx.getRelativeTimeout()) let nextTimeout = ctx.getRelativeTimeout()
echo "nextTimeout: ", nextTimeout
poll(nextTimeout)
ctx.sendPackets() ctx.sendPackets()
when isMainModule: when isMainModule: