aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpmulticonnection.py
blob: 927ac06cc58166c741c0cbfa1020dc40080c4019 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  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 = []
  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. self.log.info("Incomming Connection from %s", addr)
  22. self.currentConnections.append(conn)
  23. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  24. self.on_accepted(conn, addr)
  25. return True
  26. def close_connection(self, conn):
  27. self.currentConnections.remove(conn)
  28. self.log.info('Now %u Receiver connected', len(self.currentConnections))