[MERGE] server: improve the socket.recv (Thanks to Florent Xicluna)

bzr revid: stw@openerp.com-20120910145708-jupna7l26ik4xt0a
This commit is contained in:
Stephane Wirtel 2012-09-10 16:57:08 +02:00
commit 9836732557
1 changed files with 11 additions and 8 deletions

View File

@ -27,6 +27,10 @@ import netsvc
#.apidoc title: Net-RPC classes
# Pickle protocol version 2 is optimized compared to default (version 0)
PICKLE_PROTOCOL = 2
class Myexception(Exception):
"""
custom exception object store
@ -62,20 +66,19 @@ class mysocket:
netsvc.close_socket(self.sock)
def mysend(self, msg, exception=False, traceback=None):
msg = cPickle.dumps([msg,traceback])
self.sock.sendall('%8d%s%s' % (len(msg), exception and "1" or "0", msg))
msg = cPickle.dumps([msg, traceback], PICKLE_PROTOCOL)
self.sock.sendall('%8d%d%s' % (len(msg), bool(exception), msg))
def myreceive(self):
buf=''
while len(buf) < 8:
chunk = self.sock.recv(8 - len(buf))
while len(buf) < 9:
chunk = self.sock.recv(9 - len(buf))
if not chunk:
raise socket.timeout
buf += chunk
size = int(buf)
buf = self.sock.recv(1)
if buf != "0":
exception = buf
size = int(buf[:8])
if buf[8] != "0":
exception = buf[8]
else:
exception = False
msg = ''