summaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpsingleconnection.py
blob: d08f43034013a33f88a77eb529f3d3921daceff9 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket, time
  3. from gi.repository import GObject
  4. from lib.config import Config
  5. class TCPSingleConnection(object):
  6. def __init__(self, port):
  7. if not hasattr(self, 'log'):
  8. self.log = logging.getLogger('TCPMultiConnection')
  9. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  10. self.boundSocket = socket.socket(socket.AF_INET6)
  11. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  12. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  13. self.boundSocket.bind(('::', port))
  14. self.boundSocket.listen(1)
  15. self.currentConnection = None
  16. self.log.debug('Setting GObject io-watch on Socket')
  17. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  18. def on_connect(self, sock, *args):
  19. conn, addr = sock.accept()
  20. self.log.info("Incomming Connection from %s", addr)
  21. if self.currentConnection is not None:
  22. self.log.warn("Another Source is already connected, closing existing pipeline")
  23. self.disconnect()
  24. time.sleep(1)
  25. self.on_accepted(conn, addr)
  26. self.currentConnection = conn
  27. return True
  28. def close_connection(self):
  29. self.currentConnection = None
  30. self.log.info('Connection closed')