Пример #1
0
void NetClient::ReadPacket(ENetEvent event)
{
    ENetPacket *pPacket = event.packet;
    assert(pPacket->dataLength>=1);
    unsigned char packetId = pPacket->data[0];
    //unsigned char *pData = &pPacket->data[1];
    //int datasize = pPacket->dataLength-1;

    switch (packetId)
    {
    case RACEINFOCHANGE_PACKET:
        ReadRaceSetupPacket(event.packet);
        break;
    case PREPARETORACE_PACKET:
        ReadPrepareToRacePacket(event.packet);
        break;
    case RACESTARTTIME_PACKET:
        ReadStartTimePacket(event.packet);
        break;
    case CARCONTROLS_PACKET:
        ReadCarControlsPacket(event.packet);
        break;
    case FILE_PACKET:
        ReadFilePacket(event.packet);
        break;
    case SERVER_TIME_SYNC_PACKET:
        ReadTimePacket(event.packet);
        break;
    case WEATHERCHANGE_PACKET:
        ReadWeatherPacket(event.packet);
        break;
    case CARSTATUS_PACKET:
        ReadCarStatusPacket(event.packet);
        break;
    case LAPSTATUS_PACKET:
        ReadLapStatusPacket(event.packet);
        break;
    case FINISHTIME_PACKET:
        ReadFinishTimePacket(event.packet);
        break;
    case ALLDRIVERREADY_PACKET:
        ReadAllDriverReadyPacket(event.packet);
        break;
    case PLAYERREJECTED_PACKET:
        ReadPlayerRejectedPacket(event.packet);
        break;
    case PLAYERACCEPTED_PACKET:
        ReadPlayerAcceptedPacket(event.packet);
        break;
    default:
        assert(false);
        GfLogDebug ("A packet of length %u containing %s was received from %s on channel %u.\n",
                event.packet -> dataLength,
                event.packet -> data,
                (char*)event.peer -> data,
                event.channelID);

    }

    enet_packet_destroy (event.packet);
}
Пример #2
0
void NetServer::ReadPacket(ENetEvent event)
{
	ENetPacket *pPacket = event.packet;
	assert(pPacket->dataLength>=1);
	unsigned char packetId = pPacket->data[0];
	//int datasize = pPacket->dataLength-1;
	
        switch (packetId)
        {
        case PLAYERINFO_PACKET:
                GfLogTrace("PlayerInfo Packet\n");
                ReadDriverInfoPacket(pPacket,event.peer);
                break;
        case CLIENTREADYTOSTART_PACKET:
                {
                        int l;
                        char name[256];
                        memset(&name[0],0,256);

                        PackedBuffer msg(pPacket->data, pPacket->dataLength);
                        GfLogTrace("ReadPacket: packed data length=%d\n",
                                msg.length());

                        try
                        {
                                msg.unpack_ubyte();
                                l = msg.unpack_int();
                                msg.unpack_string(name, l);
                        }
                        catch (PackedBufferException &e)
                        {
                                GfLogFatal("ReadPacket: packed buffer error\n");
                        }

                        std::vector<NetDriver>::iterator p;
                        p = m_vecWaitForPlayers.begin();
                        while(p!=m_vecWaitForPlayers.end())
                        {
                                if (strcmp(p->name,name)==0)
                                {
                                        GfLogTrace("%s ready to start\n",&name[0]);
                                        m_vecWaitForPlayers.erase(p);
                                        break;
                                }

                                p++;
                        }

                        if (m_vecWaitForPlayers.size()==0)
                                m_bBeginRace = true;

                }
                break;
        case SERVER_TIME_REQUEST_PACKET:
                SendTimePacket(pPacket,event.peer);
                break;
        case CARCONTROLS_PACKET:
                ReadCarControlsPacket(event.packet);
                break;
        case CARSTATUS_PACKET:
                ReadCarStatusPacket(event.packet);
                break;
        case LAPSTATUS_PACKET:
                ReadLapStatusPacket(event.packet);
                break;
        case DRIVERREADY_PACKET:
                ReadDriverReadyPacket(event.packet);
                break;

	default:
			GfLogTrace ("A packet of length %u containing %s was received from %s on channel %u.\n",
                    event.packet -> dataLength,
                    event.packet -> data,
                    (char*)event.peer -> data,
                    event.channelID);
	}

    enet_packet_destroy (event.packet);
}