def parse(self, data: bytes) -> FastPathOutputEvent: stream = BytesIO(data) header = Uint8.unpack(stream) compressionFlags = None if self.isCompressed(header): compressionFlags = Uint8.unpack(stream) size = Uint16LE.unpack(stream) eventType = header & 0xf fragmentation = header & 0b00110000 != 0 if fragmentation: log.debug( "Fragmentation is present in output fastpath event packets." " Not parsing it and saving to FastPathOutputUpdateEvent.") return FastPathOutputEvent(header, compressionFlags, payload=stream.read(size)) if eventType == FastPathOutputType.FASTPATH_UPDATETYPE_BITMAP: return self.parseBitmapEventRaw(stream, header, compressionFlags, size) elif eventType == FastPathOutputType.FASTPATH_UPDATETYPE_ORDERS: return self.parseOrdersEvent(stream, header, compressionFlags, size) read = stream.read(size) return FastPathOutputEvent(header, compressionFlags, read)
def onPDUReceived(self, pdu: PlayerPDU, isMainThread = False): if not isMainThread: self.viewer.mainThreadHook.emit(lambda: self.onPDUReceived(pdu, True)) return log.debug("Received %(pdu)s", {"pdu": pdu}) if pdu.header in self.handlers: self.handlers[pdu.header](pdu)
def recv(self, data): try: pdu = self.mainParser.parse(data) except UnknownPDUTypeError as e: log.debug(str(e)) if self.observer: self.observer.onUnparsedData(data) else: self.pduReceived(pdu, self.hasNext)
def onPDUReceived(self, pdu: PlayerPDU, isMainThread=False): # Ensure we are on the main thread. if not isMainThread: self.viewer.mainThreadHook.emit( lambda: self.onPDUReceived(pdu, True)) return log.debug("Received %(pdu)s", {"pdu": pdu}) # Call the parent PDU Received method. super().onPDUReceived(pdu)
def onInput(self, pdu: PlayerMessagePDU): pdu = self.inputParser.parse(pdu.payload) for event in pdu.events: if isinstance(event, FastPathScanCodeEvent): log.debug("handling %(arg1)s", {"arg1": event}) self.onScanCode(event.scancode, not event.isReleased) elif isinstance(event, FastPathMouseEvent): self.onMousePosition(event.mouseX, event.mouseY) else: log.debug("Can't handle input event: %(arg1)s", {"arg1": event})
def send(self, data): """ Send data through the socket :type data: bytes """ if self.isConnected: try: log.debug("sending %(arg1)s to %(arg2)s", { "arg1": data, "arg2": self.socket.getpeername() }) self.socket.send(data) except Exception as e: log.error("Cant send data over the network socket: %(data)s", {"data": e}) self.isConnected = False
def onScanCode(self, code: int, isPressed: bool): """ Handle scan code. """ log.debug("Reading scancode %(arg1)s", {"arg1": code}) if code in [0x2A, 0x36]: self.text.moveCursor(QTextCursor.End) self.text.insertPlainText( "\n<LSHIFT PRESSED>" if isPressed else "\n<LSHIFT RELEASED>") self.writeInCaps = not self.writeInCaps elif code == 0x3A and isPressed: self.text.moveCursor(QTextCursor.End) self.text.insertPlainText("\n<CAPSLOCK>") self.writeInCaps = not self.writeInCaps elif isPressed: char = scancodeToChar(code) self.text.moveCursor(QTextCursor.End) self.text.insertPlainText( char if self.writeInCaps else char.lower())
def onOutput(self, pdu: PlayerMessagePDU): pdu = self.outputParser.parse(pdu.payload) for event in pdu.events: reassembledEvent = self.reassembleEvent(event) if reassembledEvent is not None: if isinstance(reassembledEvent, FastPathOrdersEvent): log.debug("Not handling orders event, not coded :)") elif isinstance(reassembledEvent, FastPathBitmapEvent): log.debug("Handling bitmap event %(arg1)s", {"arg1": type(reassembledEvent)}) self.onBitmap(reassembledEvent) else: log.debug("Can't handle output event: %(arg1)s", {"arg1": type(reassembledEvent)}) else: log.debug("Reassembling output event...")
def onPDUReceived(self, pdu: PlayerPDU, isMainThread=False): log.debug("Received %(pdu)s", {"pdu": pdu}) if pdu.header in self.handlers: self.handlers[pdu.header](pdu)