Cleanup: extract datagram reading and writing procs

This commit is contained in:
Mark Spanbroek 2020-10-07 11:04:04 +02:00 committed by markspanbroek
parent 3e79774af1
commit 5bc4c8442a
2 changed files with 40 additions and 13 deletions

View File

@ -0,0 +1,32 @@
import std/monotimes
import quic
import ngtcp2
import path
proc write*(connection: ptr ngtcp2_conn, datagram: var Datagram, datagramInfo: var ngtcp2_pkt_info): int =
var offset = 0
var length = 1
while length > 0 and offset < datagram.len:
length = connection.ngtcp2_conn_write_stream(
addr zeroPath,
addr datagramInfo,
addr datagram[offset],
(datagram.len - offset).uint,
nil,
NGTCP2_WRITE_STREAM_FLAG_MORE.uint32,
-1,
nil,
0,
getMonoTime().ticks.uint
)
offset = offset + length
offset
proc read*(connection: ptr ngtcp2_conn, datagram: Datagram, datagramInfo: ngtcp2_pkt_info) =
assert 0 == connection.ngtcp2_conn_read_pkt(
addr zeroPath,
unsafeAddr datagramInfo,
unsafeAddr datagram[0],
datagram.len.uint,
getMonoTime().ticks.uint
)

View File

@ -1,10 +1,10 @@
import unittest
import std/monotimes
import ngtcp2
import helpers/server
import helpers/client
import helpers/ids
import helpers/path
import helpers/udp
suite "ngtcp2":
@ -22,36 +22,31 @@ suite "ngtcp2":
var datagram: array[16348, uint8]
var datagramInfo: ngtcp2_pkt_info
var datagramLength = 0
# handshake client -> server
echo "--- CLIENT 1>>> SERVER"
check datagram.len == client.ngtcp2_conn_write_stream(addr zeroPath, addr datagramInfo, addr datagram[0], datagram.len.uint, nil, NGTCP2_WRITE_STREAM_FLAG_MORE.uint32, -1, nil, 0, getMonoTime().ticks.uint)
datagramLength = client.write(datagram, datagramInfo)
# setup server connection using received datagram
let server = setupServer(zeroPath, serverId, datagram)
defer: ngtcp2_conn_del(server)
echo "--- CLIENT >>>1 SERVER"
check 0 == server.ngtcp2_conn_read_pkt(addr zeroPath, addr datagramInfo, addr datagram[0], datagram.len.uint, getMonoTime().ticks.uint)
server.read(datagram[0..<datagramLength], datagramInfo)
echo "--- CLIENT <<<2 SERVER"
var offset = 0
block:
var length = 1
offset = 0
while length > 0:
length = server.ngtcp2_conn_write_stream(addr zeroPath, addr datagramInfo, addr datagram[offset], (datagram.len - offset).uint, nil, NGTCP2_WRITE_STREAM_FLAG_MORE.uint32, -1, nil, 0, getMonoTime().ticks.uint)
offset = offset + length
datagramLength = server.write(datagram, datagramInfo)
echo "--- CLIENT 2<<< SERVER"
check 0 == client.ngtcp2_conn_read_pkt(addr zeroPath, addr datagramInfo, addr datagram[0], offset.uint, getMonoTime().ticks.uint)
client.read(datagram[0..<datagramLength], datagramInfo)
echo "--- CLIENT 3>>> SERVER"
check datagram.len == client.ngtcp2_conn_write_pkt(addr zeroPath, addr datagramInfo, addr datagram[0], datagram.len.uint, getMonoTime().ticks.uint)
datagramLength = client.write(datagram, datagramInfo)
check client.ngtcp2_conn_get_handshake_completed().bool
echo "--- CLIENT >>>3 SERVER"
check 0 == server.ngtcp2_conn_read_pkt(addr zeroPath, addr datagramInfo, addr datagram[0], datagram.len.uint, getMonoTime().ticks.uint)
server.read(datagram[0..<datagramLength], datagramInfo)
check server.ngtcp2_conn_get_handshake_completed().bool