[IMP] OSX handling of socket shutdown in TinySocketClientThread
[odoo/odoo.git] / openerp / tiny_socket.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import socket
23 import cPickle
24 import cStringIO
25 import marshal
26
27 import netsvc
28
29 class Myexception(Exception):
30     """
31     custome exception object store 
32     * faultcode
33     * faulestring
34     * args
35     """
36     
37     def __init__(self, faultCode, faultString):
38         self.faultCode = faultCode
39         self.faultString = faultString
40         self.args = (faultCode, faultString)
41
42 class mysocket:
43
44     def __init__(self, sock=None):
45         if sock is None:
46             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
47         else:
48             self.sock = sock
49         # self.sock.settimeout(120)
50         # prepare this socket for long operations: it may block for infinite
51         # time, but should exit as soon as the net is down
52         self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
53         
54     def connect(self, host, port=False):
55         if not port:
56             protocol, buf = host.split('//')
57             host, port = buf.split(':')
58         self.sock.connect((host, int(port)))
59         
60     def disconnect(self):
61         netsvc.close_socket(self.sock)
62         
63     def mysend(self, msg, exception=False, traceback=None):
64         msg = cPickle.dumps([msg,traceback])
65         self.sock.sendall('%8d%s%s' % (len(msg), exception and "1" or "0", msg))
66             
67     def myreceive(self):
68         buf=''
69         while len(buf) < 8:
70             chunk = self.sock.recv(8 - len(buf))
71             if not chunk:
72                 raise socket.timeout
73             buf += chunk
74         size = int(buf)
75         buf = self.sock.recv(1)
76         if buf != "0":
77             exception = buf
78         else:
79             exception = False
80         msg = ''
81         while len(msg) < size:
82             chunk = self.sock.recv(size-len(msg))
83             if not chunk:
84                 raise socket.timeout
85             msg = msg + chunk
86         msgio = cStringIO.StringIO(msg)
87         unpickler = cPickle.Unpickler(msgio)
88         unpickler.find_global = None
89         res = unpickler.load()
90
91         if isinstance(res[0],Exception):
92             if exception:
93                 raise Myexception(str(res[0]), str(res[1]))
94             raise res[0]
95         else:
96             return res[0]