def run(self): if self.parent.sock is None: return data = "" while not self.stopflag: rfds, _, efds = select.select([self.parent.sock], [], [self.parent.sock], 0.1) if len(efds) > 0: logger.error("remote client error") break if len(rfds) < 1: continue try: data = self.parent.sock.recv(SockClient.RECV_SIZE) except: logger.error("recv error") break if data == "": logger.error("socket closed") break logger.debug("data from %s:%d -> %s" % (self.parent.ip, self.parent.port, data)) sigObject.emit(signals.SIG_DATA_RECVED, self.parent._id, data) self.parent.close(self.stopflag) logger.debug("tcp client stopped")
def onRemoteClosed(self, _id, parentId): if _id != self.sock.getId(): logger.debug("NOT CURRENT SOCKET, IGNORE") return sockType = self.sock.getSockType() if sockType == socktypes.TCP_CLIENT_LOCAL: self.setupUi_disconnected() logger.debug("setupUi_disconnected") elif sockType == socktypes.TCP_CLIENT_REMOTE: sigObject.emit(signals.SIG_REMOVE_SOCK_TAB, self.sock.getId())
def run(self): while not self.stopflag: #logger.debug("waiting for client...") try: client, addr = self.sock.accept() tcpClient = TcpClient(self._id, client, addr, socktypes.TCP_CLIENT_REMOTE) sigObject.emit(signals.SIG_REMOTE_TCP_CLIENT_CONNECTED, tcpClient, self._id, tcpClient.getId(), addr[0], addr[1]) logger.debug("new client %s:%d connected" % addr) tcpClient.start() self.tcpClients[tcpClient.getId()] = tcpClient except socket.timeout: pass logger.debug("server stopped")
def onConnectBtnClicked(self): sockType = self.sock.getSockType() if self.sock.isConnected(): if sockType == socktypes.TCP_CLIENT_LOCAL: self.sock.stop(onlyStopSocket=True) self.setupUi_disconnected() elif sockType == socktypes.TCP_CLIENT_REMOTE: sigObject.emit(signals.SIG_REMOVE_SOCK_TAB, self.sock.getId()) else: if sockType == socktypes.TCP_CLIENT_LOCAL: if self.sock.connect(): self.sock.start() self.setupUi_connected()
def close(self, stopflag): logger.debug("------ CLOSE ------") if self.sock is None: return self.sock.close() self.sock = None self.conFlag = False if not stopflag: logger.debug("emit SIG_REMOTE_CLOSED") sigObject.emit(signals.SIG_REMOTE_CLOSED, self._id, self.parentId) elif not self.onlyStopSocket: logger.debug("emit SIG_REMOVE_SOCK_TAB") sigObject.emit(signals.SIG_REMOVE_SOCK_TAB, self._id)
def run(self): while not self.stopflag: rfds, _, efds = select.select([self.sock], [], [self.sock], 0.1) if len(efds) > 0: logger.error("remote client error") break if len(rfds) < 1: continue try: data, self.remoteAddr = self.sock.recvfrom(UdpServer.RECV_SIZE) logger.debug("recv data -> %s from -> %s:%d" % (data, self.remoteAddr[0], self.remoteAddr[1])) sigObject.emit(signals.SIG_DATA_RECVED, self._id, data) except Exception as e: logger.debug("recv exp. %s" % e.message) break self.close()
def handleDiscovery(self, dev, isNewDev, isNewData): #logger.debug("%s[%d]" % (dev.addr, dev.rssi)) sigObject.emit(signals.BLE_INFO_UPDATE, dev.addr, dev.rssi)
def disconnectBle(self): if self.sock.sendall(protocolPacker.getDisconnectBuff("")): sigObject.emit(signals.SIG_DISCONNECT_BLE)