aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpmulticonnection.py
blob: 2ca6921f9c6642d372c970adacbcdf09b26b0379 (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. log = logging.getLogger('TCPMultiConnection')
  7. port = None
  8. boundSocket = None
  9. currentConnections = []
  10. def __init__(self, port):
  11. self.port = port
  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. self.log.info("Incomming Connection from %s", addr)
  23. self.currentConnections.append(conn)
  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. self.currentConnections.remove(conn)
  29. self.log.info('Disconnected Receiver %s', conn.getpeername())
  30. self.log.info('Now %u Receiver connected', len(self.currentConnections))