bool NetPacket::ParsePacketHeader(Sstr &packet) { if(packet.size() < GetPacketHeaderSize()){ return false; }else{ char *curpos = packet.data(); _PacketHeader._PacketId = ReadVarInt32(curpos); _PacketHeader._PacketType = ReadVarInt32(curpos+4); _PacketHeader._PacketDataLength = ReadVarInt32(curpos+8); return true; } }
EDemoCommands CDemoFile::ReadMessageType( int *pTick, bool *pbCompressed ) { uint32 Cmd = ReadVarInt32( m_fileBuffer, m_fileBufferPos ); if( pbCompressed ) *pbCompressed = !!( Cmd & DEM_IsCompressed ); Cmd = ( Cmd & ~DEM_IsCompressed ); int Tick = ReadVarInt32( m_fileBuffer, m_fileBufferPos ); if( pTick ) *pTick = Tick; if( m_fileBufferPos >= m_fileBuffer.size() ) return DEM_Error; return ( EDemoCommands )Cmd; }
/** * Determine which of the messages in demo.proto comes next. * * @param pTick Optional Will provide the updated tick count with the message * @param pbCompressed Optional Will be true if the following message is compressed * @return The type of the message that should come next in the file */ EDemoCommands CDemoFile::ReadMessageType( int *pTick, bool *pbCompressed ) { uint32 Cmd = ReadVarInt32( m_fileBuffer, m_fileBufferPos ); if( pbCompressed )//This is a null check *pbCompressed = !!( Cmd & DEM_IsCompressed );//Double negation to go from uint32 to bool without truncation issues. Cmd = ( Cmd & ~DEM_IsCompressed );//the second three bits (0x70) are being used to say if it's compressed not sure why 3 bits. This may increase the total number of allowable values in overloaded field int Tick = ReadVarInt32( m_fileBuffer, m_fileBufferPos ); if( pTick )//Another null check *pTick = Tick; if( m_fileBufferPos >= m_fileBuffer.size() )//This would indicate that we'd finished the string already. return DEM_Error;//If we'd actually gone > rather than = random memory would have been read. return ( EDemoCommands )Cmd; }
/** * @param pMsg The demo message to parse the uncompressed buffer * @param bCompressed - Whether or not the message is compressed * @param */ bool CDemoFile::ReadMessage( IDemoMessage *pMsg, bool bCompressed, int *pSize, int *pUncompressedSize ) { int Size = ReadVarInt32( m_fileBuffer, m_fileBufferPos ); if( pSize ) { *pSize = Size; } if( pUncompressedSize )//Assume we set this to zero so it doesn't get used by accident { *pUncompressedSize = 0; } if( m_fileBufferPos + Size > m_fileBuffer.size() ) { assert( 0 ); return false; } if( pMsg )//we don't bother actually reading it if they don't care about the results. { const char *parseBuffer = &m_fileBuffer[ m_fileBufferPos ]; m_fileBufferPos += Size; if( bCompressed ) { if ( snappy::IsValidCompressedBuffer( parseBuffer, Size ) ) { size_t uDecompressedLen; if ( snappy::GetUncompressedLength( parseBuffer, Size, &uDecompressedLen ) ) { if( pUncompressedSize ) { *pUncompressedSize = uDecompressedLen; } m_parseBufferSnappy.resize( uDecompressedLen );//we checked how big it was now we give ourselves the space char *parseBufferUncompressed = &m_parseBufferSnappy[ 0 ]; if ( snappy::RawUncompress( parseBuffer, Size, parseBufferUncompressed ) ) { if ( pMsg->GetProtoMsg().ParseFromArray( parseBufferUncompressed, uDecompressedLen ) ) { return true; } } } } assert( 0 ); fprintf( stderr, "CDemoFile::ReadMessage() snappy::RawUncompress failed.\n" ); return false; } return pMsg->GetProtoMsg().ParseFromArray( parseBuffer, Size ); //I would have put this in an else } else//even if they don't care about the message we still need to move past the message we were supposed to read { m_fileBufferPos += Size; return true; } }
bool CDemoFile::ReadMessage( IDemoMessage *pMsg, bool bCompressed, int *pSize, int *pUncompressedSize ) { int Size = ReadVarInt32( m_fileBuffer, m_fileBufferPos ); if( pSize ) { *pSize = Size; } if( pUncompressedSize ) { *pUncompressedSize = 0; } if( m_fileBufferPos + Size > m_fileBuffer.size() ) { assert( 0 ); return false; } if( pMsg ) { const char *parseBuffer = &m_fileBuffer[ m_fileBufferPos ]; m_fileBufferPos += Size; if( bCompressed ) { if ( snappy::IsValidCompressedBuffer( parseBuffer, Size ) ) { size_t uDecompressedLen; if ( snappy::GetUncompressedLength( parseBuffer, Size, &uDecompressedLen ) ) { if( pUncompressedSize ) { *pUncompressedSize = uDecompressedLen; } m_parseBufferSnappy.resize( uDecompressedLen ); char *parseBufferUncompressed = &m_parseBufferSnappy[ 0 ]; if ( snappy::RawUncompress( parseBuffer, Size, parseBufferUncompressed ) ) { if ( pMsg->GetProtoMsg().ParseFromArray( parseBufferUncompressed, uDecompressedLen ) ) { return true; } } } } assert( 0 ); fprintf( stderr, "CDemoFile::ReadMessage() snappy::RawUncompress failed.\n" ); return false; } return pMsg->GetProtoMsg().ParseFromArray( parseBuffer, Size ); } else { m_fileBufferPos += Size; return true; } }