Implemented sequence tracking.

This commit is contained in:
Jakob Friedl
2025-07-26 18:20:54 +02:00
parent a6039172b2
commit 882579b3cb
10 changed files with 86 additions and 50 deletions

28
src/common/sequence.nim Normal file
View File

@@ -0,0 +1,28 @@
import tables
import ./[types, utils]
var sequenceTable {.global.}: Table[uint32, uint64]
proc nextSequence*(agentId: uint32): uint64 =
sequenceTable[agentId] = sequenceTable.getOrDefault(agentId, 0'u64) + 1
return sequenceTable[agentId]
proc validateSequence*(agentId: uint32, seqNr: uint64, packetType: uint8): bool =
let lastSeqNr = sequenceTable.getOrDefault(agentId, 0'u64)
# Heartbeat messages are not used for sequence tracking
if cast[PacketType](packetType) == MSG_HEARTBEAT:
return true
# In order to keep agents running after server restart, accept all connection with seqNr = 1, to update the table
if seqNr == 1'u64:
sequenceTable[agentId] = seqNr
return true
# Validate that the sequence number of the current packet is higher than the currently stored one
if seqNr <= lastSeqNr:
return false
# Update sequence number
sequenceTable[agentId] = seqNr
return true

View File

@@ -74,7 +74,6 @@ type
Task* = object
header*: Header
taskId*: uint32 # [4 bytes ] task id
listenerId*: uint32 # [4 bytes ] listener id
timestamp*: uint32 # [4 bytes ] unix timestamp
@@ -84,7 +83,6 @@ type
TaskResult* = object
header*: Header
taskId*: uint32 # [4 bytes ] task id
listenerId*: uint32 # [4 bytes ] listener id
timestamp*: uint32 # [4 bytes ] unix timestamp
@@ -103,6 +101,7 @@ type
# Registration binary structure
type
# All variable length fields are stored as seq[byte], prefixed with 4 bytes indicating the length of the following data
AgentMetadata* = object
listenerId*: uint32
@@ -151,7 +150,7 @@ type
port*: int
protocol*: Protocol
# Server structure
# Server context structure
type
KeyPair* = object
privateKey*: Key
@@ -165,7 +164,7 @@ type
interactAgent*: Agent
keyPair*: KeyPair
# Agent Config
# Agent config
type
AgentConfig* = ref object
agentId*: string