def start(self, relayHost, relayPort, serverHost, serverPort): def ON_CLIENT_CONNECT(relayClient): serverClient = client.Client(password=self.password, clientID=relayClient["clientID"], clientType=relayClient["clientType"]) serverClient.eventScope = relayClient["clientID"] def ON_RECEIVE(data): self.relayServer.sendTo(relayClient["clientSocket"], data) events.registerEvent(events.Event("ON_RECEIVE", ON_RECEIVE, scope=serverClient.eventScope)) if self.serverClientEncrypt is True: serverClient.enableEncrypt(self.serverClient_secret_key) serverClient.printUnsignedData = False Thread(target=serverClient.connect, args=[serverHost, serverPort]).start() self.clients.append({"relayClient": relayClient, "serverClient": serverClient}) def ON_CLIENT_DISCONNECT(client): for c in self.clients: if c["relayClient"] == client: c["serverClient"].disconnect() self.clients.remove(c) for event in events.events: if event.name == "ON_RECEIVE" and event.scope == client["clientID"]: events.unregisterEvent(event) def ON_RECEIVE(client, data): for c in self.clients: if c["relayClient"] == client: c["serverClient"].send(data) events.registerEvent(events.Event("ON_CLIENT_CONNECT", ON_CLIENT_CONNECT)) events.registerEvent(events.Event("ON_CLIENT_DISCONNECT", ON_CLIENT_DISCONNECT)) events.registerEvent(events.Event("ON_RECEIVE", ON_RECEIVE)) if self.relayServerEncrypt is True: self.relayServer.enableEncrypt(self.relayServer_secret_key) self.relayServer.printUnsignedData = False Thread(target=self.relayServer.start, args=[relayHost, relayPort]).start()
def ON_CLIENT_CONNECT(relayClient): serverClient = client.Client(password=self.password, clientID=relayClient["clientID"], clientType=relayClient["clientType"]) serverClient.eventScope = relayClient["clientID"] def ON_RECEIVE(data): self.relayServer.sendTo(relayClient["clientSocket"], data) events.registerEvent(events.Event("ON_RECEIVE", ON_RECEIVE, scope=serverClient.eventScope)) if self.serverClientEncrypt is True: serverClient.enableEncrypt(self.serverClient_secret_key) serverClient.printUnsignedData = False Thread(target=serverClient.connect, args=[serverHost, serverPort]).start() self.clients.append({"relayClient": relayClient, "serverClient": serverClient})
def sendPackageAndWaitForPackage(self, package, method, maxTime=1.5): self.waitReceived = None def ON_RECEIVE(data): if data["METHOD"] == method: self.waitReceived = data event = events.Event("ON_RECEIVE", ON_RECEIVE, scope=self.eventScope) events.registerEvent(event) self.send(package) startTime = time.time() while self.waitReceived is None and (time.time() - startTime) <= maxTime: time.sleep(0.1) events.unregisterEvent(event) return self.waitReceived
def ON_RECEIVE(self, function): events.registerEvent( events.Event("ON_RECEIVE", function, scope=self.eventScope))
def ON_CLIENT_DISCONNECT(self, function): events.registerEvent( events.Event("ON_CLIENT_DISCONNECT", function, scope=self.eventScope))