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

@ -11,4 +11,7 @@ class Interface:
pass
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.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):
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -276,6 +285,7 @@ class TCPServerInterface(Interface):
self.owner = owner
address = (self.bind_ip, self.bind_port)
ThreadingTCPServer.allow_reuse_address = True
self.server = ThreadingTCPServer(address, handlerFactory(self.incoming_connection))
thread = threading.Thread(target=self.server.serve_forever)

View File

@ -2,6 +2,7 @@ from .Interfaces import *
import configparser
from .vendor.configobj import ConfigObj
import RNS
import signal
import atexit
import struct
import array
@ -78,6 +79,12 @@ class Reticulum:
RNS.Transport.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):
"""
Initialises and starts a Reticulum instance. This must be
@ -109,7 +116,7 @@ class Reticulum:
self.requested_loglevel = RNS.LOG_EXTREME
if self.requested_loglevel < RNS.LOG_CRITICAL:
self.requested_loglevel = RNS.LOG_CRITICAL
RNS.loglevel = self.requested_loglevel
self.is_shared_instance = False
@ -147,6 +154,7 @@ class Reticulum:
RNS.Transport.start(self)
atexit.register(Reticulum.exit_handler)
signal.signal(signal.SIGINT, Reticulum.sigint_handler)
def __start_local_interface(self):
if self.share_instance:

View File

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

View File

@ -108,4 +108,7 @@ def prettyhexrep(data):
return hexrep
def panic():
os._exit(255)
os._exit(255)
def exit():
sys.exit(0)