67 |
67 |
68 @return tuple containing the received command and a dictionary |
68 @return tuple containing the received command and a dictionary |
69 containing the associated data |
69 containing the associated data |
70 @rtype tuple of (str, dict) |
70 @rtype tuple of (str, dict) |
71 """ |
71 """ |
72 line = self.__connection.recv(1024 * 1024, socket.MSG_PEEK) |
72 # step 1: receive the data |
73 # 1MB buffer |
73 # The JSON RPC string is prefixed by a 9 character long length field. |
74 |
74 length = self.__connection.recv(9) |
75 eol = line.find(b'\n') |
75 length = int(length) |
76 |
76 data = b'' |
77 if eol >= 0: |
77 while len(data) < length: |
78 size = eol + 1 |
78 newData = self.__connection.recv(length - len(data)) |
79 |
79 if not newData: |
80 # Now we know how big the line is, read it for real. |
|
81 line = self.__connection.recv(size).decode( |
|
82 'utf8', 'backslashreplace') |
|
83 try: |
|
84 commandDict = json.loads(line.strip()) |
|
85 except (TypeError, ValueError) as err: |
|
86 self.sendJson("ClientException", { |
|
87 "ExceptionType": "ProtocolError", |
|
88 "ExceptionValue": str(err), |
|
89 "ProtocolData": line.strip(), |
|
90 }) |
|
91 return None, None |
80 return None, None |
92 |
81 data += newData |
93 method = commandDict["method"] |
82 |
94 params = commandDict["params"] |
83 # step 2: decode and convert the data |
95 |
84 line = data.decode( |
96 return method, params |
85 'utf8', 'backslashreplace') |
97 |
86 try: |
98 return None, None |
87 commandDict = json.loads(line.strip()) |
|
88 except (TypeError, ValueError) as err: |
|
89 self.sendJson("ClientException", { |
|
90 "ExceptionType": "ProtocolError", |
|
91 "ExceptionValue": str(err), |
|
92 "ProtocolData": line.strip(), |
|
93 }) |
|
94 return None, None |
|
95 |
|
96 method = commandDict["method"] |
|
97 params = commandDict["params"] |
|
98 |
|
99 return method, params |
99 |
100 |
100 def handleCall(self, method, params): |
101 def handleCall(self, method, params): |
101 """ |
102 """ |
102 Public method to handle a method call from the server. |
103 Public method to handle a method call from the server. |
103 |
104 |