DbgMuxConnHandler: implement DbgMuxConnUdpBridge

fixeria/threads
Vadim Yanitskiy 5 months ago
parent af18b58e1f
commit c58e7e3f07
  1. 41
      handlers.py

@ -18,6 +18,7 @@
import logging as log
import threading
import socket
import queue
import enum
import abc
@ -120,3 +121,43 @@ class DbgMuxConnHandler(DbgMuxFrameHandler):
@abc.abstractmethod
def _handle_terminate(self) -> None:
''' Called on connection termination '''
class DbgMuxConnUdpBridge(DbgMuxConnHandler):
DGRAM_MAX_LEN: int = 1024
def __init__(self, *args, **kw):
super().__init__(*args)
self.raddr: str = kw.get('raddr', '127.0.0.1')
self.rport: int = kw.get('rport', 9999)
self.laddr: str = kw.get('laddr', '127.0.0.1')
self.lport: int = kw.get('lport', 8888)
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._sock.bind((self.laddr, self.lport))
self._sock.settimeout(0.5)
self._thread = threading.Thread(target=self._worker,
daemon=True)
self._shutdown = threading.Event()
def _worker(self) -> None:
while not self._shutdown.is_set():
try:
(data, addr) = self._sock.recvfrom(self.DGRAM_MAX_LEN)
if self.conn_state == ConnState.Established:
self.send_data(data)
except TimeoutError:
pass
def _handle_data(self, data: bytes) -> None:
self._sock.sendto(data, (self.raddr, self.rport))
def _handle_establish(self) -> None:
self._shutdown.clear()
self._thread.start()
def _handle_terminate(self) -> None:
self._shutdown.set()

Loading…
Cancel
Save