src/eric7/EricNetwork/EricJsonServer.py

branch
eric7
changeset 10524
ed4fd87c4d4c
parent 10439
21c28b0f9e41
child 10697
8a609e4c71b6
equal deleted inserted replaced
10523:e4069ddd7dc7 10524:ed4fd87c4d4c
7 Module implementing the JSON based server base class. 7 Module implementing the JSON based server base class.
8 """ 8 """
9 9
10 import contextlib 10 import contextlib
11 import json 11 import json
12 import struct
13 import zlib
12 14
13 from PyQt6.QtCore import ( 15 from PyQt6.QtCore import (
14 QCoreApplication, 16 QCoreApplication,
15 QEventLoop, 17 QEventLoop,
16 QProcess, 18 QProcess,
153 except KeyError: 155 except KeyError:
154 connection = None 156 connection = None
155 else: 157 else:
156 connection = self.__connection 158 connection = self.__connection
157 159
158 while connection and connection.canReadLine(): 160 while connection and connection.bytesAvailable():
159 data = connection.readLine() 161 header = connection.read(struct.calcsize(b"!II"))
160 jsonLine = bytes(data).decode("utf-8", "backslashreplace") 162 length, datahash = struct.unpack(b"!II", header)
161 163
162 # - print("JSON Server ({0}): {1}".format(self.__name, jsonLine)) 164 data = bytearray()
165 while len(data) < length:
166 maxSize = length - len(data)
167 if connection.bytesAvailable() < maxSize:
168 connection.waitForReadyRead(50)
169 data += connection.read(maxSize)
170
171 if zlib.adler32(data) & 0xFFFFFFFF != datahash:
172 # corrupted data -> discard and continue
173 continue
174
175 jsonString = data.decode("utf-8", "backslashreplace")
176
177 # - print("JSON Server ({0}): {1}".format(self.__name, jsonString))
163 # - this is for debugging only 178 # - this is for debugging only
164 179
165 try: 180 try:
166 clientDict = json.loads(jsonLine.strip()) 181 clientDict = json.loads(jsonString.strip())
167 except (TypeError, ValueError) as err: 182 except (TypeError, ValueError) as err:
168 EricMessageBox.critical( 183 EricMessageBox.critical(
169 None, 184 None,
170 self.tr("JSON Protocol Error"), 185 self.tr("JSON Protocol Error"),
171 self.tr( 186 self.tr(
173 """ could not be decoded. Please report""" 188 """ could not be decoded. Please report"""
174 """ this issue with the received data to the""" 189 """ this issue with the received data to the"""
175 """ eric bugs email address.</p>""" 190 """ eric bugs email address.</p>"""
176 """<p>Error: {0}</p>""" 191 """<p>Error: {0}</p>"""
177 """<p>Data:<br/>{1}</p>""" 192 """<p>Data:<br/>{1}</p>"""
178 ).format(str(err), Utilities.html_encode(jsonLine.strip())), 193 ).format(str(err), Utilities.html_encode(jsonString.strip())),
179 EricMessageBox.Ok, 194 EricMessageBox.Ok,
180 ) 195 )
181 return 196 return
182 197
183 self.handleCall(clientDict["method"], clientDict["params"]) 198 self.handleCall(clientDict["method"], clientDict["params"])
210 else: 225 else:
211 connection = self.__connection 226 connection = self.__connection
212 227
213 if connection is not None: 228 if connection is not None:
214 data = cmd.encode("utf8", "backslashreplace") 229 data = cmd.encode("utf8", "backslashreplace")
215 length = "{0:09d}".format(len(data)) 230 header = struct.pack(b"!II", len(data), zlib.adler32(data) & 0xFFFFFFFF)
216 connection.write(length.encode() + data) 231 connection.write(header)
232 connection.write(data)
217 if flush: 233 if flush:
218 connection.flush() 234 connection.flush()
219 235
220 def startClient( 236 def startClient(
221 self, interpreter, clientScript, clientArgs, idString="", environment=None 237 self, interpreter, clientScript, clientArgs, idString="", environment=None

eric ide

mercurial