aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpmulticonnection.py
blob: e7ab700153d9224cea6c3d85f8a06386cf09c028 (plain)
  1. import logging
  2. import socket
  3. from queue import Queue
  4. from gi.repository import GObject
  5. from lib.config import Config
  6. class TCPMultiConnection(object):
  7. def __init__(self, port):
  8. if not hasattr(self, 'log'):
  9. self.log = logging.getLogger('TCPMultiConnection')
  10. self.boundSocket = None
  11. self.currentConnections = dict()
  12. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  13. self.boundSocket = socket.socket(socket.AF_INET6)
  14. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  15. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY,
  16. False)
  17. self.boundSocket.bind(('::', port))
  18. self.boundSocket.listen(1)
  19. self.log.debug('Setting GObject io-watch on Socket')
  20. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  21. def on_connect(self, sock, *args):
  22. conn, addr = sock.accept()
  23. conn.setblocking(False)
  24. self.log.info("Incomming Connection from [%s]:%u (fd=%u)",
  25. addr[0], addr[1], conn.fileno())
  26. self.currentConnections[conn] = Queue()
  27. self.log.info('Now %u Receiver connected',
  28. len(self.currentConnections))
  29. self.on_accepted(conn, addr)
  30. return True
  31. def close_connection(self, conn):
  32. if conn in self.currentConnections:
  33. conn.close()
  34. del(self.currentConnections[conn])
  35. self.log.info('Now %u Receiver connected',
  36. len(self.currentConnections))