mirror of https://github.com/vacp2p/nim-quic.git
Cleanup: extract datagram reading and writing procs
This commit is contained in:
parent
3e79774af1
commit
5bc4c8442a
|
@ -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
|
||||||
|
)
|
|
@ -1,10 +1,10 @@
|
||||||
import unittest
|
import unittest
|
||||||
import std/monotimes
|
|
||||||
import ngtcp2
|
import ngtcp2
|
||||||
import helpers/server
|
import helpers/server
|
||||||
import helpers/client
|
import helpers/client
|
||||||
import helpers/ids
|
import helpers/ids
|
||||||
import helpers/path
|
import helpers/path
|
||||||
|
import helpers/udp
|
||||||
|
|
||||||
suite "ngtcp2":
|
suite "ngtcp2":
|
||||||
|
|
||||||
|
@ -22,36 +22,31 @@ suite "ngtcp2":
|
||||||
|
|
||||||
var datagram: array[16348, uint8]
|
var datagram: array[16348, uint8]
|
||||||
var datagramInfo: ngtcp2_pkt_info
|
var datagramInfo: ngtcp2_pkt_info
|
||||||
|
var datagramLength = 0
|
||||||
|
|
||||||
# handshake client -> server
|
# handshake client -> server
|
||||||
echo "--- CLIENT 1>>> 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
|
# setup server connection using received datagram
|
||||||
let server = setupServer(zeroPath, serverId, datagram)
|
let server = setupServer(zeroPath, serverId, datagram)
|
||||||
defer: ngtcp2_conn_del(server)
|
defer: ngtcp2_conn_del(server)
|
||||||
|
|
||||||
echo "--- CLIENT >>>1 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"
|
echo "--- CLIENT <<<2 SERVER"
|
||||||
var offset = 0
|
datagramLength = server.write(datagram, datagramInfo)
|
||||||
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
|
|
||||||
|
|
||||||
echo "--- CLIENT 2<<< SERVER"
|
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"
|
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
|
check client.ngtcp2_conn_get_handshake_completed().bool
|
||||||
|
|
||||||
echo "--- CLIENT >>>3 SERVER"
|
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
|
check server.ngtcp2_conn_get_handshake_completed().bool
|
||||||
|
|
Loading…
Reference in New Issue