#include "main.h" //Cuts the first "size" bytes off a packet and builds the remainder into another packet void PacketChop(unsigned char* pDataArray, NetMessage* pData, int size) { assert(pDataArray!=NULL); if (size==-1){return;} if (size==pData->dataSize){return;} NetMessage s; s.pData=pDataArray+size; s.dataSize=pData->dataSize-size; s.networkId=pData->networkId; gpFileServer->AddIncomingFront(&s); } //Just a simple accessor for local data LocalData* AccessLocalData(short id) { hash_map::iterator itr=gData.find(id); if (itr!=gData.end()) { return &itr->second; } return NULL; } //Handles all client packets void OpcodeProcessing() { //Create a data pack and go through and get all available messages from the server. NetMessage pckData; while (gpFileServer->GetNextNetMessage(&pckData)==0) { assert(pckData.networkId>0); switch(pckData.dataSize) { case CLIENT_DISCONNECTED: gData.erase(pckData.networkId); break; case CLIENT_CONNECTED: if (true) { LocalData ldata; memset(&ldata, 0, sizeof(LocalData)); ldata.networkId=pckData.networkId; ldata.wtfByte=0xF1; typedef pair lpair; gData.insert(lpair(pckData.networkId, ldata)); } break; default: //Make sure a login info exists for this id LocalData* pLir=AccessLocalData(pckData.networkId); if (pLir!=NULL && pckData.dataSize>=2) { unsigned char opcode=pckData.pData[1]; hash_map::iterator itr; //Check the login server hash map itr=gClientOpcodesTable.find(opcode); if (itr!=gClientOpcodesTable.end()) { //No handler for this opcode if (itr->second.handler==NULL) { gCoreLog.debug("No handler found for %s", itr->second.name); gCoreLog.LogUnknownPacket(pckData.pData, pckData.dataSize); } //If this is the first 5 bytes the client sends. >_> Chop it! >:) else if (opcode==0 && pckData.pData[0]==1) { PacketChop(pckData.pData, &pckData, 5); } //Oh...we have a real packet. =D else { int packet_size=itr->second.handler(&pckData, pLir); PacketChop(pckData.pData, &pckData, packet_size); } } //Unknown packet else { if (pckData.pData!=NULL) { gCoreLog.LogUnknownPacket(pckData.pData, pckData.dataSize); gpFileServer->Kick(pckData.networkId); } } } } //free the memory used by the NetMessage. Deletes the original data. if (pckData.pData!=NULL) { delete [] pckData.pData; } } }