blob: a7d661703dc3a6221340dacd087e8f9049bbe89e [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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 Slee89e2bb82007-03-01 00:20:36 +000019
David Reisse29995e2008-07-31 20:15:17 +000020import errno
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +090021import logging
Bryan Duxbury69720412012-01-03 17:32:30 +000022import os
Mark Sleecde2b612006-09-03 21:13:07 +000023import socket
David Reiss73af3b72010-08-30 21:57:07 +000024import sys
Mark Sleecde2b612006-09-03 21:13:07 +000025
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090026from .TTransport import TTransportBase, TTransportException, TServerTransportBase
Bryan Duxbury69720412012-01-03 17:32:30 +000027
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +090028logger = logging.getLogger(__name__)
29
Bryan Duxbury69720412012-01-03 17:32:30 +000030
David Reisse29995e2008-07-31 20:15:17 +000031class TSocketBase(TTransportBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090032 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 Sleecde2b612006-09-03 21:13:07 +000043
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090044 def close(self):
45 if self.handle:
46 self.handle.close()
47 self.handle = None
David Reisse29995e2008-07-31 20:15:17 +000048
Bryan Duxbury69720412012-01-03 17:32:30 +000049
David Reisse29995e2008-07-31 20:15:17 +000050class TSocket(TSocketBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090051 """Socket implementation of TTransport base."""
Mark Sleecde2b612006-09-03 21:13:07 +000052
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090053 def __init__(self, host='localhost', port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC):
54 """Initialize a TSocket
David Reissc16a8f62007-12-14 23:46:47 +000055
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090056 @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 Reiss0c90f6f2008-02-06 22:18:40 +000068
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090069 def setHandle(self, h):
70 self.handle = h
Mark Sleec9676562006-09-05 17:34:52 +000071
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090072 def isOpen(self):
73 return self.handle is not None
Aditya Agarwalf954f972007-02-06 01:26:12 +000074
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090075 def setTimeout(self, ms):
76 if ms is None:
77 self._timeout = None
78 else:
79 self._timeout = ms / 1000.0
David Reiss0c90f6f2008-02-06 22:18:40 +000080
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090081 if self.handle is not None:
82 self.handle.settimeout(self._timeout)
Mark Sleecde2b612006-09-03 21:13:07 +000083
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +090084 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 Sukegawa10308cb2016-02-03 01:57:03 +090091 def open(self):
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +090092 if self.handle:
93 raise TTransportException(TTransportException.ALREADY_OPEN)
Mark Slee22974602007-07-06 22:20:19 +000094 try:
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +090095 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 Sleecde2b612006-09-03 21:13:07 +0000114
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900115 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 Sleecde2b612006-09-03 21:13:07 +0000134
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900135 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 Sleecde2b612006-09-03 21:13:07 +0000148
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900149 def flush(self):
150 pass
Mark Sleec9676562006-09-05 17:34:52 +0000151
Bryan Duxbury69720412012-01-03 17:32:30 +0000152
David Reissd73255d2009-03-24 22:51:02 +0000153class TServerSocket(TSocketBase, TServerTransportBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900154 """Socket implementation of TServerTransport base."""
Mark Sleec9676562006-09-05 17:34:52 +0000155
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900156 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
lshgdut7af79c82018-11-21 10:09:42 +0800162 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 Slee256bdc42007-11-27 08:42:19 +0000171
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900172 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 Slee22974602007-07-06 22:20:19 +0000178
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900179 # 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 Reisse29995e2008-07-31 20:15:17 +0000189
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900190 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])
lshgdut7af79c82018-11-21 10:09:42 +0800195 self.handle.listen(self._backlog)
Mark Sleec9676562006-09-05 17:34:52 +0000196
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900197 def accept(self):
198 client, addr = self.handle.accept()
199 result = TSocket()
200 result.setHandle(client)
201 return result