add random loss

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2023-05-13 11:37:10 +02:00
parent 527db66a02
commit e934dd7b96
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E

View File

@ -7,7 +7,7 @@
# ChronoSim: simulation/emulation wrapper around Chronos
import
std/[tables, deques],
std/[tables, deques, random],
chronos,
chronicles
@ -54,6 +54,8 @@ when(emulateDatagram): #enable network emulator
proc getLatency(src: TransportAddress, dst: TransportAddress) : Duration =
50.milliseconds
proc getLoss(src: TransportAddress, dst: TransportAddress) : float =
0.0
proc getLineTime(transp: DatagramTransport, msg: seq[byte]) : Duration =
# let bandwith = transp.bandwidth
let bandwidth = 100 # Bytes/ms = KB/sec
@ -65,6 +67,9 @@ when(emulateDatagram): #enable network emulator
#transp.egress.addLast(remote, msg)
#await sleepAsync(getLineTime(transp, msg))
if rand(1.0) < getLoss(transp.local, remote):
return
await sleepAsync(getLatency(transp.local, remote))
{.gcsafe.}:
network[remote.port].recvFrom(transp.local, msg)