Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 19 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 20 | import errno |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 21 | import logging |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 22 | import os |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | import socket |
David Reiss | 73af3b7 | 2010-08-30 21:57:07 +0000 | [diff] [blame] | 24 | import sys |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 25 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 26 | from .TTransport import TTransportBase, TTransportException, TServerTransportBase |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 27 | |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 28 | logger = logging.getLogger(__name__) |
| 29 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 30 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 31 | class TSocketBase(TTransportBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 32 | def _resolveAddr(self): |
| 33 | if self._unix_socket is not None: |
| 34 | return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None, |
| 35 | self._unix_socket)] |
| 36 | else: |
| 37 | return socket.getaddrinfo(self.host, |
| 38 | self.port, |
| 39 | self._socket_family, |
| 40 | socket.SOCK_STREAM, |
| 41 | 0, |
| 42 | socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 43 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 44 | def close(self): |
| 45 | if self.handle: |
| 46 | self.handle.close() |
| 47 | self.handle = None |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 48 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 49 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 50 | class TSocket(TSocketBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 51 | """Socket implementation of TTransport base.""" |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 52 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 53 | def __init__(self, host='localhost', port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC): |
| 54 | """Initialize a TSocket |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 55 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 56 | @param host(str) The host to connect to. |
| 57 | @param port(int) The (TCP) port to connect to. |
| 58 | @param unix_socket(str) The filename of a unix socket to connect to. |
| 59 | (host and port will be ignored.) |
| 60 | @param socket_family(int) The socket family to use with this socket. |
| 61 | """ |
| 62 | self.host = host |
| 63 | self.port = port |
| 64 | self.handle = None |
| 65 | self._unix_socket = unix_socket |
| 66 | self._timeout = None |
| 67 | self._socket_family = socket_family |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 68 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 69 | def setHandle(self, h): |
| 70 | self.handle = h |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 71 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 72 | def isOpen(self): |
| 73 | return self.handle is not None |
Aditya Agarwal | f954f97 | 2007-02-06 01:26:12 +0000 | [diff] [blame] | 74 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | def setTimeout(self, ms): |
| 76 | if ms is None: |
| 77 | self._timeout = None |
| 78 | else: |
| 79 | self._timeout = ms / 1000.0 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 80 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 81 | if self.handle is not None: |
| 82 | self.handle.settimeout(self._timeout) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 83 | |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 84 | def _do_open(self, family, socktype): |
| 85 | return socket.socket(family, socktype) |
| 86 | |
| 87 | @property |
| 88 | def _address(self): |
| 89 | return self._unix_socket if self._unix_socket else '%s:%d' % (self.host, self.port) |
| 90 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 91 | def open(self): |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 92 | if self.handle: |
| 93 | raise TTransportException(TTransportException.ALREADY_OPEN) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 94 | try: |
Nobuaki Sukegawa | 1c8b5cb | 2016-02-14 22:04:38 +0900 | [diff] [blame] | 95 | addrs = self._resolveAddr() |
| 96 | except socket.gaierror: |
| 97 | msg = 'failed to resolve sockaddr for ' + str(self._address) |
| 98 | logger.exception(msg) |
| 99 | raise TTransportException(TTransportException.NOT_OPEN, msg) |
| 100 | for family, socktype, _, _, sockaddr in addrs: |
| 101 | handle = self._do_open(family, socktype) |
| 102 | handle.settimeout(self._timeout) |
| 103 | try: |
| 104 | handle.connect(sockaddr) |
| 105 | self.handle = handle |
| 106 | return |
| 107 | except socket.error: |
| 108 | handle.close() |
| 109 | logger.info('Could not connect to %s', sockaddr, exc_info=True) |
| 110 | msg = 'Could not connect to any of %s' % list(map(lambda a: a[4], |
| 111 | addrs)) |
| 112 | logger.error(msg) |
| 113 | raise TTransportException(TTransportException.NOT_OPEN, msg) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 114 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 115 | def read(self, sz): |
| 116 | try: |
| 117 | buff = self.handle.recv(sz) |
| 118 | except socket.error as e: |
| 119 | if (e.args[0] == errno.ECONNRESET and |
| 120 | (sys.platform == 'darwin' or sys.platform.startswith('freebsd'))): |
| 121 | # freebsd and Mach don't follow POSIX semantic of recv |
| 122 | # and fail with ECONNRESET if peer performed shutdown. |
| 123 | # See corresponding comment and code in TSocket::read() |
| 124 | # in lib/cpp/src/transport/TSocket.cpp. |
| 125 | self.close() |
| 126 | # Trigger the check to raise the END_OF_FILE exception below. |
| 127 | buff = '' |
| 128 | else: |
| 129 | raise |
| 130 | if len(buff) == 0: |
| 131 | raise TTransportException(type=TTransportException.END_OF_FILE, |
| 132 | message='TSocket read 0 bytes') |
| 133 | return buff |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 134 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 135 | def write(self, buff): |
| 136 | if not self.handle: |
| 137 | raise TTransportException(type=TTransportException.NOT_OPEN, |
| 138 | message='Transport not open') |
| 139 | sent = 0 |
| 140 | have = len(buff) |
| 141 | while sent < have: |
| 142 | plus = self.handle.send(buff) |
| 143 | if plus == 0: |
| 144 | raise TTransportException(type=TTransportException.END_OF_FILE, |
| 145 | message='TSocket sent 0 bytes') |
| 146 | sent += plus |
| 147 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 148 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 149 | def flush(self): |
| 150 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 151 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 152 | |
David Reiss | d73255d | 2009-03-24 22:51:02 +0000 | [diff] [blame] | 153 | class TServerSocket(TSocketBase, TServerTransportBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 154 | """Socket implementation of TServerTransport base.""" |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 155 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 156 | def __init__(self, host=None, port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC): |
| 157 | self.host = host |
| 158 | self.port = port |
| 159 | self._unix_socket = unix_socket |
| 160 | self._socket_family = socket_family |
| 161 | self.handle = None |
lshgdut | 7af79c8 | 2018-11-21 10:09:42 +0800 | [diff] [blame] | 162 | self._backlog = 128 |
| 163 | |
| 164 | def setBacklog(self, backlog=None): |
| 165 | if not self.handle: |
| 166 | self._backlog = backlog |
| 167 | else: |
| 168 | # We cann't update backlog when it is already listening, since the |
| 169 | # handle has been created. |
| 170 | logger.warn('You have to set backlog before listen.') |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 171 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 172 | def listen(self): |
| 173 | res0 = self._resolveAddr() |
| 174 | socket_family = self._socket_family == socket.AF_UNSPEC and socket.AF_INET6 or self._socket_family |
| 175 | for res in res0: |
| 176 | if res[0] is socket_family or res is res0[-1]: |
| 177 | break |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 178 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 179 | # We need remove the old unix socket if the file exists and |
| 180 | # nobody is listening on it. |
| 181 | if self._unix_socket: |
| 182 | tmp = socket.socket(res[0], res[1]) |
| 183 | try: |
| 184 | tmp.connect(res[4]) |
| 185 | except socket.error as err: |
| 186 | eno, message = err.args |
| 187 | if eno == errno.ECONNREFUSED: |
| 188 | os.unlink(res[4]) |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 189 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 190 | self.handle = socket.socket(res[0], res[1]) |
| 191 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 192 | if hasattr(self.handle, 'settimeout'): |
| 193 | self.handle.settimeout(None) |
| 194 | self.handle.bind(res[4]) |
lshgdut | 7af79c8 | 2018-11-21 10:09:42 +0800 | [diff] [blame] | 195 | self.handle.listen(self._backlog) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 196 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 197 | def accept(self): |
| 198 | client, addr = self.handle.accept() |
| 199 | result = TSocket() |
| 200 | result.setHandle(client) |
| 201 | return result |