Minor adjustments in utp_discv5_protocol (#459)

- Move SocketConfig parameter location
- Reuse rng from disc5 protocol
- add exports
- Some whitespace clean-up
This commit is contained in:
Kim De Mey 2022-01-10 13:49:36 +01:00 committed by GitHub
parent 0f18272315
commit 26ab9b078e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 24 deletions

View File

@ -13,7 +13,7 @@ import
./utp_router,
../keys
export utp_router
export utp_router, protocol
type UtpDiscv5Protocol* = ref object of TalkProtocol
prot: protocol.Protocol
@ -26,8 +26,8 @@ proc hash(x: UtpSocketKey[Node]): Hash =
!$h
func `$`*(x: UtpSocketKey[Node]): string =
"(remoteId: " & $x.remoteAddress.id &
", remoteAddress: " & $x.remoteAddress.address &
"(remoteId: " & $x.remoteAddress.id &
", remoteAddress: " & $x.remoteAddress.address &
", rcvId: "& $x.rcvId &
")"
@ -66,16 +66,15 @@ proc new*(
p: protocol.Protocol,
subProtocolName: seq[byte],
acceptConnectionCb: AcceptConnectionCallback[Node],
socketConfig: SocketConfig = SocketConfig.init(),
allowConnectionCb: AllowConnectionCallback[Node] = nil,
rng = newRng()): UtpDiscv5Protocol =
socketConfig: SocketConfig = SocketConfig.init()): UtpDiscv5Protocol =
doAssert(not(isNil(acceptConnectionCb)))
let router = UtpRouter[Node].new(
acceptConnectionCb,
allowConnectionCb,
socketConfig,
rng
p.rng
)
router.sendCb = initSendCallback(p, subProtocolName)

View File

@ -113,8 +113,8 @@ proc new*[A](
rng = newRng()): UtpRouter[A] =
UtpRouter[A].new(acceptConnectionCb, nil, socketConfig, rng)
# There are different possiblites how connection was established, and we need to
# check every case
# There are different possibilities on how the connection got established, need
# to check every case.
proc getSocketOnReset[A](r: UtpRouter[A], sender: A, id: uint16): Option[UtpSocket[A]] =
# id is our recv id
let recvKey = UtpSocketKey[A].init(sender, id)

View File

@ -907,7 +907,7 @@ proc calculateSelectiveAckBytes*(socket: UtpSocket, receivedPackedAckNr: uint16
var ackedBytes = 0'u32
var bits = (len(ext.acks)) * 8 - 1
while bits >= 0:
let v = base + uint16(bits)
@ -920,12 +920,12 @@ proc calculateSelectiveAckBytes*(socket: UtpSocket, receivedPackedAckNr: uint16
if (maybePacket.isNone() or maybePacket.unsafeGet().transmissions == 0):
dec bits
continue
let pkt = maybePacket.unsafeGet()
if (getBit(ext.acks, bits)):
ackedBytes = ackedBytes + pkt.payloadLength
dec bits
return ackedBytes
@ -940,7 +940,7 @@ proc selectiveAckPackets(socket: UtpSocket, receivedPackedAckNr: uint16, ext: S
return
var bits = (len(ext.acks)) * 8 - 1
while bits >= 0:
let v = base + uint16(bits)
@ -953,12 +953,12 @@ proc selectiveAckPackets(socket: UtpSocket, receivedPackedAckNr: uint16, ext: S
if (maybePacket.isNone() or maybePacket.unsafeGet().transmissions == 0):
dec bits
continue
let pkt = maybePacket.unsafeGet()
if (getBit(ext.acks, bits)):
discard socket.ackPacket(v, currentTime)
dec bits
# TODO Add handling of fast timeouts and duplicate acks counting
@ -970,7 +970,7 @@ proc selectiveAckPackets(socket: UtpSocket, receivedPackedAckNr: uint16, ext: S
# The bitmask has reverse byte order. The first byte represents packets [ack_nr + 2, ack_nr + 2 + 7] in reverse order
# The least significant bit in the byte represents ack_nr + 2, the most significant bit in the byte represents ack_nr + 2 + 7
# The next byte in the mask represents [ack_nr + 2 + 8, ack_nr + 2 + 15] in reverse order, and so on
proc generateSelectiveAckBitMask*(socket: UtpSocket): array[4, byte] =
proc generateSelectiveAckBitMask*(socket: UtpSocket): array[4, byte] =
let window = min(32, socket.inBuffer.len())
var arr: array[4, uint8] = [0'u8, 0, 0, 0]
var i = 0
@ -981,7 +981,7 @@ proc generateSelectiveAckBitMask*(socket: UtpSocket): array[4, byte] =
return arr
# Generates ack packet based on current state of the socket.
proc generateAckPacket*(socket: UtpSocket): Packet =
proc generateAckPacket*(socket: UtpSocket): Packet =
let bitmask =
if (socket.reorderCount != 0 and (not socket.reachedFin)):
some(socket.generateSelectiveAckBitMask())
@ -1017,16 +1017,16 @@ proc startIncomingSocket*(socket: UtpSocket) {.async.} =
# running
proc processPacket*(socket: UtpSocket, p: Packet) {.async.} =
debug "Process packet",
debug "Process packet",
socketKey = socket.socketKey,
packetType = p.header.pType,
packetType = p.header.pType,
seqNr = p.header.seqNr,
ackNr = p.header.ackNr,
timestamp = p.header.timestamp,
timestampDiff = p.header.timestampDiff
let timestampInfo = getMonoTimestamp()
if socket.isAckNrInvalid(p):
debug "Received packet with invalid ack number",
ackNr = p.header.ackNr,
@ -1178,7 +1178,7 @@ proc processPacket*(socket: UtpSocket, p: Packet) {.async.} =
debug "Received FIN packet",
eofPktNr = pkSeqNr,
curAckNr = socket.ackNr
socket.gotFin = true
socket.eofPktNr = pkSeqNr

View File

@ -145,14 +145,14 @@ procSuite "Utp protocol over discovery v5 tests":
node1,
utpProtId,
registerIncomingSocketCallback(queue),
SocketConfig.init(lowSynTimeout))
socketConfig = SocketConfig.init(lowSynTimeout))
utp2 =
UtpDiscv5Protocol.new(
node2,
utpProtId,
registerIncomingSocketCallback(queue),
SocketConfig.init(),
allowOneIdCallback(allowedId))
allowOneIdCallback(allowedId),
SocketConfig.init())
# nodes must know about each other
check:
@ -191,7 +191,7 @@ procSuite "Utp protocol over discovery v5 tests":
node2,
utpProtId,
registerIncomingSocketCallback(queue),
SocketConfig.init(incomingSocketReceiveTimeout = none[Duration]())
socketConfig = SocketConfig.init(incomingSocketReceiveTimeout = none[Duration]())
)
# nodes must know about each other