shittyserver: squish a couple bugs

This commit is contained in:
Marks Polakovs 2020-04-11 15:59:54 +02:00
parent 150596920f
commit bcef6fe319

View file

@ -146,6 +146,7 @@ class Session(object):
connection_state: Optional[str] connection_state: Optional[str]
pc: Optional[Any] pc: Optional[Any]
connection_id: str connection_id: str
lock: asyncio.Lock
def __init__(self) -> None: def __init__(self) -> None:
self.websocket = None self.websocket = None
@ -154,6 +155,7 @@ class Session(object):
self.connection_state = None self.connection_state = None
self.connection_id = str(uuid.uuid4()) self.connection_id = str(uuid.uuid4())
self.ended = False self.ended = False
self.lock = asyncio.Lock()
def to_dict(self) -> Dict[str, str]: def to_dict(self) -> Dict[str, str]:
return {"connection_id": self.connection_id} return {"connection_id": self.connection_id}
@ -169,6 +171,7 @@ class Session(object):
async def end(self) -> None: async def end(self) -> None:
global active_sessions global active_sessions
async with self.lock:
if self.ended: if self.ended:
print(self.connection_id, "already over") print(self.connection_id, "already over")
else: else:
@ -193,6 +196,9 @@ class Session(object):
else: else:
print(self.connection_id, "wasn't in active_sessions!") print(self.connection_id, "wasn't in active_sessions!")
if live_session == self:
live_session = None
await notify_mattserver_about_sessions() await notify_mattserver_about_sessions()
print(self.connection_id, "bye bye") print(self.connection_id, "bye bye")
self.ended = True self.ended = True
@ -333,10 +339,10 @@ async def telnet_server(
print(parts) print(parts)
if parts[0] == "Q": if parts[0] == "Q":
data: Dict[str, Dict[str, str]] = {} result: Dict[str, Dict[str, str]] = {}
for sid, sess in active_sessions.items(): for sid, sess in active_sessions.items():
data[sid] = sess.to_dict() result[sid] = sess.to_dict()
writer.write((json.dumps(data) + "\r\n").encode("utf-8")) writer.write((json.dumps(result) + "\r\n").encode("utf-8"))
elif parts[0] == "SEL": elif parts[0] == "SEL":
sid = parts[1] sid = parts[1]