Implemented sequence tracking.
This commit is contained in:
28
src/common/sequence.nim
Normal file
28
src/common/sequence.nim
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user