Saner packet parsing loop.

This commit is contained in:
q3k 2012-11-22 20:01:21 +01:00
parent 4fb9aa48f9
commit a8245bb6bd
5 changed files with 46 additions and 28 deletions

View file

@ -14,6 +14,7 @@ CClientConnection::CClientConnection(TSocket Socket)
m_Buffer = new unsigned char[4096];
m_BufferPosition = 0;
m_CurrentPacket = 0;
m_InPacket = false;
}
TSocket CClientConnection::GetSocket(void)
@ -34,32 +35,45 @@ void CClientConnection::FillReadBuffer(unsigned char *Data, unsigned int Length)
int CClientConnection::Communicate(void)
{
switch (m_State)
if (!m_InPacket)
{
case CONNECTED:
char PacketID = m_Buffer[0];
switch (m_State)
{
if (m_CurrentPacket == 0)
case CONNECTED:
{
char PacketID = m_Buffer[0];
if (PacketID == 2)
m_CurrentPacket = new X02Handshake(m_Buffer + 1);
else
if (PacketID != 2)
{
LOG("Error: Unexpected packet ID %i on CONNECT state.\n", PacketID);
return -1;
}
m_CurrentPacket = new X02Handshake(m_Buffer + 1);
m_InPacket = true;
}
}
}
else
{
// continue packet
m_CurrentPacket->SetDataLength(m_BufferPosition);
int Result = m_CurrentPacket->Process();
if (Result != -1)
{
// packet received? logic time!
switch (m_CurrentPacket->GetPacketID())
{
// HANDSHAKE
case 2:
{
LOG("Got handshake for protocol version %i\n", ((X02Handshake *)m_CurrentPacket)->GetProtocolVersion());
((X02Handshake *)m_CurrentPacket)->SetDataLength(m_BufferPosition);
int Result =((X02Handshake *)m_CurrentPacket)->Process();
if (Result == -1)
break;
LOG("Got handshake for protocol version %i\n", ((X02Handshake *)m_CurrentPacket)->GetProtocolVersion());
char Temp[256];
UTF16ToASCIIDumb(((X02Handshake *)m_CurrentPacket)->GetUsername(), Temp);
LOG(" Username: %s\n", Temp);
char Temp[256];
UTF16ToASCIIDumb(((X02Handshake *)m_CurrentPacket)->GetUsername(), Temp);
LOG(" Username: %s\n", Temp);
break;
m_InPacket = false;
}
}
}
}
return 0;

View file

@ -1,6 +1,7 @@
#ifndef __CLIENT_CONNECTION_H__
#define __CLIENT_CONNECTION_H__
#include "packets/GenericPacket.h"
#include "config.h"
namespace umcs {
@ -18,7 +19,8 @@ namespace umcs {
};
class CClientConnection {
private:
void *m_CurrentPacket;
CGenericPacket *m_CurrentPacket;
bool m_InPacket;
protected:
TSocket m_Socket;
EClientConnectionState m_State;

View file

@ -4,7 +4,7 @@ using namespace umcs;
#include "../config.h"
#include <cstring>
GenericPacket::GenericPacket(unsigned char *Buffer)
CGenericPacket::CGenericPacket(unsigned char *Buffer)
{
m_Buffer = Buffer;
m_CurrentByte = 0;
@ -12,12 +12,12 @@ GenericPacket::GenericPacket(unsigned char *Buffer)
m_CurrentField = 0;
}
void GenericPacket::SetDataLength(unsigned int Length)
void CGenericPacket::SetDataLength(unsigned int Length)
{
m_AvailableDataLength = Length;
}
int GenericPacket::Process(void)
int CGenericPacket::Process(void)
{
for (unsigned int i = m_CurrentField; i < GetNumFields(); i++)
{
@ -33,12 +33,12 @@ int GenericPacket::Process(void)
return (m_AvailableDataLength - m_CurrentByte);
}
char GenericPacket::PeekByte(void)
char CGenericPacket::PeekByte(void)
{
return (char)m_Buffer[m_CurrentByte];
}
bool GenericPacket::ReadByte(char &Byte)
bool CGenericPacket::ReadByte(char &Byte)
{
if (GetAvailableBytes() < 1)
return false;
@ -48,7 +48,7 @@ bool GenericPacket::ReadByte(char &Byte)
return true;
}
bool GenericPacket::ReadInteger(int &Integer)
bool CGenericPacket::ReadInteger(int &Integer)
{
if (GetAvailableBytes() < 4)
return false;
@ -65,7 +65,7 @@ bool GenericPacket::ReadInteger(int &Integer)
return true;
}
bool GenericPacket::ReadWString(unsigned short * &WString)
bool CGenericPacket::ReadWString(unsigned short * &WString)
{
// fuck you (notch|java) for making the prefix count the number of characters, not bytes
if (GetAvailableBytes() < 2)

View file

@ -2,7 +2,7 @@
#define __GENERIC_PACKET_H__
namespace umcs {
class GenericPacket {
class CGenericPacket {
private:
unsigned char *m_Buffer;
unsigned int m_CurrentByte;
@ -21,9 +21,10 @@ namespace umcs {
bool ReadInteger(int &Integer);
bool ReadWString(unsigned short * &WString);
public:
GenericPacket(unsigned char *Buffer);
CGenericPacket(unsigned char *Buffer);
void SetDataLength(unsigned int Length);
int Process(void);
virtual int GetPacketID(void) { return -1; }
};
};

View file

@ -4,7 +4,7 @@
#include "GenericPacket.h"
namespace umcs {
class X02Handshake : public GenericPacket {
class X02Handshake : public CGenericPacket {
private:
char m_ProtocolVersion;
unsigned short *m_Username;
@ -14,7 +14,8 @@ namespace umcs {
unsigned int GetNumFields(void) { return 4; };
bool ReadField(unsigned int Field);
public:
X02Handshake(unsigned char *Buffer) : GenericPacket(Buffer) { }
X02Handshake(unsigned char *Buffer) : CGenericPacket(Buffer) { }
virtual int GetPacketID(void) { return 2; }
char GetProtocolVersion(void) { return m_ProtocolVersion; }
unsigned short *GetUsername(void) { return m_Username; }