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
|
import os, sys, asyncio, re, signal
|
||||||
from platform import uname
|
from platform import uname
|
||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
from logs import log
|
from logs import log
|
||||||
|
|
||||||
|
|
||||||
class LinksDownException(Exception): ...
|
class LinkDownError(Exception): ...
|
||||||
|
|
||||||
|
|
||||||
|
class FailedLinkError(Exception): ...
|
||||||
|
|
||||||
|
|
||||||
class Globals: ...
|
class Globals: ...
|
||||||
|
|
||||||
|
|
||||||
# The two below lines are a hacky fix for python 3.10 asyncio
|
|
||||||
TimeoutErrors = (TimeoutError, asyncio.exceptions.TimeoutError)
|
TimeoutErrors = (TimeoutError, asyncio.exceptions.TimeoutError)
|
||||||
DisconnectErrors = (ConnectionResetError, BrokenPipeError, IndexError, *TimeoutErrors)
|
DisconnectErrors = (ConnectionResetError, BrokenPipeError, IndexError, *TimeoutErrors)
|
||||||
G = Globals()
|
G = Globals()
|
||||||
|
@ -401,6 +403,7 @@ Please note that this is not network level statistics.\n""".encode(
|
||||||
|
|
||||||
async def connectServer(hostname: str, port: int):
|
async def connectServer(hostname: str, port: int):
|
||||||
global G
|
global G
|
||||||
|
try:
|
||||||
reader, writer = await asyncio.open_connection(hostname, port)
|
reader, writer = await asyncio.open_connection(hostname, port)
|
||||||
await reader.read(1024)
|
await reader.read(1024)
|
||||||
writer.write(f"S2S-{G.remoteID}\n".encode("utf8"))
|
writer.write(f"S2S-{G.remoteID}\n".encode("utf8"))
|
||||||
|
@ -474,7 +477,9 @@ async def connectServer(hostname: str, port: int):
|
||||||
case "-":
|
case "-":
|
||||||
cName = buffer[2:]
|
cName = buffer[2:]
|
||||||
if G.clientsConnected.get(cName.lower(), None) == rID:
|
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.servers[rID][cName.lower()]
|
||||||
del G.clientsConnected[cName.lower()]
|
del G.clientsConnected[cName.lower()]
|
||||||
G.S2SLogs.append(("-", cName, rID))
|
G.S2SLogs.append(("-", cName, rID))
|
||||||
|
@ -515,7 +520,10 @@ async def connectServer(hostname: str, port: int):
|
||||||
"utf8"
|
"utf8"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
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 TimeoutErrors:
|
except TimeoutErrors:
|
||||||
pass
|
pass
|
||||||
|
@ -544,7 +552,7 @@ async def connectServer(hostname: str, port: int):
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
msgInd += 1
|
msgInd += 1
|
||||||
if G.cwlgd:
|
if G.cwlgd:
|
||||||
raise LinksDownException
|
raise LinkDownError
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
writer.close()
|
writer.close()
|
||||||
await writer.wait_closed()
|
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"))
|
G.msgs.append(log(f"{rID} has de-linked from the network"))
|
||||||
except DisconnectErrors:
|
except DisconnectErrors:
|
||||||
if G.cwlgd:
|
if G.cwlgd:
|
||||||
raise LinksDownException
|
raise LinkDownError
|
||||||
for cName in G.servers[rID]:
|
for cName in G.servers[rID]:
|
||||||
G.msgs.append(log(f"{cName}'s server is going down"))
|
G.msgs.append(log(f"{cName}'s server is going down"))
|
||||||
try:
|
try:
|
||||||
|
@ -564,6 +572,10 @@ async def connectServer(hostname: str, port: int):
|
||||||
pass
|
pass
|
||||||
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 OSError as E:
|
||||||
|
log("OSError: " + str(E), "ERROR")
|
||||||
|
if G.cwlgd:
|
||||||
|
raise FailedLinkError
|
||||||
|
|
||||||
|
|
||||||
async def runServer(address: str, port: int):
|
async def runServer(address: str, port: int):
|
||||||
|
@ -578,6 +590,11 @@ async def runServer(address: str, port: int):
|
||||||
try:
|
try:
|
||||||
links.append(G.event.wait())
|
links.append(G.event.wait())
|
||||||
await asyncio.gather(*links)
|
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:
|
except Exception:
|
||||||
crash = True
|
crash = True
|
||||||
G.msgs.append(log("Server crash", "FATAL")[1:])
|
G.msgs.append(log("Server crash", "FATAL")[1:])
|
||||||
|
|
Loading…
Reference in a new issue