Local interface bitrate simulation

This commit is contained in:
Mark Qvist 2023-10-27 18:12:53 +02:00
parent 38236366cf
commit 0e12442a28
1 changed files with 10 additions and 4 deletions

View File

@ -28,6 +28,7 @@ import time
import sys import sys
import os import os
import RNS import RNS
from threading import Lock
class HDLC(): class HDLC():
FLAG = 0x7E FLAG = 0x7E
@ -150,9 +151,6 @@ class LocalClientInterface(Interface):
def processIncoming(self, data): def processIncoming(self, data):
if self._force_bitrate:
time.sleep(len(data) / self.bitrate * 8)
self.rxb += len(data) self.rxb += len(data)
if hasattr(self, "parent_interface") and self.parent_interface != None: if hasattr(self, "parent_interface") and self.parent_interface != None:
self.parent_interface.rxb += len(data) self.parent_interface.rxb += len(data)
@ -170,8 +168,16 @@ class LocalClientInterface(Interface):
if self.online: if self.online:
try: try:
self.writing = True self.writing = True
if self._force_bitrate: if self._force_bitrate:
time.sleep(len(data) / self.bitrate * 8) if not hasattr(self, "send_lock"):
self.send_lock = Lock()
with self.send_lock:
s = len(data) / self.bitrate * 8
RNS.log(f"Simulating latency of {RNS.prettytime(s)} for {len(data)} bytes")
time.sleep(s)
data = bytes([HDLC.FLAG])+HDLC.escape(data)+bytes([HDLC.FLAG]) data = bytes([HDLC.FLAG])+HDLC.escape(data)+bytes([HDLC.FLAG])
self.socket.sendall(data) self.socket.sendall(data)
self.writing = False self.writing = False