summaryrefslogtreecommitdiff
path: root/voctocore/lib/tcpsingleconnection.py
blob: d6a05ef98367bc172562fd597f69dd2d39c3f119 (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. port = None
  8. boundSocket = None
  9. currentConnection = None
  10. def __init__(self, port):
  11. self.port = port
  12. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  13. self.boundSocket = socket.socket(socket.AF_INET6)
  14. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  15. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  16. self.boundSocket.bind(('::', port))
  17. self.boundSocket.listen(1)
  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')