eric7/DebugClients/Python/AsyncFile.py

branch
eric7
changeset 8954
c8b027c654bc
parent 8881
54e42bc2437a
equal deleted inserted replaced
8953:3e8adcb59aad 8954:c8b027c654bc
34 34
35 class AsyncFile: 35 class AsyncFile:
36 """ 36 """
37 Class wrapping a socket object with a file interface. 37 Class wrapping a socket object with a file interface.
38 """ 38 """
39 maxtries = 10 39 MAX_TRIES = 10
40
41 BUFSIZE = 2 ** 14 # 16 kBytes
42 CMD_BUFSIZE = 2 ** 12 # 4 kBytes
40 43
41 def __init__(self, sock, mode, name): 44 def __init__(self, sock, mode, name):
42 """ 45 """
43 Constructor 46 Constructor
44 47
115 buf = buf.encode('utf-8', 'backslashreplace') 118 buf = buf.encode('utf-8', 'backslashreplace')
116 self.sock.sendall(buf) 119 self.sock.sendall(buf)
117 self.nWriteErrors = 0 120 self.nWriteErrors = 0
118 except OSError: 121 except OSError:
119 self.nWriteErrors += 1 122 self.nWriteErrors += 1
120 if self.nWriteErrors > self.maxtries: 123 if self.nWriteErrors > AsyncFile.MAX_TRIES:
121 self.wpending = [] # delete all output 124 self.wpending = [] # delete all output
122 self.writeLock.release() 125 self.writeLock.release()
123 126
124 def isatty(self): 127 def isatty(self):
125 """ 128 """
161 @rtype str 164 @rtype str
162 """ 165 """
163 self.__checkMode('r') 166 self.__checkMode('r')
164 167
165 if size < 0: 168 if size < 0:
166 size = 20000 169 size = AsyncFile.BUFSIZE
167 170
168 return self.sock.recv(size).decode('utf8', 'backslashreplace') 171 return self.sock.recv(size).decode('utf8', 'backslashreplace')
169 172
170 def read(self, size=-1): 173 def read(self, size=-1):
171 """ 174 """
193 # The command string is prefixed by a 9 character long length field. 196 # The command string is prefixed by a 9 character long length field.
194 length = self.sock.recv(9) 197 length = self.sock.recv(9)
195 length = int(length) 198 length = int(length)
196 data = b'' 199 data = b''
197 while len(data) < length: 200 while len(data) < length:
198 newByte = self.sock.recv(1) 201 remaining = length - len(data)
199 data += newByte 202 newBytes = self.sock.recv(min(remaining, AsyncFile.CMD_BUFSIZE))
200 if newByte == b'\n': 203 data += newBytes
204 if newBytes[-1] == b'\n':
201 break 205 break
202 206
203 # step 2: convert the data 207 # step 2: convert the data
204 return data.decode('utf8', 'backslashreplace') 208 return data.decode('utf8', 'backslashreplace')
205 209
216 @rtype str 220 @rtype str
217 """ 221 """
218 self.__checkMode('r') 222 self.__checkMode('r')
219 223
220 if size < 0: 224 if size < 0:
221 size = 20000 225 size = AsyncFile.BUFSIZE
222 226
223 # The integration of the debugger client event loop and the connection 227 # The integration of the debugger client event loop and the connection
224 # to the debugger relies on the two lines of the debugger command being 228 # to the debugger relies on the two lines of the debugger command being
225 # delivered as two separate events. Therefore we make sure we only 229 # delivered as two separate events. Therefore we make sure we only
226 # read a line at a time. 230 # read a line at a time.

eric ide

mercurial