Added signal handler and interface detachment oon exit.

This commit is contained in:
Mark Qvist 2021-09-24 16:09:07 +02:00
parent 81cdb0b7e6
commit 3d4ac0126b
5 changed files with 33 additions and 3 deletions

View File

@ -12,3 +12,6 @@ class Interface:
def get_hash(self): def get_hash(self):
return RNS.Identity.full_hash(str(self).encode("utf-8")) return RNS.Identity.full_hash(str(self).encode("utf-8"))
def detach(self):
pass

View File

@ -96,6 +96,15 @@ class TCPClientInterface(Interface):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, TCP_KEEPIDLE, int(TCPClientInterface.TCP_PROBE_AFTER)) sock.setsockopt(socket.IPPROTO_TCP, TCP_KEEPIDLE, int(TCPClientInterface.TCP_PROBE_AFTER))
def detach(self):
if self.socket != None:
if hasattr(self.socket, "close"):
if callable(self.socket.close):
RNS.log("Detaching "+str(self), RNS.LOG_DEBUG)
self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()
self.socket = None
def connect(self, initial=False): def connect(self, initial=False):
try: try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -276,6 +285,7 @@ class TCPServerInterface(Interface):
self.owner = owner self.owner = owner
address = (self.bind_ip, self.bind_port) address = (self.bind_ip, self.bind_port)
ThreadingTCPServer.allow_reuse_address = True
self.server = ThreadingTCPServer(address, handlerFactory(self.incoming_connection)) self.server = ThreadingTCPServer(address, handlerFactory(self.incoming_connection))
thread = threading.Thread(target=self.server.serve_forever) thread = threading.Thread(target=self.server.serve_forever)

View File

@ -2,6 +2,7 @@ from .Interfaces import *
import configparser import configparser
from .vendor.configobj import ConfigObj from .vendor.configobj import ConfigObj
import RNS import RNS
import signal
import atexit import atexit
import struct import struct
import array import array
@ -78,6 +79,12 @@ class Reticulum:
RNS.Transport.exit_handler() RNS.Transport.exit_handler()
RNS.Identity.exit_handler() RNS.Identity.exit_handler()
@staticmethod
def sigint_handler(signal, frame):
RNS.Transport.detach_interfaces()
RNS.exit()
def __init__(self,configdir=None, loglevel=None): def __init__(self,configdir=None, loglevel=None):
""" """
Initialises and starts a Reticulum instance. This must be Initialises and starts a Reticulum instance. This must be
@ -147,6 +154,7 @@ class Reticulum:
RNS.Transport.start(self) RNS.Transport.start(self)
atexit.register(Reticulum.exit_handler) atexit.register(Reticulum.exit_handler)
signal.signal(signal.SIGINT, Reticulum.sigint_handler)
def __start_local_interface(self): def __start_local_interface(self):
if self.share_instance: if self.share_instance:

View File

@ -1422,6 +1422,12 @@ class Transport:
else: else:
return False return False
@staticmethod
def detach_interfaces():
for interface in Transport.interfaces:
interface.detach()
@staticmethod @staticmethod
def exit_handler(): def exit_handler():
try: try:

View File

@ -109,3 +109,6 @@ def prettyhexrep(data):
def panic(): def panic():
os._exit(255) os._exit(255)
def exit():
sys.exit(0)