Exemplo n.º 1
0
void CNetServer::Process()
{
	CPacket * pPacket = NULL;

	// Loop until we have processed all packets in the packet queue (if any)
	while(pPacket = Receive())
	{
		// Do we have a packet handler?
		if(m_pfnPacketHandler)
		{
			// Pass it to the packet handler
			m_pfnPacketHandler(pPacket);
		}
		// Deallocate the packet memory used
		DeallocatePacket(pPacket);
	}
}
Exemplo n.º 2
0
void CClientPacketRecorder::DoPulse()
{
    // Count the frame
    m_lFrames += m_uiFrameSkip;

    // Are we playing?
    if (m_bPlaying && m_pfnPacketHandler && m_szFilename)
    {
        // Time to play the next packet?
        long lCurTime = (long)CClientTime::GetTime() - m_lRelative;
        // g_pCore->GetConsole()->Printf("current time:%u\n",lCurTime);
        while ((m_bFrameBased && (m_lFrames - m_lRelative >= m_lNextPacketTime)) || (!m_bFrameBased && (lCurTime >= m_lNextPacketTime)))
        {
            // Load the file
            FILE* pFile = fopen(m_szFilename, "rb");
            if (pFile)
            {
                // Seek to our current offset + the bytes occupied by the time?
                fseek(pFile, m_ulCurrentOffset, SEEK_SET);

                unsigned long ulTimeStamp;
                fread(&ulTimeStamp, sizeof(unsigned long), 1, pFile);

                // Reached end of file?
                if (feof(pFile))
                {
                    fclose(pFile);
                    Stop();
                    return;
                }

                // Read out the packet id
                unsigned char ucPacketID = fgetc(pFile);

                // Is it 0xFE (local player data) or 0xFF (local keys)?
                if (ucPacketID == 0xFE)
                {
                    ReadLocalData(pFile);
                }
                else
                {
                    // Raise a breakpoint?
                    if (ucPacketID == 0xFD)
                    {
                        assert(false);
                        Stop();
                        return;
                    }

                    // Read out number of bytes in the packet
                    unsigned long ulSize = 0;
                    fread(&ulSize, sizeof(unsigned long), 1, pFile);

                    // Create a bitstream
                    NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
                    if (pBitStream)
                    {
                        // Write the filedata to the bitstream
                        for (unsigned long i = 0; i < ulSize; i++)
                        {
                            char c = fgetc(pFile);
                            pBitStream->Write(c);
                        }

                        // Send it to the packethandler
                        // g_pCore->GetConsole()->Printf("(time: %u, current: %u) %u\n",ulTimeStamp,lCurTime,ucPacketID);
                        m_pfnPacketHandler(ucPacketID, *pBitStream);

                        // Destroy the bitstream
                        g_pNet->DeallocateNetBitStream(pBitStream);
                    }
                }

                // Remember the new offset and read out the time for the next packet
                m_ulCurrentOffset = ftell(pFile);
                fread(&m_lNextPacketTime, sizeof(long), 1, pFile);
                m_ucNextPacketID = fgetc(pFile);
                // g_pCore->GetConsole()->Printf("next time: %u\n",m_lNextPacketTime);

                // Reached end of file?
                int iTemp = 0;
                fread(&iTemp, sizeof(int), 1, pFile);
                if (feof(pFile))
                {
                    fclose(pFile);
                    Stop();
                    return;
                }

                // Close the file
                fclose(pFile);
            }
        }
    }
}