summaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpsingleconnection.py
blob: 886318fff1255ef6e82ee4e9bd38b1550b8767af (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.port = port
  10. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  11. self.boundSocket = socket.socket(socket.AF_INET6)
  12. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  13. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  14. self.boundSocket.bind(('::', port))
  15. self.boundSocket.listen(1)
  16. self.currentConnection = None
  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. if self.currentConnection is not None:
  23. self.log.warn("Another Source is already connected, closing existing pipeline")
  24. self.disconnect()
  25. time.sleep(1)
  26. self.on_accepted(conn, addr)
  27. self.currentConnection = conn
  28. return True
  29. def close_connection(self):
  30. self.currentConnection = None
  31. self.log.info('Connection closed')