Skip to content

Commit

Permalink
Now, the server thread sleeps too. Code optimizations. Increased call…
Browse files Browse the repository at this point in the history
…s to evt.set() to avoid false freeze detection
  • Loading branch information
jmdaweb committed Sep 13, 2018
1 parent a8d6b42 commit bc24a94
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def __init__(self):
self.clients={}
self.client_sockets=[]
self.running=False
self.evt=Event()

def add_client(self, client):
self.clients[client.id] = client
Expand Down Expand Up @@ -165,7 +166,6 @@ def __init__(self):
self.bind_host=options.interface
self.bind_host6=options.interface6
self.channels={}
self.checkEvent=Event()
printDebugMessage("Initialized instance variables", 2)

def createServerSocket(self, port, port6, bind_host, bind_host6):
Expand Down Expand Up @@ -208,8 +208,9 @@ def run(self):
printDebugMessage("The server is running with pid "+str(os.getpid()), 0)
try:
while self.running:
self.checkEvent.set()
self.evt.set()
try:
sleep(0.01)
if socket.has_ipv6:
if self.server_socket is not None:
r, w, e = select.select(self.client_sockets+[self.server_socket, self.server_socket6], self.client_sockets, self.client_sockets, 60)
Expand All @@ -227,10 +228,12 @@ def run(self):
if id!=0:
printDebugMessage("The client "+str(id)+" has connection problems. Disconnecting...", 1)
self.clients[id].close()
self.evt.set()
for sock in w:
id=self.searchId(sock)
if id!=0:
self.clients[id].confirmSend()
self.evt.set()
for sock in r:
if sock is self.server_socket:
self.accept_new_connection(sock)
Expand Down Expand Up @@ -285,7 +288,7 @@ def accept_new_connection(self, srv_sock):

def close(self):
self.running = False
self.checkEvent.set()
self.evt.set()
printDebugMessage("Closing channels...", 2)
for c in list(self.channels.values()):
c.running=False
Expand Down Expand Up @@ -314,7 +317,6 @@ def __init__(self, server, password):
self.server=server
self.password=password
printDebugMessage("Created new channel with password "+password, 3)
self.evt=Event()
self.evt.set()
self.checkThread=CheckThread(self)

Expand All @@ -332,10 +334,12 @@ def run(self):
if id!=0:
printDebugMessage("The client "+str(id)+" has connection problems. Disconnecting...", 0)
self.clients[id].close()
self.evt.set()
for sock in w:
id=self.searchId(sock)
if id!=0:
self.clients[id].confirmSend()
self.evt.set()
for sock in r:
id=self.searchId(sock)
if id!=0:
Expand Down Expand Up @@ -368,7 +372,7 @@ def run(self):
self.running=True
while self.running:
try:
time.sleep(1)
sleep(1)
except:
pass
self.channel.evt.wait(self.timeout)
Expand Down Expand Up @@ -409,6 +413,7 @@ def handle_data(self):
printError()
self.close()
return
self.server.evt.set()
if sock_data == '': #Disconnect
printDebugMessage("Received empty buffer from client "+str(self.id)+", disconnecting", 1)
self.close()
Expand Down Expand Up @@ -570,15 +575,15 @@ def startAndWait():
serverThread=Server()
serverThread.start()
try:
time.sleep(10)
sleep(10)
except:
pass
while serverThread.running: # Wait actively to catch system signals
try:
time.sleep(1)
serverThread.checkEvent.wait(80)
if serverThread.checkEvent.isSet(): # clear and continue
serverThread.checkEvent.clear()
sleep(1)
serverThread.evt.wait(80)
if serverThread.evt.isSet(): # clear and continue
serverThread.evt.clear()
else:
if serverThread.running: # The server is frozen
printDebugMessage("The server thread seems frozen, stopping the daemon.", 0)
Expand Down

0 comments on commit bc24a94

Please sign in to comment.