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