- Joined
- Jun 10, 2009
- Messages
- 659
- Reaction score
- 141
This is the logic how the server reads packets from the client taken from outdated server files released long back for the game Tantra Online. As I am trying to build emulator in C# for a client which is of later version, I would like to understand what exactly this code is doing so that I can code it in C#.
Thanks in advance!
Code:
char* CPSock::ReadClientMessage(int* ErrorCode, int* ErrorType)
{
*ErrorCode = 0;
// Proc°¡ Recv¸¦ ÃÊ°úÇÑ °æ¿ì. (½É°¢ÇÑ ¿À·ù, ³ªÅ¸³¯¼ö ¾ø� °æ¿ì�Ù)
if (nProcPosition >= nRecvPosition) {
nRecvPosition = 0;
nProcPosition = 0;
return 0;
}
// Init packet authentication
if (Init == 0) {
if (nRecvPosition - nProcPosition < 4)
return 0;
int InitCode = *((unsigned int*)(pRecvBuffer + nProcPosition));
if (InitCode != INITCODE) {
*ErrorCode = 2;
*ErrorType = InitCode;
CloseSocket(); //fors_debug ÌØÊâ�¦Àí
return 0;
}
Init = 1;
nProcPosition += 4;
}
// Ccheck received message is larger than HEADER
if (nRecvPosition - nProcPosition < sizeof(HEADER))
return 0;
// Check full message arrived
int sh = sizeof(HEADER);
unsigned short Size = *((unsigned short*)(pRecvBuffer + nProcPosition + 4));
Size = Size + sizeof(HEADER);
unsigned char CheckSum = *((unsigned char*)(pRecvBuffer + nProcPosition + 3));
unsigned int SockType = *((unsigned short*)(pRecvBuffer + nProcPosition));
unsigned int ClentTick = *((unsigned int*)(pRecvBuffer + nProcPosition + 6));
if (Size > MAX_MESSAGE_SIZE || Size < sizeof(HEADER)) {
nRecvPosition = 0;
nProcPosition = 0;
*ErrorCode = 2;
*ErrorType = Size;
return 0;
}
unsigned short Rest = nRecvPosition - nProcPosition;
if (Size > Rest)
return 0;
// Get message
char* pMsg = &(pRecvBuffer[nProcPosition]);
nProcPosition = nProcPosition + Size;
if (nRecvPosition <= nProcPosition) {
nRecvPosition = 0;
nProcPosition = 0;
}
// Compare check_sum in packet
int KeywordFlag = oldRecvChecksum % 2;
unsigned char Sum = 0;
int pos = ucRecvSeq;
for (int i = sizeof(HEADER); i < Size; i++, pos++) {
int rst = pos % 256;
unsigned char Trans = pKeyWord[rst][KeywordFlag];
int mod = i & 0x3;
if (mod == 0)
pMsg[i] = pMsg[i] - (Trans << 2);
if (mod == 1)
pMsg[i] = pMsg[i] + (Trans >> 1);
if (mod == 2)
pMsg[i] = pMsg[i] - (Trans << 1);
if (mod == 3)
pMsg[i] = pMsg[i] + (Trans >> 2);
Sum += pMsg[i];
}
ucRecvSeq++;
oldRecvChecksum = CheckSum;
// return packet, even check_sum not match
if (Sum != CheckSum) {
*ErrorCode = 1;
*ErrorType = Size;
return pMsg;
}
return pMsg;
}
Thanks in advance!