Combine data and explicit congestion notification into Datagram object

This commit is contained in:
Mark Spanbroek 2020-10-29 10:33:15 +01:00 committed by markspanbroek
parent 783a6c0000
commit 74dc38d01c
6 changed files with 33 additions and 30 deletions

View File

@ -1 +1,7 @@
type DatagramBuffer* = openArray[byte]
import congestion
type
Datagram* = object
data*: seq[byte]
ecn*: ECN
DatagramBuffer* = openArray[byte]

View File

@ -1,5 +1,7 @@
import std/monotimes
import ngtcp2
import ../datagram
import ../congestion
import connection
import errors
import path
@ -15,11 +17,12 @@ proc openStream*(connection: Connection): Stream =
proc close*(stream: Stream) =
checkResult ngtcp2_conn_shutdown_stream(stream.connection.conn, stream.id, 0)
proc write*(stream: Stream, message: seq[byte]): seq[byte] =
proc write*(stream: Stream, message: seq[byte]): Datagram =
var packetInfo: ngtcp2_pkt_info
let length = ngtcp2_conn_write_stream(
stream.connection.conn,
stream.connection.path.toPathPtr,
nil,
addr packetInfo,
addr stream.connection.buffer[0],
stream.connection.buffer.len.uint,
nil,
@ -30,4 +33,5 @@ proc write*(stream: Stream, message: seq[byte]): seq[byte] =
getMonoTime().ticks.uint
)
checkResult length.cint
stream.connection.buffer[0..<length]
result.data = stream.connection.buffer[0..<length]
result.ecn = ECN(packetInfo.ecn)

View File

@ -6,7 +6,7 @@ import connection
import path
import errors
proc write*(connection: Connection, ecn: var ECN): seq[byte] =
proc write*(connection: Connection): Datagram =
var packetInfo: ngtcp2_pkt_info
let length = ngtcp2_conn_write_stream(
connection.conn,
@ -21,12 +21,8 @@ proc write*(connection: Connection, ecn: var ECN): seq[byte] =
0,
getMonoTime().ticks.uint
)
ecn = ECN(packetInfo.ecn)
connection.buffer[0..<length]
proc write*(connection: Connection): seq[byte] =
var ecn: ECN
write(connection, ecn)
result.data = connection.buffer[0..<length]
result.ecn = ECN(packetInfo.ecn)
proc read*(connection: Connection, datagram: DatagramBuffer, ecn = ecnNonCapable) =
var packetInfo: ngtcp2_pkt_info
@ -39,3 +35,6 @@ proc read*(connection: Connection, datagram: DatagramBuffer, ecn = ecnNonCapable
datagram.len.uint,
getMonoTime().ticks.uint
)
proc read*(connection: Connection, datagram: Datagram) =
connection.read(datagram.data, datagram.ecn)

View File

@ -3,19 +3,18 @@ import addresses
proc performHandshake*: tuple[client, server: Connection] =
var datagram: seq[byte]
var ecn: ECN
var datagram: Datagram
let client = newClientConnection(zeroAddress, zeroAddress)
datagram = client.write(ecn)
datagram = client.write()
let server = newServerConnection(zeroAddress, zeroAddress, datagram)
server.read(datagram, ecn)
let server = newServerConnection(zeroAddress, zeroAddress, datagram.data)
server.read(datagram)
datagram = server.write(ecn)
client.read(datagram, ecn)
datagram = server.write()
client.read(datagram)
datagram = client.write(ecn)
server.read(datagram, ecn)
datagram = client.write()
server.read(datagram)
(client, server)

View File

@ -25,7 +25,7 @@ suite "streams":
let stream = performHandshake().client.openStream()
let message = @[1'u8, 2'u8, 3'u8]
let datagram = stream.write(message)
check datagram.contains(message)
check datagram.data.contains(message)
test "raises when stream could not be written to":
let stream = performHandshake().client.openStream()

View File

@ -5,20 +5,15 @@ import ../helpers/addresses
suite "udp":
var ecn: ECN
setup:
ecn = ECN.default
test "writes packets to datagrams":
let client = newClientConnection(zeroAddress, zeroAddress)
check client.write(ecn).len > 0
check client.write().data.len > 0
test "reads packets from datagram":
let client = newClientConnection(zeroAddress, zeroAddress)
let datagram = client.write(ecn)
let server = newServerConnection(zeroAddress, zeroAddress, datagram)
server.read(datagram, ecn)
let datagram = client.write()
let server = newServerConnection(zeroAddress, zeroAddress, datagram.data)
server.read(datagram)
test "raises error when reading datagram fails":
let datagram = repeat(0'u8, 4096)