def parseListChannelsResponse(self, data): #self.channels = {} if len(data) <= 8: logdebug().error('No channels found') self.sigChannelsLoaded.emit() return status1, data = Protocol.extractInt(data) status2, data = Protocol.extractInt(data) logdebug().info("Load channels header " + repr(status1) + repr(status2)) while len(data) > 4: room, data = Protocol.extractTLV(data) romname, data = Protocol.extractTLV(data) title, data = Protocol.extractTLV(data) users, data = Protocol.extractInt(data) port, data = Protocol.extractInt(data) index, data = Protocol.extractInt(data) # 'sfa3': {'title': 'Street Fighter Alpha 3', 'rom': 'sfa3:sfa3u', 'room': 'sfa3'}, # 'sfa2': {'title': 'Street Fighter Alpha 2', 'rom': 'sfa2', 'room': 'sfa2'}, channel = { 'rom': romname.split(':')[0], 'room': room, 'title': title, 'users': users, 'port': port, } self.channels[room] = channel logdebug().info(repr(self.channels)) self.sigChannelsLoaded.emit() if len(data) > 0: logdebug().error('Channel REMAINING DATA len {} {}'.format( len(data), repr(data)))
def parseMessage(self, data): """ Decode and route a message received from server Message is encoded as follows: byte 0-3 - positive integer, 0 for internal messages, 1 for referee messages, otherwise the extension ID of the extension to receive the message. byte 4-7 - extension-defined positive integer byte 8+ - json-packed python object parameter Args: data: Encoded message data """ cmd, data = Protocol.extractTLV(data) extID, cmd = Protocol.extractInt(cmd) prefix, cmd = Protocol.extractInt(cmd) params = None if cmd == '' else json.loads(cmd) # DEBUG dispext = "Extension" if extID == 0 else _ExtensionDict[extID].__name__ dispprefix = self._codeToString.get((extID, prefix), prefix) self.controller.sigStatusMessage.emit( "Ext Rcv: ext=%s, prefix=%s, params=%s" % (dispext, dispprefix, params)) if extID == 0: # internal message if prefix == _Message.InitializationDataResponse: # initialization data received from server # self.referee = ExtensionReferee(gamedata=params[1], extensionIO=self) self.gameVariableData = params[1] self._commandDict = {("/" + chatcommand): extID for chatcommand, extID in params[2]} self._sigInstantiate.emit(params[0]) self._evtInstantiationFinished.wait( ) # Block message loop until extensions are initialized # If it hangs here, the __init__() in some extension is freezing elif prefix == _Message.ChatMessage: self.chatMessage(params) elif prefix == _Message.KillEmulator: self.controller.killEmulator() elif prefix == _Message.BeginMonitoring: if self._monitorThread != None and self._monitorThread.is_alive( ): return self._monitorThread = threading.Thread( target=self._monitorCurrentMatch) self._monitorThread.daemon = True self._monitorThread.start() elif extID in self._instanceDict: # message for another extension if extID in self._instanceDict: self._instanceDict[extID].receiveMessage(prefix, params)
def extractStateChangesResponse(data): if len(data) >= 4: code, data = Protocol.extractInt(data) p1, data = Protocol.extractTLV(data) if code == 0: p2 = '' return PlayerStates.QUIT, p1, p2, None, data elif code != 1: logdebug().error( "Unknown player state change code {}".format(code)) state, data = Protocol.extractInt(data) p2, data = Protocol.extractTLV(data) if not p2: p2 = "null" ip, data = Protocol.extractTLV(data) # \xff\xff\xff\x9f # \x00\x00\x00& unknown1, data = Protocol.extractInt(data) unknown2, data = Protocol.extractInt(data) city, data = Protocol.extractTLV(data) cc, data = Protocol.extractTLV(data) if cc: cc = cc.lower() country, data = Protocol.extractTLV(data) # \x00\x00\x17y marker, data = Protocol.extractInt(data) playerinfo = dict( player=p1, ip=ip, city=city, cc=cc, country=country, spectators=0, ) return state, p1, p2, playerinfo, data
def extractStateChangesResponse(data): if len(data) >= 4: code, data = Protocol.extractInt(data) p1, data = Protocol.extractTLV(data) if code == 0: p2 = '' return PlayerStates.QUIT, p1, p2, None, data elif code != 1: logdebug().error("Unknown player state change code {}".format(code)) state, data = Protocol.extractInt(data) p2, data = Protocol.extractTLV(data) if not p2: p2 = "null" ip, data = Protocol.extractTLV(data) # \xff\xff\xff\x9f # \x00\x00\x00& unknown1, data = Protocol.extractInt(data) unknown2, data = Protocol.extractInt(data) city, data = Protocol.extractTLV(data) cc, data = Protocol.extractTLV(data) if cc: cc = cc.lower() country, data = Protocol.extractTLV(data) # \x00\x00\x17y marker, data = Protocol.extractInt(data) playerinfo = dict( player=p1, ip=ip, city=city, cc=cc, country=country, ) return state, p1, p2, playerinfo, data
def parseListChannelsResponse(self, data): #self.channels = {} if len(data) <= 8: logdebug().error('No channels found') self.sigChannelsLoaded.emit() return status1, data = Protocol.extractInt(data) status2, data = Protocol.extractInt(data) logdebug().info("Load channels header " + repr(status1) + repr(status2)) while len(data) > 4: room, data = Protocol.extractTLV(data) romname, data = Protocol.extractTLV(data) title, data = Protocol.extractTLV(data) users, data = Protocol.extractInt(data) index, data = Protocol.extractInt(data) # 'sfa3': {'title': 'Street Fighter Alpha 3', 'rom': 'sfa3:sfa3u', 'room': 'sfa3'}, # 'sfa2': {'title': 'Street Fighter Alpha 2', 'rom': 'sfa2', 'room': 'sfa2'}, channel = { 'rom': romname.split(':')[0], 'room': room, 'title': title, 'users': users, } self.channels[room] = channel logdebug().info(repr(self.channels)) self.sigChannelsLoaded.emit() if len(data) > 0: logdebug().error('Channel REMAINING DATA len {} {}'.format(len(data), repr(data)))
def dispatchInbandData(self, seq, data): if not seq in self.tcpCommandsWaitingForResponse: logdebug().error("Sequence {} data {} not matched".format(seq, data)) return origRequest = self.tcpCommandsWaitingForResponse[seq] del self.tcpCommandsWaitingForResponse[seq] if origRequest == Protocol.AUTH: self.parseAuthResponse(data) elif origRequest == Protocol.MOTD: self.parseMotdResponse(data) elif origRequest == Protocol.LIST_CHANNELS: self.parseListChannelsResponse(data) elif origRequest == Protocol.LIST_USERS: self.parseListUsersResponse(data) elif origRequest == Protocol.SPECTATE: status, data = Protocol.extractInt(data) if status != 0: self.sigStatusMessage.emit("Fail to spectate " + str(status)) elif origRequest in [Protocol.WELCOME, Protocol.JOIN_CHANNEL, Protocol.TOGGLE_AFK, Protocol.SEND_CHALLENGE, Protocol.CHAT, Protocol.ACCEPT_CHALLENGE, Protocol.DECLINE_CHALLENGE, Protocol.CANCEL_CHALLENGE]: if len(data) == 4: status, data = Protocol.extractInt(data) if status != 0: codestr = Protocol.codeToString(origRequest) logdebug().error("{} failed, data {}".format(codestr, repr(data))) self.sigActionFailed.emit(codestr) else: logdebug().error("Unknown response for {}; seq {}; data {}".format( Protocol.codeToString(origRequest), seq, repr(data))) else: logdebug().error("Not handling {} response; seq {}; data {}".format( Protocol.codeToString(origRequest), seq, repr(data)))
def parseAuthResponse(self, data): if len(data) < 4: logdebug().error("Unknown auth response {}".format(repr(data))) return result, data = Protocol.extractInt(data) if result == 0: self.selectTimeout = 15 self.sigLoginSuccess.emit() # password incorrect, user incorrect #if result == 0x6 or result == 0x4: else: if self.tcpSock: self.tcpSock.close() self.tcpConnected = False #if self.udpSock: # self.udpSock.close() # self.udpConnected = False self.sigLoginFailed.emit() #self.sigStatusMessage.emit("Login failed {}".format(result)) if result==6: self.sigStatusMessage.emit("Login failed: wrong password") elif result==9: self.sigStatusMessage.emit("Login failed: too many connections") elif result==4: self.sigStatusMessage.emit("Login failed: username doesn't exist into database") elif result==8: self.sigStatusMessage.emit("Clone connection closed.\nPlease login again.") else: self.sigStatusMessage.emit("Login failed {}".format(result))
def parseAuthResponse(self, data): if len(data) < 4: logdebug().error("Unknown auth response {}".format(repr(data))) return result, data = Protocol.extractInt(data) if result == 0: self.selectTimeout = 15 self.sigLoginSuccess.emit() # password incorrect, user incorrect #if result == 0x6 or result == 0x4: else: if self.tcpSock: self.tcpSock.close() self.tcpConnected = False #if self.udpSock: # self.udpSock.close() # self.udpConnected = False self.sigLoginFailed.emit() #self.sigStatusMessage.emit("Login failed {}".format(result)) if result == 6: self.sigStatusMessage.emit("Login failed: wrong password") elif result == 9: self.sigStatusMessage.emit( "Login failed: too many connections") elif result == 4: self.sigStatusMessage.emit( "Login failed: username doesn't exist into database") elif result == 8: self.sigStatusMessage.emit( "Clone connection closed.\nPlease login again.") else: self.sigStatusMessage.emit("Login failed {}".format(result))
def handleTcpResponse(self): if self.tcpReadState == self.STATE_TCP_READ_LEN: if len(self.tcpData) >= 4: self.tcpResponseLen, self.tcpData = Protocol.extractInt( self.tcpData) self.tcpReadState = self.STATE_TCP_READ_DATA self.handleTcpResponse() elif self.tcpReadState == self.STATE_TCP_READ_DATA: if len(self.tcpData) >= self.tcpResponseLen: # tcpResponseLen should be >= 4 if self.tcpResponseLen < 4: logdebug().error( 'Cannot handle TLV payload of less than 4 bytes') self.tcpData = self.tcpData[self.tcpResponseLen:] self.tcpResponseLen = 0 self.tcpReadState = self.STATE_TCP_READ_LEN self.handleTcpResponse() else: data = self.tcpData[:self.tcpResponseLen] self.tcpData = self.tcpData[self.tcpResponseLen:] seq = Protocol.unpackInt(data[0:4]) self.tcpResponseLen = 0 self.tcpReadState = self.STATE_TCP_READ_LEN self.dispatch(seq, data[4:]) self.handleTcpResponse()
def parseStateChangesResponse(self, data): count, data = Protocol.extractInt(data) while count > 0 and len(data) >= 4: state, p1, p2, playerinfo, data = self.__class__.extractStateChangesResponse( data) if state == PlayerStates.PLAYING: self.parsePlayerStartGameResponse(p1, p2, playerinfo) if Settings.USER_LOG_PLAYHISTORY and self.username in [p1, p2]: loguser().info(u"[IN A GAME] {} vs {}".format(p1, p2)) elif state == PlayerStates.AVAILABLE: self.parsePlayerAvailableResponse(p1, playerinfo) elif state == PlayerStates.AFK: self.parsePlayerAFKResponse(p1, playerinfo) elif state == PlayerStates.QUIT: self.parsePlayerLeftResponse(p1) else: logdebug().error( "Unknown state change payload state: {} {}".format( state, repr(data))) if state == PlayerStates.PLAYING: msg = p1 + ' ' + PlayerStates.codeToString(state) + ' ' + p2 else: msg = p1 + ' ' + PlayerStates.codeToString(state) logdebug().info(msg) count -= 1 if len(data) > 0: logdebug().error("stateChangesResponse, remaining data {}".format( repr(data)))
def parseStateChangesResponse(self, data): count, data = Protocol.extractInt(data) while count > 0 and len(data) >= 4: state, p1, p2, playerinfo, data = self.__class__.extractStateChangesResponse(data) if state == PlayerStates.PLAYING: self.parsePlayerStartGameResponse(p1, p2, playerinfo) if self.username == p1: self.playingagainst = p2 if self.username == p2: self.playingagainst = p1 if Settings.USER_LOG_PLAYHISTORY and self.username in [p1, p2]: loguser().info(u"[IN A GAME] {} vs {}".format(p1, p2)) elif state == PlayerStates.AVAILABLE: self.parsePlayerAvailableResponse(p1, playerinfo) if self.playingagainst == p1: self.playingagainst = '' self.killEmulator() elif state == PlayerStates.AFK: self.parsePlayerAFKResponse(p1, playerinfo) if self.playingagainst == p1: self.playingagainst = '' self.killEmulator() elif state == PlayerStates.QUIT: self.parsePlayerLeftResponse(p1) else: logdebug().error( "Unknown state change payload state: {} {}".format(state, repr(data))) if state == PlayerStates.PLAYING: msg = p1 + ' ' + PlayerStates.codeToString(state) + ' ' + p2 else: msg = p1 + ' ' + PlayerStates.codeToString(state) logdebug().info(msg) count -= 1 if len(data) > 0: logdebug().error("stateChangesResponse, remaining data {}".format(repr(data)))
def dispatchInbandData(self, seq, data): if not seq in self.tcpCommandsWaitingForResponse: logdebug().error("Sequence {} data {} not matched".format( seq, data)) return origRequest = self.tcpCommandsWaitingForResponse[seq] del self.tcpCommandsWaitingForResponse[seq] if origRequest == Protocol.AUTH: self.parseAuthResponse(data) elif origRequest == Protocol.MOTD: self.parseMotdResponse(data) elif origRequest == Protocol.LIST_CHANNELS: self.parseListChannelsResponse(data) elif origRequest == Protocol.LIST_USERS: self.parseListUsersResponse(data) elif origRequest == Protocol.SPECTATE: status, data = Protocol.extractInt(data) if status != 0: self.sigStatusMessage.emit("Fail to spectate " + str(status)) elif origRequest in [ Protocol.WELCOME, Protocol.JOIN_CHANNEL, Protocol.TOGGLE_AFK, Protocol.SEND_CHALLENGE, Protocol.CHAT, Protocol.ACCEPT_CHALLENGE, Protocol.DECLINE_CHALLENGE, Protocol.CANCEL_CHALLENGE ]: if len(data) == 4: status, data = Protocol.extractInt(data) if status != 0: codestr = Protocol.codeToString(origRequest) logdebug().error("{} failed, data {}".format( codestr, repr(data))) if codestr == "SEND_CHALLENGE": self.sigActionFailed.emit("SEND_CHALLENGE failed") elif codestr == "CANCEL_CHALLENGE": pass else: self.sigActionFailed.emit(codestr) else: logdebug().error( "Unknown response for {}; seq {}; data {}".format( Protocol.codeToString(origRequest), seq, repr(data))) else: logdebug().error( "Not handling {} response; seq {}; data {}".format( Protocol.codeToString(origRequest), seq, repr(data)))
def parseMotdResponse(self, data): if not data: return status, data = Protocol.extractInt(data) channel, data = Protocol.extractTLV(data) topic, data = Protocol.extractTLV(data) msg, data = Protocol.extractTLV(data) self.sigMotdReceived.emit(channel, topic, msg)
def parseListUsersResponse(self, data): self.resetPlayers() if not data: return status, data = Protocol.extractInt(data) status2, data = Protocol.extractInt(data) while len(data) > 8: p1, data = Protocol.extractTLV(data) # if len(data) <= 4: break state, data = Protocol.extractInt(data) p2, data = Protocol.extractTLV(data) ip, data = Protocol.extractTLV(data) unk1, data = Protocol.extractInt(data) unk2, data = Protocol.extractInt(data) city, data = Protocol.extractTLV(data) cc, data = Protocol.extractTLV(data) if cc: cc = cc.lower() country, data = Protocol.extractTLV(data) port, data = Protocol.extractInt(data) color, data = Protocol.extractInt(data) color = None if color >= 0x1000000 else QtGui.QColor((color & 0xFF0000) >> 16, (color & 0xFF00) >> 8, color & 0xFF) spectators, data = Protocol.extractInt(data) self.addUser( player=p1, ip=ip, port=port, city=city, cc=cc, country=country, color=color, spectators=spectators+1, ) if state == PlayerStates.AVAILABLE: self.available[p1] = True elif state == PlayerStates.AFK: self.awayfromkb[p1] = True elif state == PlayerStates.PLAYING: if not p2: p2 = 'null' self.playing[p1] = p2 self.sigPlayersLoaded.emit() if len(data) > 0: logdebug().error('List users - REMAINING DATA len {} {}'.format(len(data), repr(data)))
def parseAuthResponse(self, data): if len(data) < 4: logdebug().error("Unknown auth response {}".format(repr(data))) return result, data = Protocol.extractInt(data) if result == 0: self.selectTimeout = 15 self.sigLoginSuccess.emit() # password incorrect, user incorrect #if result == 0x6 or result == 0x4: else: if self.tcpSock: self.tcpSock.close() self.tcpConnected = False if self.udpSock: self.udpSock.close() self.udpConnected = False self.sigLoginFailed.emit() self.sigStatusMessage.emit("Login failed {}".format(result))
def parseListUsersResponse(self, data): self.resetPlayers() if not data: return status, data = Protocol.extractInt(data) status2, data = Protocol.extractInt(data) while len(data) > 8: p1, data = Protocol.extractTLV(data) # if len(data) <= 4: break state, data = Protocol.extractInt(data) p2, data = Protocol.extractTLV(data) ip, data = Protocol.extractTLV(data) unk1, data = Protocol.extractInt(data) unk2, data = Protocol.extractInt(data) city, data = Protocol.extractTLV(data) cc, data = Protocol.extractTLV(data) if cc: cc = cc.lower() country, data = Protocol.extractTLV(data) port, data = Protocol.extractInt(data) spectators, data = Protocol.extractInt(data) self.addUser( player=p1, ip=ip, port=port, city=city, cc=cc, country=country, spectators=spectators+1, ) if state == PlayerStates.AVAILABLE: self.available[p1] = True elif state == PlayerStates.AFK: self.awayfromkb[p1] = True elif state == PlayerStates.PLAYING: if not p2: p2 = 'null' self.playing[p1] = p2 self.sigPlayersLoaded.emit() if len(data) > 0: logdebug().error('List users - REMAINING DATA len {} {}'.format(len(data), repr(data)))
def handleTcpResponse(self): if self.tcpReadState == self.STATE_TCP_READ_LEN: if len(self.tcpData) >= 4: self.tcpResponseLen, self.tcpData = Protocol.extractInt(self.tcpData) self.tcpReadState = self.STATE_TCP_READ_DATA self.handleTcpResponse() elif self.tcpReadState == self.STATE_TCP_READ_DATA: if len(self.tcpData) >= self.tcpResponseLen: # tcpResponseLen should be >= 4 if self.tcpResponseLen < 4: logdebug().error('Cannot handle TLV payload of less than 4 bytes') self.tcpData = self.tcpData[self.tcpResponseLen:] self.tcpResponseLen = 0 self.tcpReadState = self.STATE_TCP_READ_LEN self.handleTcpResponse() else: data = self.tcpData[:self.tcpResponseLen] self.tcpData = self.tcpData[self.tcpResponseLen:] seq = Protocol.unpackInt(data[0:4]) self.tcpResponseLen = 0 self.tcpReadState = self.STATE_TCP_READ_LEN self.dispatch(seq, data[4:]) self.handleTcpResponse()
def parseStateChangesResponse(self, data): count, data = Protocol.extractInt(data) while count > 0 and len(data) >= 4: state, p1, p2, playerinfo, data = self.__class__.extractStateChangesResponse(data) if state == PlayerStates.PLAYING: self.parsePlayerStartGameResponse(p1, p2, playerinfo) elif state == PlayerStates.AVAILABLE: self.parsePlayerAvailableResponse(p1, playerinfo) elif state == PlayerStates.AFK: self.parsePlayerAFKResponse(p1, playerinfo) elif state == PlayerStates.QUIT: self.parsePlayerLeftResponse(p1) else: logger().error( "Unknown state change payload state: {} {}".format(state, repr(data))) if state == PlayerStates.PLAYING: msg = p1 + ' ' + PlayerStates.codeToString(state) + ' ' + p2 else: msg = p1 + ' ' + PlayerStates.codeToString(state) logger().info(msg) count -= 1 if len(data) > 0: logger().error("stateChangesResponse, remaining data {}".format(repr(data)))