Added typedef for the tokens

This commit is contained in:
heinrich5991 2012-03-31 11:50:55 +02:00 committed by oy
parent 66fcf9308c
commit bbf261891c
6 changed files with 33 additions and 31 deletions

View file

@ -87,7 +87,7 @@ int CNetRecvUnpacker::FetchChunk(CNetChunk *pChunk)
} }
// packs the data tight and sends it // packs the data tight and sends it
void CNetBase::SendPacketConnless(NETSOCKET Socket, NETADDR *pAddr, int Version, unsigned int Token, unsigned int ResponseToken, const void *pData, int DataSize) void CNetBase::SendPacketConnless(NETSOCKET Socket, NETADDR *pAddr, int Version, TOKEN Token, TOKEN ResponseToken, const void *pData, int DataSize)
{ {
unsigned char aBuffer[NET_MAX_PACKETSIZE]; unsigned char aBuffer[NET_MAX_PACKETSIZE];
@ -365,7 +365,7 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
} }
void CNetBase::SendControlMsg(NETSOCKET Socket, NETADDR *pAddr, int Version, unsigned int Token, int Ack, int ControlMsg, const void *pExtra, int ExtraSize) void CNetBase::SendControlMsg(NETSOCKET Socket, NETADDR *pAddr, int Version, TOKEN Token, int Ack, int ControlMsg, const void *pExtra, int ExtraSize)
{ {
CNetPacketConstruct Construct; CNetPacketConstruct Construct;
Construct.m_Version = Version; Construct.m_Version = Version;
@ -382,7 +382,7 @@ void CNetBase::SendControlMsg(NETSOCKET Socket, NETADDR *pAddr, int Version, uns
} }
void CNetBase::SendToken(NETSOCKET Socket, NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken) void CNetBase::SendToken(NETSOCKET Socket, NETADDR *pAddr, TOKEN Token, TOKEN ResponseToken)
{ {
char aToken[4]; char aToken[4];
aToken[0] = (Token>>24)&0xff; aToken[0] = (Token>>24)&0xff;

View file

@ -103,6 +103,8 @@ enum
typedef int (*NETFUNC_DELCLIENT)(int ClientID, const char* pReason, void *pUser); typedef int (*NETFUNC_DELCLIENT)(int ClientID, const char* pReason, void *pUser);
typedef int (*NETFUNC_NEWCLIENT)(int ClientID, void *pUser); typedef int (*NETFUNC_NEWCLIENT)(int ClientID, void *pUser);
typedef unsigned int TOKEN;
struct CNetChunk struct CNetChunk
{ {
// -1 means that it's a connless packet // -1 means that it's a connless packet
@ -141,8 +143,8 @@ class CNetPacketConstruct
{ {
public: public:
int m_Version; int m_Version;
unsigned int m_Token; TOKEN m_Token;
unsigned int m_ResponseToken; // only used in connless packets TOKEN m_ResponseToken; // only used in connless packets
int m_Flags; int m_Flags;
int m_Ack; int m_Ack;
int m_NumChunks; int m_NumChunks;
@ -160,11 +162,11 @@ public:
int ProcessMessage(const NETADDR *pAddr, const CNetPacketConstruct *pPacket, bool Notify); int ProcessMessage(const NETADDR *pAddr, const CNetPacketConstruct *pPacket, bool Notify);
bool CheckToken(const NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken, bool Notify); bool CheckToken(const NETADDR *pAddr, TOKEN Token, TOKEN ResponseToken, bool Notify);
unsigned int GenerateToken(const NETADDR *pAddr); TOKEN GenerateToken(const NETADDR *pAddr);
private: private:
static unsigned int GenerateToken(const NETADDR *pAddr, int64 Seed); static TOKEN GenerateToken(const NETADDR *pAddr, int64 Seed);
NETSOCKET m_Socket; NETSOCKET m_Socket;
@ -198,8 +200,8 @@ private:
CNetPacketConstruct m_Construct; CNetPacketConstruct m_Construct;
unsigned int m_Token; TOKEN m_Token;
unsigned int m_PeerToken; TOKEN m_PeerToken;
NETADDR m_PeerAddr; NETADDR m_PeerAddr;
NETSOCKET m_Socket; NETSOCKET m_Socket;
@ -217,14 +219,14 @@ private:
void ResendChunk(CNetChunkResend *pResend); void ResendChunk(CNetChunkResend *pResend);
void Resend(); void Resend();
static unsigned int GenerateToken(const NETADDR *pPeerAddr); static TOKEN GenerateToken(const NETADDR *pPeerAddr);
public: public:
void Init(NETSOCKET Socket, bool BlockCloseMsg); void Init(NETSOCKET Socket, bool BlockCloseMsg);
int Connect(NETADDR *pAddr); int Connect(NETADDR *pAddr);
void Disconnect(const char *pReason); void Disconnect(const char *pReason);
void SetToken(unsigned int Token); void SetToken(TOKEN Token);
int Update(); int Update();
int Flush(); int Flush();
@ -327,8 +329,8 @@ public:
int Close(); int Close();
// the token and version parameter are only used for connless packets // the token and version parameter are only used for connless packets
int Recv(CNetChunk *pChunk, unsigned int *pResponseToken = 0, int *pVersion = 0); int Recv(CNetChunk *pChunk, TOKEN *pResponseToken = 0, int *pVersion = 0);
int Send(CNetChunk *pChunk, unsigned int Token = NET_TOKEN_NONE, int Version = NET_PACKETVERSION); int Send(CNetChunk *pChunk, TOKEN Token = NET_TOKEN_NONE, int Version = NET_PACKETVERSION);
int Update(); int Update();
// //
@ -405,8 +407,8 @@ public:
int Connect(NETADDR *Addr); int Connect(NETADDR *Addr);
// communication // communication
int Recv(CNetChunk *pChunk, unsigned int *pResponseToken = 0, int *pVersion = 0); int Recv(CNetChunk *pChunk, TOKEN *pResponseToken = 0, int *pVersion = 0);
int Send(CNetChunk *pChunk, unsigned int Token = NET_TOKEN_NONE, int Version = NET_PACKETVERSION); int Send(CNetChunk *pChunk, TOKEN Token = NET_TOKEN_NONE, int Version = NET_PACKETVERSION);
// pumping // pumping
int Update(); int Update();
@ -436,9 +438,9 @@ public:
static int Compress(const void *pData, int DataSize, void *pOutput, int OutputSize); static int Compress(const void *pData, int DataSize, void *pOutput, int OutputSize);
static int Decompress(const void *pData, int DataSize, void *pOutput, int OutputSize); static int Decompress(const void *pData, int DataSize, void *pOutput, int OutputSize);
static void SendControlMsg(NETSOCKET Socket, NETADDR *pAddr, int Version, unsigned int Token, int Ack, int ControlMsg, const void *pExtra, int ExtraSize); static void SendControlMsg(NETSOCKET Socket, NETADDR *pAddr, int Version, TOKEN Token, int Ack, int ControlMsg, const void *pExtra, int ExtraSize);
static void SendToken(NETSOCKET Socket, NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken = NET_TOKEN_NONE); static void SendToken(NETSOCKET Socket, NETADDR *pAddr, TOKEN Token, TOKEN ResponseToken = NET_TOKEN_NONE);
static void SendPacketConnless(NETSOCKET Socket, NETADDR *pAddr, int Version, unsigned int Token, unsigned int ResponseToken, const void *pData, int DataSize); static void SendPacketConnless(NETSOCKET Socket, NETADDR *pAddr, int Version, TOKEN Token, TOKEN ResponseToken, const void *pData, int DataSize);
static void SendPacket(NETSOCKET Socket, NETADDR *pAddr, CNetPacketConstruct *pPacket); static void SendPacket(NETSOCKET Socket, NETADDR *pAddr, CNetPacketConstruct *pPacket);
static int UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct *pPacket); static int UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct *pPacket);

View file

@ -59,7 +59,7 @@ int CNetClient::ResetErrorString()
return 0; return 0;
} }
int CNetClient::Recv(CNetChunk *pChunk, unsigned int *pResponseToken, int *pVersion) int CNetClient::Recv(CNetChunk *pChunk, TOKEN *pResponseToken, int *pVersion)
{ {
while(1) while(1)
{ {
@ -115,7 +115,7 @@ int CNetClient::Recv(CNetChunk *pChunk, unsigned int *pResponseToken, int *pVers
return 0; return 0;
} }
int CNetClient::Send(CNetChunk *pChunk, unsigned int Token, int Version) int CNetClient::Send(CNetChunk *pChunk, TOKEN Token, int Version)
{ {
if(pChunk->m_Flags&NETSENDFLAG_CONNLESS) if(pChunk->m_Flags&NETSENDFLAG_CONNLESS)
{ {

View file

@ -31,7 +31,7 @@ void CNetConnection::Reset()
mem_zero(&m_Construct, sizeof(m_Construct)); mem_zero(&m_Construct, sizeof(m_Construct));
} }
void CNetConnection::SetToken(unsigned int Token) void CNetConnection::SetToken(TOKEN Token)
{ {
if(State() != NET_CONNSTATE_OFFLINE) if(State() != NET_CONNSTATE_OFFLINE)
return; return;
@ -39,7 +39,7 @@ void CNetConnection::SetToken(unsigned int Token)
m_Token = Token; m_Token = Token;
} }
unsigned int CNetConnection::GenerateToken(const NETADDR *pPeerAddr) TOKEN CNetConnection::GenerateToken(const NETADDR *pPeerAddr)
{ {
return ((rand() & 0xffff) << 16) | (rand() & 0xffff); return ((rand() & 0xffff) << 16) | (rand() & 0xffff);
} }

View file

@ -95,7 +95,7 @@ int CNetServer::Update()
/* /*
TODO: chopp up this function into smaller working parts TODO: chopp up this function into smaller working parts
*/ */
int CNetServer::Recv(CNetChunk *pChunk, unsigned int *pResponseToken, int *pVersion) int CNetServer::Recv(CNetChunk *pChunk, TOKEN *pResponseToken, int *pVersion)
{ {
while(1) while(1)
{ {
@ -231,7 +231,7 @@ int CNetServer::Recv(CNetChunk *pChunk, unsigned int *pResponseToken, int *pVers
return 0; return 0;
} }
int CNetServer::Send(CNetChunk *pChunk, unsigned int Token, int Version) int CNetServer::Send(CNetChunk *pChunk, TOKEN Token, int Version)
{ {
if(pChunk->m_Flags&NETSENDFLAG_CONNLESS) if(pChunk->m_Flags&NETSENDFLAG_CONNLESS)
{ {

View file

@ -9,10 +9,10 @@
#include "network.h" #include "network.h"
static unsigned int Hash(char *pData, int Size) static TOKEN Hash(char *pData, int Size)
{ {
md5_state_t State; md5_state_t State;
unsigned int Result; TOKEN Result;
md5_byte_t aDigest[sizeof(Result)]; md5_byte_t aDigest[sizeof(Result)];
md5_init(&State); md5_init(&State);
@ -70,12 +70,12 @@ void CNetTokenManager::GenerateSeed()
} }
} }
unsigned int CNetTokenManager::GenerateToken(const NETADDR *pAddr) TOKEN CNetTokenManager::GenerateToken(const NETADDR *pAddr)
{ {
return GenerateToken(pAddr, m_Seed); return GenerateToken(pAddr, m_Seed);
} }
unsigned int CNetTokenManager::GenerateToken(const NETADDR *pAddr, int64 Seed) TOKEN CNetTokenManager::GenerateToken(const NETADDR *pAddr, int64 Seed)
{ {
char aBuf[sizeof(NETADDR) + sizeof(int64)]; char aBuf[sizeof(NETADDR) + sizeof(int64)];
int Result; int Result;
@ -89,9 +89,9 @@ unsigned int CNetTokenManager::GenerateToken(const NETADDR *pAddr, int64 Seed)
return Result; return Result;
} }
bool CNetTokenManager::CheckToken(const NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken, bool Notify) bool CNetTokenManager::CheckToken(const NETADDR *pAddr, TOKEN Token, TOKEN ResponseToken, bool Notify)
{ {
unsigned int CurrentToken = GenerateToken(pAddr, m_Seed); TOKEN CurrentToken = GenerateToken(pAddr, m_Seed);
if(CurrentToken == Token) if(CurrentToken == Token)
return true; return true;