Skip to content

Commit

Permalink
Backend fixes for sockets staying open
Browse files Browse the repository at this point in the history
  • Loading branch information
Nasty07 committed Aug 8, 2022
1 parent a515c5f commit e112707
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
8 changes: 2 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@
]:
game.player.jump()
elif event.key == pygame.K_ESCAPE and not game.pause_menu:
game.showing_gui = True
game.showing_title = True
game.pause_menu = False
if game.crashing:
src.game.game_crash.stop()
game.reset_everything()
game.client.stop()
game.gui.add(src.game.gui.Button((52, 90), "Online", game.start))
game.gui.add(
src.game.gui.Button((107, 90), "Private", game.start_private)
Expand All @@ -122,9 +121,6 @@
game.gui.add(
src.game.gui.EmojiButton((12, 10), "👥", game.join_with_code)
)
game.pin_code = None
game.private = False
game.client.stop()
break
elif event.key == pygame.K_f and not game.crashing:
game.showing_gui = True
Expand Down
13 changes: 12 additions & 1 deletion src/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,21 @@ def __init__(self):
self.ending_man = EndingIncrementManager(self)
self.tile_timer = TimedTileToggler(self)
self.sound = True
self.pin_code = None
self.pin_code = ""
self.private = False
mixer.play(-1)

def reset_everything(self):
"""Reset everything!"""
self.showing_gui = True
self.showing_title = True
self.pause_menu = False
self.pin_code = ""
self.private = False
self.other_players.empty()
self.tiles = pygame.sprite.LayeredUpdates()
self.objects = pygame.sprite.LayeredUpdates(self.player)

def quit(self):
"""Quit button event"""
if self.client.running:
Expand Down
30 changes: 10 additions & 20 deletions src/server/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _resource_path(file: str):
manager = ConnectionManager()
db = GameDatabase()
anticheat = GameAntiCheat()
players = set()


async def new_game(player, private: bool = False):
Expand Down Expand Up @@ -83,16 +84,16 @@ async def join_with_code(player, code):
await new_game(player)


async def ping_pong(websocket):
async def ping_pong(ws):
"""Trying to keep broadcast alive."""
while websocket in manager.active_broadcasts:
while ws is not None:
t0 = time.perf_counter()
pong_waiter = await websocket.ping()
pong_waiter = await ws.ping()
await pong_waiter
t1 = time.perf_counter()
latency = f"{t1-t0:.2f}"
message = json.dumps({"type": "ping", "latency": latency})
await websocket.send(message)
await ws.send(message)
await asyncio.sleep(0.5)


Expand Down Expand Up @@ -163,9 +164,6 @@ async def close_main(websocket, player):
await manager.drop_main(websocket)


players = set()


async def close_broadcast(websocket, request_id):
"""Close broadcast websocket properly."""
for play in players.copy():
Expand Down Expand Up @@ -224,16 +222,9 @@ async def handler(websocket):
await manager.add_broadcast(websocket)
for pl in players.copy():
if pl.unique_id == event["unique_id"]:
while True:
if isinstance(
websocket, websockets.legacy.server.WebSocketServerProtocol
):
logging.info("Attached websocket")
pl.attach_broadcast(websocket)
await ping_pong(websocket)
break
else:
await asyncio.sleep(0.2)
logging.info("Attached websocket")
pl.broadcast = websocket
await ping_pong(pl.broadcast)

except websockets.exceptions.ConnectionClosedError:
logging.info("Websocket closed with ConnectionClosedError")
Expand All @@ -243,10 +234,9 @@ async def handler(websocket):

finally:
# Drop websocket after figuring out its type
if websocket in manager.active_connections and player:
if websocket and player:
await close_main(websocket, player)
elif websocket in manager.active_broadcasts:
await close_broadcast(websocket, event["unique_id"])
await close_broadcast(player.broadcast, player.unique_id)


async def main(PORT):
Expand Down
4 changes: 0 additions & 4 deletions src/server/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def data(self):
"direction": self.direction,
}

def attach_broadcast(self, websocket):
"""Adds second websocket in Player"""
self.broadcast = websocket


class GameInstance:
"""Logical Game Instance"""
Expand Down

0 comments on commit e112707

Please sign in to comment.