forked from Firepup650/python-talk
Clarify intentional outages from server linking related things
This commit is contained in:
parent
1714010182
commit
7c0c4b0875
1 changed files with 173 additions and 156 deletions
31
server.py
31
server.py
|
@ -1,17 +1,19 @@
|
|||
#! /usr/bin/python3
|
||||
#!/usr/bin/python3
|
||||
import os, sys, asyncio, re, signal
|
||||
from platform import uname
|
||||
from traceback import format_exc
|
||||
from logs import log
|
||||
|
||||
|
||||
class LinksDownException(Exception): ...
|
||||
class LinkDownError(Exception): ...
|
||||
|
||||
|
||||
class FailedLinkError(Exception): ...
|
||||
|
||||
|
||||
class Globals: ...
|
||||
|
||||
|
||||
# The two below lines are a hacky fix for python 3.10 asyncio
|
||||
TimeoutErrors = (TimeoutError, asyncio.exceptions.TimeoutError)
|
||||
DisconnectErrors = (ConnectionResetError, BrokenPipeError, IndexError, *TimeoutErrors)
|
||||
G = Globals()
|
||||
|
@ -401,6 +403,7 @@ Please note that this is not network level statistics.\n""".encode(
|
|||
|
||||
async def connectServer(hostname: str, port: int):
|
||||
global G
|
||||
try:
|
||||
reader, writer = await asyncio.open_connection(hostname, port)
|
||||
await reader.read(1024)
|
||||
writer.write(f"S2S-{G.remoteID}\n".encode("utf8"))
|
||||
|
@ -474,7 +477,9 @@ async def connectServer(hostname: str, port: int):
|
|||
case "-":
|
||||
cName = buffer[2:]
|
||||
if G.clientsConnected.get(cName.lower(), None) == rID:
|
||||
G.msgs.append(log(f"{cName} has disconnected from {rID}"))
|
||||
G.msgs.append(
|
||||
log(f"{cName} has disconnected from {rID}")
|
||||
)
|
||||
del G.servers[rID][cName.lower()]
|
||||
del G.clientsConnected[cName.lower()]
|
||||
G.S2SLogs.append(("-", cName, rID))
|
||||
|
@ -515,7 +520,10 @@ async def connectServer(hostname: str, port: int):
|
|||
"utf8"
|
||||
)
|
||||
)
|
||||
log(f"Recieved invalid message ({buffer}) from {sName}", "WARN")
|
||||
log(
|
||||
f"Recieved invalid message ({buffer}) from {sName}",
|
||||
"WARN",
|
||||
)
|
||||
await writer.drain()
|
||||
except TimeoutErrors:
|
||||
pass
|
||||
|
@ -544,7 +552,7 @@ async def connectServer(hostname: str, port: int):
|
|||
await writer.drain()
|
||||
msgInd += 1
|
||||
if G.cwlgd:
|
||||
raise LinksDownException
|
||||
raise LinkDownError
|
||||
await writer.drain()
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
|
@ -555,7 +563,7 @@ async def connectServer(hostname: str, port: int):
|
|||
G.msgs.append(log(f"{rID} has de-linked from the network"))
|
||||
except DisconnectErrors:
|
||||
if G.cwlgd:
|
||||
raise LinksDownException
|
||||
raise LinkDownError
|
||||
for cName in G.servers[rID]:
|
||||
G.msgs.append(log(f"{cName}'s server is going down"))
|
||||
try:
|
||||
|
@ -564,6 +572,10 @@ async def connectServer(hostname: str, port: int):
|
|||
pass
|
||||
del G.servers[rID]
|
||||
G.msgs.append(log(f"{rID} has de-linked from the network"))
|
||||
except OSError as E:
|
||||
log("OSError: " + str(E), "ERROR")
|
||||
if G.cwlgd:
|
||||
raise FailedLinkError
|
||||
|
||||
|
||||
async def runServer(address: str, port: int):
|
||||
|
@ -578,6 +590,11 @@ async def runServer(address: str, port: int):
|
|||
try:
|
||||
links.append(G.event.wait())
|
||||
await asyncio.gather(*links)
|
||||
except LinkDownError:
|
||||
G.msgs.append(log("Lost a server link, going down", "FATAL")[1:])
|
||||
crash = True
|
||||
except FailedLinkError:
|
||||
G.msgs.append(log("Failed to establish a server link, going down", "FATAL")[1:])
|
||||
except Exception:
|
||||
crash = True
|
||||
G.msgs.append(log("Server crash", "FATAL")[1:])
|
||||
|
|
Loading…
Reference in a new issue