src/eric7/EricNetwork/EricJsonServer.py

branch
eric7
changeset 10524
ed4fd87c4d4c
parent 10439
21c28b0f9e41
child 10697
8a609e4c71b6
--- a/src/eric7/EricNetwork/EricJsonServer.py	Wed Jan 24 18:52:50 2024 +0100
+++ b/src/eric7/EricNetwork/EricJsonServer.py	Thu Jan 25 14:13:36 2024 +0100
@@ -9,6 +9,8 @@
 
 import contextlib
 import json
+import struct
+import zlib
 
 from PyQt6.QtCore import (
     QCoreApplication,
@@ -155,15 +157,28 @@
         else:
             connection = self.__connection
 
-        while connection and connection.canReadLine():
-            data = connection.readLine()
-            jsonLine = bytes(data).decode("utf-8", "backslashreplace")
+        while connection and connection.bytesAvailable():
+            header = connection.read(struct.calcsize(b"!II"))
+            length, datahash = struct.unpack(b"!II", header)
 
-            # - print("JSON Server ({0}): {1}".format(self.__name, jsonLine))
+            data = bytearray()
+            while len(data) < length:
+                maxSize = length - len(data)
+                if connection.bytesAvailable() < maxSize:
+                    connection.waitForReadyRead(50)
+                data += connection.read(maxSize)
+
+            if zlib.adler32(data) & 0xFFFFFFFF != datahash:
+                # corrupted data -> discard and continue
+                continue
+
+            jsonString = data.decode("utf-8", "backslashreplace")
+
+            # - print("JSON Server ({0}): {1}".format(self.__name, jsonString))
             # - this is for debugging only
 
             try:
-                clientDict = json.loads(jsonLine.strip())
+                clientDict = json.loads(jsonString.strip())
             except (TypeError, ValueError) as err:
                 EricMessageBox.critical(
                     None,
@@ -175,7 +190,7 @@
                         """ eric bugs email address.</p>"""
                         """<p>Error: {0}</p>"""
                         """<p>Data:<br/>{1}</p>"""
-                    ).format(str(err), Utilities.html_encode(jsonLine.strip())),
+                    ).format(str(err), Utilities.html_encode(jsonString.strip())),
                     EricMessageBox.Ok,
                 )
                 return
@@ -212,8 +227,9 @@
 
         if connection is not None:
             data = cmd.encode("utf8", "backslashreplace")
-            length = "{0:09d}".format(len(data))
-            connection.write(length.encode() + data)
+            header = struct.pack(b"!II", len(data), zlib.adler32(data) & 0xFFFFFFFF)
+            connection.write(header)
+            connection.write(data)
             if flush:
                 connection.flush()
 

eric ide

mercurial