forked from Firepup650/python-talk
Small formatting change + Error simplification
This commit is contained in:
parent
60098ddfd4
commit
1714010182
1 changed files with 10 additions and 14 deletions
22
server.py
22
server.py
|
@ -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 = {}
|
||||||
|
@ -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]:
|
||||||
|
|
Loading…
Reference in a new issue