aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpsingleconnection.py
blob: e604ce6bbcc031f239a6cf4c583073b4bb954036 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject
  4. from lib.config import Config
  5. class TCPSingleConnection(object):
  6. log = logging.getLogger('TCPSingleConnection')
  7. def __init__(self, port):
  8. if not hasattr(self, 'log'):
  9. self.log = logging.getLogger('TCPMultiConnection')
  10. self.port = port
  11. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  12. self.boundSocket = socket.socket(socket.AF_INET6)
  13. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  14. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 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. return True
  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')