src/eric7/EricNetwork/EricJsonServer.py

branch
eric7
changeset 10697
8a609e4c71b6
parent 10524
ed4fd87c4d4c
child 10928
46651e194fbe
equal deleted inserted replaced
10696:55e4a7497833 10697:8a609e4c71b6
8 """ 8 """
9 9
10 import contextlib 10 import contextlib
11 import json 11 import json
12 import struct 12 import struct
13 import time
13 import zlib 14 import zlib
14 15
15 from PyQt6.QtCore import ( 16 from PyQt6.QtCore import (
16 QCoreApplication, 17 QCoreApplication,
17 QEventLoop, 18 QEventLoop,
147 Private slot handling received data from the client. 148 Private slot handling received data from the client.
148 149
149 @param idString id of the connection 150 @param idString id of the connection
150 @type str 151 @type str
151 """ 152 """
153 headerSize = struct.calcsize(b"!II")
154
152 if idString: 155 if idString:
153 try: 156 try:
154 connection = self.__connections[idString] 157 connection = self.__connections[idString]
155 except KeyError: 158 except KeyError:
156 connection = None 159 connection = None
157 else: 160 else:
158 connection = self.__connection 161 connection = self.__connection
159 162
160 while connection and connection.bytesAvailable(): 163 while connection and connection.bytesAvailable():
161 header = connection.read(struct.calcsize(b"!II")) 164 now = time.monotonic()
165 while connection.bytesAvailable() < headerSize:
166 connection.waitForReadyRead(50)
167 if time.monotonic() - now > 2.0: # 2 seconds timeout
168 return
169 header = connection.read(headerSize)
162 length, datahash = struct.unpack(b"!II", header) 170 length, datahash = struct.unpack(b"!II", header)
163 171
164 data = bytearray() 172 data = bytearray()
173 now = time.monotonic()
165 while len(data) < length: 174 while len(data) < length:
166 maxSize = length - len(data) 175 maxSize = length - len(data)
167 if connection.bytesAvailable() < maxSize: 176 if connection.bytesAvailable() < maxSize:
168 connection.waitForReadyRead(50) 177 connection.waitForReadyRead(50)
169 data += connection.read(maxSize) 178 newData = connection.read(maxSize)
179 if newData:
180 data += newData
181 else:
182 if time.monotonic() - now > 2.0: # 2 seconds timeout
183 break
170 184
171 if zlib.adler32(data) & 0xFFFFFFFF != datahash: 185 if zlib.adler32(data) & 0xFFFFFFFF != datahash:
172 # corrupted data -> discard and continue 186 # corrupted data -> discard and continue
173 continue 187 continue
174 188

eric ide

mercurial