summaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpmulticonnection.py
blob: 5ad4ca2f97ec70ee5a50e2dd696ed171e1bb7fc5 (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.port = port
  10. self.port = None
  11. self.boundSocket = None
  12. self.currentConnections = []
  13. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  14. self.boundSocket = socket.socket(socket.AF_INET6)
  15. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  16. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 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. self.log.info("Incomming Connection from %s", addr)
  24. self.currentConnections.append(conn)
  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, addr=None):
  29. if addr is None:
  30. addr = conn.getpeername()
  31. self.currentConnections.remove(conn)
  32. self.log.info('Disconnected Receiver %s', addr)
  33. self.log.info('Now %u Receiver connected', len(self.currentConnections))