summaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpsingleconnection.py
blob: 4ff94d29fe4cf68d9b0100cbbe5d1c1966df19c6 (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. 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")
  24. return True
  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')