Small formatting change + Error simplification

This commit is contained in:
Firepup Sixfifty 2024-07-17 02:22:01 +00:00
parent 60098ddfd4
commit 1714010182
Signed by: Firepup650
SSH key fingerprint: SHA256:U0Zp8EhEe3CMqFSrC79CqatzaEiL4sjta80/RSX2XrY

View file

@ -12,8 +12,8 @@ class Globals: ...
# The two below lines are a hacky fix for python 3.10 asyncio # The two below lines are a hacky fix for python 3.10 asyncio
TimoutError = TimeoutError TimeoutErrors = (TimeoutError, asyncio.exceptions.TimeoutError)
TimeoutError = (TimoutError, asyncio.exceptions.TimeoutError) DisconnectErrors = (ConnectionResetError, BrokenPipeError, IndexError, *TimeoutErrors)
G = Globals() G = Globals()
G.uniqueClients = 0 G.uniqueClients = 0
G.servers = {} G.servers = {}
@ -50,7 +50,7 @@ try:
log("Explicitly erasing cached messages") log("Explicitly erasing cached messages")
G.msgs = [] G.msgs = []
elif arg in ["-?", "-h", "--help"]: elif arg in ["-?", "-h", "--help"]:
filename = filename if filename.startswith("./") else "python3 "+filename filename = filename if filename.startswith("./") else "python3 " + filename
print( print(
f"""{filename} <args> f"""{filename} <args>
All arguments are optional! All arguments are optional!
@ -130,7 +130,7 @@ async def handleClient(reader, writer):
await asyncio.wait_for( await asyncio.wait_for(
reader.read(), 0.01 reader.read(), 0.01
) # Silently consume the excess username data ) # Silently consume the excess username data
except TimeoutError: except TimeoutErrors:
pass pass
if not name: if not name:
writer.write(b"Nice try. Actually set a nick.\n") writer.write(b"Nice try. Actually set a nick.\n")
@ -205,7 +205,7 @@ Please note that this is not network level statistics.\n""".encode(
G.S2SLogs.append(("M", (name, request), G.remoteID)) G.S2SLogs.append(("M", (name, request), G.remoteID))
if response: if response:
G.msgs.append(response) G.msgs.append(response)
except TimeoutError: except TimeoutErrors:
pass pass
if msgIndex < len(G.msgs): if msgIndex < len(G.msgs):
writer.writelines(G.msgs[msgIndex:]) writer.writelines(G.msgs[msgIndex:])
@ -345,7 +345,7 @@ Please note that this is not network level statistics.\n""".encode(
"WARN", "WARN",
) )
await writer.drain() await writer.drain()
except TimeoutError: except TimeoutErrors:
pass pass
if any(G.servers[sName].values()): if any(G.servers[sName].values()):
for name in G.servers[sName]: for name in G.servers[sName]:
@ -381,11 +381,7 @@ Please note that this is not network level statistics.\n""".encode(
del G.clientsConnected[cName.lower()] del G.clientsConnected[cName.lower()]
del G.servers[sName] del G.servers[sName]
G.msgs.append(log(f"{sName} has de-linked from the network")) G.msgs.append(log(f"{sName} has de-linked from the network"))
except ( except DisconnectErrors:
ConnectionResetError,
BrokenPipeError,
IndexError,
): # Don't ask. IndexError needs to be caught here too.
if not name.startswith("S2S-"): if not name.startswith("S2S-"):
G.uniqueClients -= 1 G.uniqueClients -= 1
G.msgs.append(log(f"{name} has disconnected from the server.")) G.msgs.append(log(f"{name} has disconnected from the server."))
@ -424,7 +420,7 @@ async def connectServer(hostname: str, port: int):
rID = raw((await reader.read(16)).decode("utf8")) rID = raw((await reader.read(16)).decode("utf8"))
try: try:
await asyncio.wait_for(reader.read(1024), 0.1) await asyncio.wait_for(reader.read(1024), 0.1)
except TimeoutError: except TimeoutErrors:
pass pass
if G.servers.get(rID, False) != False: if G.servers.get(rID, False) != False:
writer.close() writer.close()
@ -521,7 +517,7 @@ async def connectServer(hostname: str, port: int):
) )
log(f"Recieved invalid message ({buffer}) from {sName}", "WARN") log(f"Recieved invalid message ({buffer}) from {sName}", "WARN")
await writer.drain() await writer.drain()
except TimeoutError: except TimeoutErrors:
pass pass
if any(G.servers[rID].values()): if any(G.servers[rID].values()):
for name in G.servers[rID]: for name in G.servers[rID]:
@ -557,7 +553,7 @@ async def connectServer(hostname: str, port: int):
del G.clientsConnected[cName.lower()] del G.clientsConnected[cName.lower()]
del G.servers[rID] del G.servers[rID]
G.msgs.append(log(f"{rID} has de-linked from the network")) G.msgs.append(log(f"{rID} has de-linked from the network"))
except (ConnectionResetError, BrokenPipeError, IndexError): except DisconnectErrors:
if G.cwlgd: if G.cwlgd:
raise LinksDownException raise LinksDownException
for cName in G.servers[rID]: for cName in G.servers[rID]: