Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # Copyright (c) 2006- Facebook |
| 2 | # Distributed under the Thrift Software License |
| 3 | # |
| 4 | # See accompanying file LICENSE or visit the Thrift site at: |
| 5 | # http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 7 | from TTransport import * |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 8 | import os |
| 9 | import errno |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 10 | import socket |
| 11 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 12 | class TSocketBase(TTransportBase): |
| 13 | def _resolveAddr(self): |
| 14 | if self._unix_socket is not None: |
| 15 | return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None, self._unix_socket)] |
| 16 | else: |
| 17 | return socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 18 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 19 | def close(self): |
| 20 | if self.handle: |
| 21 | self.handle.close() |
| 22 | self.handle = None |
| 23 | |
| 24 | class TSocket(TSocketBase): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 25 | """Socket implementation of TTransport base.""" |
| 26 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 27 | def __init__(self, host='localhost', port=9090, unix_socket=None): |
| 28 | """Initialize a TSocket |
| 29 | |
| 30 | @param host(str) The host to connect to. |
| 31 | @param port(int) The (TCP) port to connect to. |
| 32 | @param unix_socket(str) The filename of a unix socket to connect to. |
| 33 | (host and port will be ignored.) |
| 34 | """ |
| 35 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 36 | self.host = host |
Aditya Agarwal | 9bae5e7 | 2007-02-07 02:36:56 +0000 | [diff] [blame] | 37 | self.port = port |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 38 | self.handle = None |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 39 | self._unix_socket = unix_socket |
| 40 | self._timeout = None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 41 | |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 42 | def setHandle(self, h): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 43 | self.handle = h |
| 44 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 45 | def isOpen(self): |
Aditya Agarwal | f954f97 | 2007-02-06 01:26:12 +0000 | [diff] [blame] | 46 | return self.handle != None |
| 47 | |
| 48 | def setTimeout(self, ms): |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 49 | if ms is None: |
| 50 | self._timeout = None |
James Wang | e168d5e | 2007-07-24 23:59:51 +0000 | [diff] [blame] | 51 | else: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 52 | self._timeout = ms/1000.0 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 53 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 54 | if (self.handle != None): |
| 55 | self.handle.settimeout(self._timeout) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 56 | |
| 57 | def open(self): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 58 | try: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 59 | res0 = self._resolveAddr() |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 60 | for res in res0: |
| 61 | self.handle = socket.socket(res[0], res[1]) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 62 | self.handle.settimeout(self._timeout) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 63 | try: |
| 64 | self.handle.connect(res[4]) |
| 65 | except socket.error, e: |
| 66 | if res is not res0[-1]: |
| 67 | continue |
| 68 | else: |
| 69 | raise e |
| 70 | break |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 71 | except socket.error, e: |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 72 | if self._unix_socket: |
| 73 | message = 'Could not connect to socket %s' % self._unix_socket |
| 74 | else: |
| 75 | message = 'Could not connect to %s:%d' % (self.host, self.port) |
| 76 | raise TTransportException(TTransportException.NOT_OPEN, message) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 77 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 78 | def read(self, sz): |
| 79 | buff = self.handle.recv(sz) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 80 | if len(buff) == 0: |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 81 | raise TTransportException('TSocket read 0 bytes') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 82 | return buff |
| 83 | |
| 84 | def write(self, buff): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 85 | sent = 0 |
| 86 | have = len(buff) |
| 87 | while sent < have: |
| 88 | plus = self.handle.send(buff) |
| 89 | if plus == 0: |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 90 | raise TTransportException('TSocket sent 0 bytes') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 91 | sent += plus |
| 92 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 93 | |
| 94 | def flush(self): |
| 95 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 96 | |
David Reiss | d73255d | 2009-03-24 22:51:02 +0000 | [diff] [blame] | 97 | class TServerSocket(TSocketBase, TServerTransportBase): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 98 | """Socket implementation of TServerTransport base.""" |
| 99 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 100 | def __init__(self, port=9090, unix_socket=None): |
| 101 | self.host = None |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 102 | self.port = port |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 103 | self._unix_socket = unix_socket |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 104 | self.handle = None |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 105 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 106 | def listen(self): |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 107 | res0 = self._resolveAddr() |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 108 | for res in res0: |
| 109 | if res[0] is socket.AF_INET6 or res is res0[-1]: |
| 110 | break |
| 111 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 112 | # We need remove the old unix socket if the file exists and |
| 113 | # nobody is listening on it. |
| 114 | if self._unix_socket: |
| 115 | tmp = socket.socket(res[0], res[1]) |
| 116 | try: |
| 117 | tmp.connect(res[4]) |
| 118 | except socket.error, err: |
| 119 | eno, message = err.args |
| 120 | if eno == errno.ECONNREFUSED: |
| 121 | os.unlink(res[4]) |
| 122 | |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 123 | self.handle = socket.socket(res[0], res[1]) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 124 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 125 | if hasattr(self.handle, 'set_timeout'): |
| 126 | self.handle.set_timeout(None) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 127 | self.handle.bind(res[4]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 128 | self.handle.listen(128) |
| 129 | |
| 130 | def accept(self): |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 131 | client, addr = self.handle.accept() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 132 | result = TSocket() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 133 | result.setHandle(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 134 | return result |