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