From ce7aaf5c8c964e323743192922f9ca937873085a Mon Sep 17 00:00:00 2001
From: Fabian Dill <fabian.dill@web.de>
Date: Tue, 1 Dec 2020 21:57:18 +0100
Subject: [PATCH] warn when data could not be sent to snes

---
 MultiClient.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/MultiClient.py b/MultiClient.py
index 3c98708c..b0d7f84c 100644
--- a/MultiClient.py
+++ b/MultiClient.py
@@ -638,13 +638,13 @@ async def snes_write(ctx : Context, write_list):
         PutAddress_Request = {"Opcode": "PutAddress", "Operands": [], 'Space': 'SNES'}
 
         try:
-            #will pack those requests as soon as qusb2snes actually supports that for real
             for address, data in write_list:
                 PutAddress_Request['Operands'] = [hex(address)[2:], hex(len(data))[2:]]
                 if ctx.snes_socket is not None:
                     await ctx.snes_socket.send(json.dumps(PutAddress_Request))
-                if ctx.snes_socket is not None:
                     await ctx.snes_socket.send(data)
+                else:
+                    logging.warning(f"Could not send data to SNES: {data}")
         except websockets.ConnectionClosed:
             logging.warning("Could not write data to SNES")
             return False
@@ -655,7 +655,8 @@ async def snes_write(ctx : Context, write_list):
 
 
 def snes_buffered_write(ctx : Context, address, data):
-    if len(ctx.snes_write_buffer) > 0 and (ctx.snes_write_buffer[-1][0] + len(ctx.snes_write_buffer[-1][1])) == address:
+    if ctx.snes_write_buffer and (ctx.snes_write_buffer[-1][0] + len(ctx.snes_write_buffer[-1][1])) == address:
+        # append to existing write command, bundling them
         ctx.snes_write_buffer[-1] = (ctx.snes_write_buffer[-1][0], ctx.snes_write_buffer[-1][1] + data)
     else:
         ctx.snes_write_buffer.append((address, data))
@@ -665,8 +666,9 @@ async def snes_flush_writes(ctx : Context):
     if not ctx.snes_write_buffer:
         return
 
-    await snes_write(ctx, ctx.snes_write_buffer)
-    ctx.snes_write_buffer = []
+    # swap buffers
+    ctx.snes_write_buffer, writes = [], ctx.snes_write_buffer
+    await snes_write(ctx, writes)
 
 
 async def send_msgs(websocket, msgs):