aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpmulticonnection.py
blob: c47381dcf6a926f787af48d6e8aaa3c83c05ee82 (plain)
  1. #!/usr/bin/python3
  2. import logging, 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, False)
  16. self.boundSocket.bind(('::', port))
  17. self.boundSocket.listen(1)
  18. self.log.debug('Setting GObject io-watch on Socket')
  19. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  20. def on_connect(self, sock, *args):
  21. conn, addr = sock.accept()
  22. conn.setblocking(False)
  23. self.log.info("Incomming Connection from [%s]:%u (fd=%u)", addr[0], addr[1], conn.fileno())
  24. self.currentConnections[conn] = Queue()
  25. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  26. self.on_accepted(conn, addr)
  27. return True
  28. def close_connection(self, conn):
  29. if conn in self.currentConnections:
  30. del(self.currentConnections[conn])
  31. self.log.info('Now %u Receiver connected', len(self.currentConnections))