aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpsingleconnection.py
blob: 3f589a7095b17dadfd705a6530fbd2cd4c225f44 (plain)
  1. import logging
  2. import socket
  3. import time
  4. from gi.repository import GObject
  5. from lib.config import Config
  6. class TCPSingleConnection(object):
  7. def __init__(self, port):
  8. if not hasattr(self, 'log'):
  9. self.log = logging.getLogger('TCPSingleConnection')
  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,
  14. False)
  15. self.boundSocket.bind(('::', port))
  16. self.boundSocket.listen(1)
  17. self.currentConnection = None
  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. if self.currentConnection is not None:
  24. self.log.warn('Another Source is already connected, '
  25. 'closing existing pipeline')
  26. self.disconnect()
  27. time.sleep(1)
  28. self.on_accepted(conn, addr)
  29. self.currentConnection = conn
  30. return True
  31. def close_connection(self):
  32. self.currentConnection = None
  33. self.log.info('Connection closed')