Handle multi-packet TCP queries

Change-Id: Ia85db4a5d9ba4e57423f34e99e3fb7b299b3d796
This commit is contained in:
Kiall Mac Innes 2014-12-03 22:41:51 +00:00
parent 1ca524dc79
commit 37559c8f73
2 changed files with 24 additions and 13 deletions

View File

@ -45,6 +45,8 @@ OPTS = [
help='mDNS Port Number'),
cfg.IntOpt('tcp-backlog', default=100,
help='mDNS TCP Backlog'),
cfg.FloatOpt('tcp-recv-timeout', default=0.5,
help='mDNS TCP Receive Timeout'),
cfg.StrOpt('storage-driver', default='sqlalchemy',
help='The storage driver to use'),
]

View File

@ -111,24 +111,33 @@ class Service(service.RPCService):
LOG.info(_LI("_handle_tcp thread started"))
while True:
client, addr = self._sock_tcp.accept()
client.settimeout(CONF['service:mdns'].tcp_recv_timeout)
LOG.warn(_LW("Handling TCP Request from: %(host)s:%(port)d") %
{'host': addr[0], 'port': addr[1]})
payload = client.recv(65535)
(expected_length,) = struct.unpack('!H', payload[0:2])
actual_length = len(payload[2:])
# Prepare a variable for the payload to be buffered
payload = ""
# For now we assume all requests are one packet
# TODO(vinod): Handle multipacket requests
if (expected_length != actual_length):
LOG.warn(_LW("got a packet with unexpected length from "
"%(host)s:%(port)d. Expected length=%(elen)d. "
"Actual length=%(alen)d.") %
{'host': addr[0], 'port': addr[1],
'elen': expected_length, 'alen': actual_length})
try:
# Receive the first 2 bytes containing the payload length
expected_length_raw = client.recv(2)
(expected_length, ) = struct.unpack('!H', expected_length_raw)
# Keep receiving data until we've got all the data we expect
while len(payload) < expected_length:
data = client.recv(65535)
if not data:
break
payload += data
except socket.timeout:
client.close()
else:
self.tg.add_thread(self._handle, addr, payload[2:], client)
LOG.warn(_LW("TCP Timeout from: %(host)s:%(port)d") %
{'host': addr[0], 'port': addr[1]})
# Dispatch a thread to handle the query
self.tg.add_thread(self._handle, addr, payload, client)
def _handle_udp(self):
LOG.info(_LI("_handle_udp thread started"))