Reworked Charles's Read and Write functions in to a class for use with future

developments.
This commit is contained in:
Josh Allen
2004-08-26 06:40:45 +00:00
parent e76553c9cc
commit d9376563b1
2 changed files with 112 additions and 122 deletions
+91 -96
View File
@@ -95,13 +95,13 @@ void NetworkSyncManager::PostStartUp(CString ServerIP)
// If network play is desired and the connection works, // If network play is desired and the connection works,
// halt until we know what server version we're dealing with // halt until we know what server version we're dealing with
ClearPacket(m_packet); m_packet.ClearPacket();
Write1(m_packet, 2); //Hello Packet m_packet.Write1(2); //Hello Packet
Write1(m_packet, NETPROTOCOLVERSION); m_packet.Write1(NETPROTOCOLVERSION);
WriteNT(m_packet, CString(PRODUCT_NAME_VER)); m_packet.WriteNT(CString(PRODUCT_NAME_VER));
//Block until responce is received //Block until responce is received
//Move mode to blocking in order to give CPU back to the //Move mode to blocking in order to give CPU back to the
@@ -113,21 +113,21 @@ void NetworkSyncManager::PostStartUp(CString ServerIP)
//Following packet must get through, so we block for it. //Following packet must get through, so we block for it.
NetPlayerClient->SendPack((char*)m_packet.Data,m_packet.Position); NetPlayerClient->SendPack((char*)m_packet.Data,m_packet.Position);
ClearPacket(m_packet); m_packet.ClearPacket();
while (dontExit) while (dontExit)
{ {
ClearPacket(m_packet); m_packet.ClearPacket();
if (NetPlayerClient->ReadPack((char *)&m_packet, NETMAXBUFFERSIZE)<1) if (NetPlayerClient->ReadPack((char *)&m_packet, NETMAXBUFFERSIZE)<1)
dontExit=false; // Also allow exit if there is a problem on the socket dontExit=false; // Also allow exit if there is a problem on the socket
if (Read1(m_packet) == (128+2)) if (m_packet.Read1() == (128+2))
dontExit=false; dontExit=false;
//Only allow passing on handshake. //Only allow passing on handshake.
//Otherwise scoreboard updates and such will confuse us. //Otherwise scoreboard updates and such will confuse us.
} }
NetPlayerClient->blocking = false; NetPlayerClient->blocking = false;
m_ServerVersion = Read1(m_packet); m_ServerVersion = m_packet.Read1();
m_ServerName = ReadNT(m_packet); m_ServerName = m_packet.ReadNT();
LOG->Info("Server Version: %d %s", m_ServerVersion, m_ServerName.c_str()); LOG->Info("Server Version: %d %s", m_ServerVersion, m_ServerName.c_str());
} }
@@ -204,21 +204,21 @@ void NetworkSyncManager::ReportScore(int playerID, int step, int score, int comb
if (!useSMserver) //Make sure that we are using the network if (!useSMserver) //Make sure that we are using the network
return; return;
ClearPacket(m_packet); m_packet.ClearPacket();
Write1(m_packet,5); m_packet.Write1(5);
uint8_t ctr = (uint8_t) (playerID * 16 + step - 1); uint8_t ctr = (uint8_t) (playerID * 16 + step - 1);
Write1(m_packet,ctr); m_packet.Write1(ctr);
ctr = uint8_t( g_CurStageStats.GetGrade((PlayerNumber)playerID)*16 ); ctr = uint8_t( g_CurStageStats.GetGrade((PlayerNumber)playerID)*16 );
Write1(m_packet,ctr); m_packet.Write1(ctr);
Write4(m_packet,score); m_packet.Write4(score);
Write2(m_packet, (uint16_t) combo); m_packet.Write2((uint16_t) combo);
Write2(m_packet, (uint16_t) m_playerLife[playerID]); m_packet.Write2((uint16_t) m_playerLife[playerID]);
//Offset Info //Offset Info
//Note: if a 0 is sent, then disregard data. //Note: if a 0 is sent, then disregard data.
@@ -236,7 +236,7 @@ void NetworkSyncManager::ReportScore(int playerID, int step, int score, int comb
if (((step<TNS_BOO)||(step>TNS_MARVELOUS))&&(step!=TNS_HIT_MINE)) if (((step<TNS_BOO)||(step>TNS_MARVELOUS))&&(step!=TNS_HIT_MINE))
iOffset = 0; iOffset = 0;
Write2(m_packet, (uint16_t) iOffset); m_packet.Write2((uint16_t) iOffset);
NetPlayerClient->SendPack((char*)m_packet.Data, m_packet.Position); NetPlayerClient->SendPack((char*)m_packet.Data, m_packet.Position);
@@ -248,9 +248,9 @@ void NetworkSyncManager::ReportSongOver()
if (!useSMserver) //Make sure that we are using the network if (!useSMserver) //Make sure that we are using the network
return ; return ;
ClearPacket(m_packet); m_packet.ClearPacket();
Write1(m_packet,4); m_packet.Write1(4);
NetPlayerClient->SendPack((char*)&m_packet.Data, m_packet.Position); NetPlayerClient->SendPack((char*)&m_packet.Data, m_packet.Position);
return; return;
@@ -260,14 +260,14 @@ void NetworkSyncManager::ReportStyle()
{ {
if (!useSMserver) if (!useSMserver)
return; return;
ClearPacket( m_packet ); m_packet.ClearPacket();
Write1( m_packet, 6 ); m_packet.Write1(6);
Write1( m_packet, (int8_t) GAMESTATE->GetNumPlayersEnabled() ); m_packet.Write1( (int8_t) GAMESTATE->GetNumPlayersEnabled() );
FOREACH_EnabledPlayer( pn ) FOREACH_EnabledPlayer( pn )
{ {
Write1( m_packet,(uint8_t) pn ); m_packet.Write1((uint8_t) pn );
WriteNT( m_packet, GAMESTATE->GetPlayerDisplayName(pn) ); m_packet.WriteNT(GAMESTATE->GetPlayerDisplayName(pn) );
} }
NetPlayerClient->SendPack( (char*)&m_packet.Data, m_packet.Position ); NetPlayerClient->SendPack( (char*)&m_packet.Data, m_packet.Position );
@@ -283,9 +283,9 @@ void NetworkSyncManager::StartRequest(short position)
LOG->Trace("Requesting Start from Server."); LOG->Trace("Requesting Start from Server.");
ClearPacket( m_packet ); m_packet.ClearPacket();
Write1( m_packet,3 ); m_packet.Write1(3 );
unsigned char ctr=0; unsigned char ctr=0;
@@ -298,38 +298,38 @@ void NetworkSyncManager::StartRequest(short position)
if (tSteps!=NULL) if (tSteps!=NULL)
ctr = uint8_t(ctr+tSteps->GetMeter()); ctr = uint8_t(ctr+tSteps->GetMeter());
Write1(m_packet,ctr); m_packet.Write1(ctr);
//Notify server if this is for sync or not. //Notify server if this is for sync or not.
ctr = char(position*16); ctr = char(position*16);
Write1(m_packet,ctr); m_packet.Write1(ctr);
if (GAMESTATE->m_pCurSong !=NULL) { if (GAMESTATE->m_pCurSong !=NULL) {
WriteNT(m_packet,GAMESTATE->m_pCurSong->m_sMainTitle); m_packet.WriteNT(GAMESTATE->m_pCurSong->m_sMainTitle);
WriteNT(m_packet,GAMESTATE->m_pCurSong->m_sSubTitle); m_packet.WriteNT(GAMESTATE->m_pCurSong->m_sSubTitle);
WriteNT(m_packet,GAMESTATE->m_pCurSong->m_sArtist); m_packet.WriteNT(GAMESTATE->m_pCurSong->m_sArtist);
} else { } else {
WriteNT(m_packet,""); m_packet.WriteNT("");
WriteNT(m_packet,""); m_packet.WriteNT("");
WriteNT(m_packet,""); m_packet.WriteNT("");
} }
if (GAMESTATE->m_pCurCourse != NULL) if (GAMESTATE->m_pCurCourse != NULL)
WriteNT(m_packet,GAMESTATE->m_pCurCourse->GetFullDisplayTitle()); m_packet.WriteNT(GAMESTATE->m_pCurCourse->GetFullDisplayTitle());
else else
WriteNT(m_packet,CString("")); m_packet.WriteNT(CString(""));
//Send Player (and song) Options //Send Player (and song) Options
WriteNT(m_packet,GAMESTATE->m_SongOptions.GetString()); m_packet.WriteNT(GAMESTATE->m_SongOptions.GetString());
int players=0; int players=0;
FOREACH_PlayerNumber (p) FOREACH_PlayerNumber (p)
{ {
players++; players++;
WriteNT(m_packet,GAMESTATE->m_PlayerOptions[p].GetString()); m_packet.WriteNT(GAMESTATE->m_PlayerOptions[p].GetString());
} }
for (int i=0;i<2-players;i++) for (int i=0;i<2-players;i++)
WriteNT(m_packet,""); //Write a NULL if no player m_packet.WriteNT(""); //Write a NULL if no player
@@ -345,15 +345,15 @@ void NetworkSyncManager::StartRequest(short position)
LOG->Trace("Waiting for RECV"); LOG->Trace("Waiting for RECV");
ClearPacket(m_packet); m_packet.ClearPacket();
while (dontExit) while (dontExit)
{ {
ClearPacket(m_packet); m_packet.ClearPacket();
if (NetPlayerClient->ReadPack((char *)&m_packet, NETMAXBUFFERSIZE)<1) if (NetPlayerClient->ReadPack((char *)&m_packet, NETMAXBUFFERSIZE)<1)
dontExit=false; // Also allow exit if there is a problem on the socket dontExit=false; // Also allow exit if there is a problem on the socket
if (Read1(m_packet) == (128+3)) if (m_packet.Read1() == (128+3))
dontExit=false; dontExit=false;
//Only allow passing on Start request. //Only allow passing on Start request.
//Otherwise scoreboard updates and such will confuse us. //Otherwise scoreboard updates and such will confuse us.
@@ -402,11 +402,11 @@ void NetworkSyncManager::ProcessInput()
NetPlayerClient->update(); NetPlayerClient->update();
ClearPacket(m_packet); m_packet.ClearPacket();
while (NetPlayerClient->ReadPack((char *)&m_packet, NETMAXBUFFERSIZE)>0) while (NetPlayerClient->ReadPack((char *)&m_packet, NETMAXBUFFERSIZE)>0)
{ {
int command = Read1(m_packet); int command = m_packet.Read1();
//Check to make sure command is valid from server //Check to make sure command is valid from server
if (command<128) if (command<128)
{ {
@@ -418,8 +418,8 @@ void NetworkSyncManager::ProcessInput()
switch (command) { switch (command) {
case 0: //Ping packet responce case 0: //Ping packet responce
ClearPacket(m_packet); m_packet.ClearPacket();
Write1(m_packet,0); m_packet.Write1(0);
NetPlayerClient->SendPack((char*)m_packet.Data,m_packet.Position); NetPlayerClient->SendPack((char*)m_packet.Data,m_packet.Position);
break; break;
case 1: //These are in responce to when/if we send packet 0's case 1: //These are in responce to when/if we send packet 0's
@@ -428,25 +428,25 @@ void NetworkSyncManager::ProcessInput()
case 4: //Undefined case 4: //Undefined
case 5: //Scoreboard Update case 5: //Scoreboard Update
{ //Ease scope { //Ease scope
int ColumnNumber=Read1(m_packet); int ColumnNumber=m_packet.Read1();
int NumberPlayers=Read1(m_packet); int NumberPlayers=m_packet.Read1();
CString ColumnData; CString ColumnData;
int i; int i;
switch (ColumnNumber) { switch (ColumnNumber) {
case 0: case 0:
ColumnData = "Names\n"; ColumnData = "Names\n";
for (i=0;i<NumberPlayers;i++) for (i=0;i<NumberPlayers;i++)
ColumnData += ReadNT(m_packet) + "\n"; ColumnData += m_packet.ReadNT() + "\n";
break; break;
case 1: case 1:
ColumnData = "Combo\n"; ColumnData = "Combo\n";
for (i=0;i<NumberPlayers;i++) for (i=0;i<NumberPlayers;i++)
ColumnData += ssprintf("%d\n",Read2(m_packet)); ColumnData += ssprintf("%d\n",m_packet.Read2());
break; break;
case 2: case 2:
ColumnData = "Grade\n"; ColumnData = "Grade\n";
for (i=0;i<NumberPlayers;i++) for (i=0;i<NumberPlayers;i++)
switch (Read1(m_packet)) { switch (m_packet.Read1()) {
case 0: case 0:
ColumnData+="AAAA\n"; break; ColumnData+="AAAA\n"; break;
case 1: case 1:
@@ -472,7 +472,7 @@ void NetworkSyncManager::ProcessInput()
break; break;
case 6: //System message from server case 6: //System message from server
{ {
CString SysMSG = ReadNT(m_packet); CString SysMSG = m_packet.ReadNT();
SCREENMAN->SystemMessage(SysMSG); SCREENMAN->SystemMessage(SysMSG);
} }
break; break;
@@ -480,7 +480,7 @@ void NetworkSyncManager::ProcessInput()
SCREENMAN->SendMessageToTopScreen(SM_NET_SelectSong); SCREENMAN->SendMessageToTopScreen(SM_NET_SelectSong);
break; break;
} }
ClearPacket(m_packet); m_packet.ClearPacket();
} }
} }
@@ -493,93 +493,88 @@ bool NetworkSyncManager::ChangedScoreboard(int Column)
} }
uint8_t PacketFunctions::Read1()
//Functions to deal with this protocol
uint8_t NetworkSyncManager::Read1(NetPacket &Packet)
{ {
if (Packet.Position>=NETMAXBUFFERSIZE) if (Position>=NETMAXBUFFERSIZE)
return 0; return 0;
return Packet.Data[Packet.Position++]; return Data[Position++];
} }
uint16_t NetworkSyncManager::Read2(NetPacket &Packet) uint16_t PacketFunctions::Read2()
{ {
if (Packet.Position>=NETMAXBUFFERSIZE-1) if (Position>=NETMAXBUFFERSIZE-1)
return 0; return 0;
uint16_t Temp; uint16_t Temp;
memcpy( &Temp, Packet.Data + Packet.Position,2 ); memcpy( &Temp, Data + Position,2 );
Packet.Position+=2; Position+=2;
return ntohs(Temp); return ntohs(Temp);
} }
uint32_t NetworkSyncManager::Read4(NetPacket &Packet) uint32_t PacketFunctions::Read4()
{ {
if (Packet.Position>=NETMAXBUFFERSIZE-3) if (Position>=NETMAXBUFFERSIZE-3)
return 0; return 0;
uint32_t Temp; uint32_t Temp;
memcpy( &Temp, Packet.Data + Packet.Position,4 ); memcpy( &Temp, Data + Position,4 );
Packet.Position+=4; Position+=4;
return ntohl(Temp); return ntohl(Temp);
} }
CString NetworkSyncManager::ReadNT(NetPacket &Packet) CString PacketFunctions::ReadNT()
{ {
//int Orig=Packet.Position; //int Orig=Packet.Position;
CString TempStr; CString TempStr;
while ((Packet.Position<NETMAXBUFFERSIZE)&& (((char*)Packet.Data)[Packet.Position]!=0)) while ((Position<NETMAXBUFFERSIZE)&& (((char*)Data)[Position]!=0))
TempStr= TempStr + (char)Packet.Data[Packet.Position++]; TempStr= TempStr + (char)Data[Position++];
Packet.Position++; Position++;
return TempStr; return TempStr;
} }
void NetworkSyncManager::Write1(NetPacket &Packet, uint8_t Data) void PacketFunctions::Write1(uint8_t data)
{ {
if (Packet.Position>=NETMAXBUFFERSIZE) if (Position>=NETMAXBUFFERSIZE)
return; return;
memcpy( &Packet.Data[Packet.Position], &Data,1 ); memcpy( &Data[Position], &data,1 );
Packet.Position++; Position++;
} }
void NetworkSyncManager::Write2(NetPacket &Packet, uint16_t Data) void PacketFunctions::Write2(uint16_t data)
{ {
if (Packet.Position>=NETMAXBUFFERSIZE-1) if (Position>=NETMAXBUFFERSIZE-1)
return; return;
Data = htons(Data); data = htons(data);
memcpy( &Packet.Data[Packet.Position], &Data,2 ); memcpy( &Data[Position], &data,2 );
Packet.Position+=2; Position+=2;
} }
void NetworkSyncManager::Write4(NetPacket &Packet, uint32_t Data) void PacketFunctions::Write4(uint32_t data)
{ {
if (Packet.Position>=NETMAXBUFFERSIZE-3) if (Position>=NETMAXBUFFERSIZE-3)
return ; return ;
Data = htonl(Data); data = htonl(data);
memcpy( &Packet.Data[Packet.Position], &Data,4 ); memcpy( &Data[Position], &data,4 );
Packet.Position+=4; Position+=4;
} }
void NetworkSyncManager::WriteNT(NetPacket &Packet, CString Data) void PacketFunctions::WriteNT(CString data)
{ {
int index=0; int index=0;
while ((Packet.Position<NETMAXBUFFERSIZE)&&(index<Data.GetLength())) while ((Position<NETMAXBUFFERSIZE)&&(index<data.GetLength()))
Packet.Data[Packet.Position++] = (unsigned char)(Data.c_str()[index++]); Data[Position++] = (unsigned char)(data.c_str()[index++]);
Packet.Data[Packet.Position++] = 0; Data[Position++] = 0;
} }
void PacketFunctions::ClearPacket()
{
memset((void*)(&Data),0, NETMAXBUFFERSIZE);
Position = 0;
}
#endif #endif
+21 -26
View File
@@ -20,6 +20,26 @@ enum NSScoreBoardColumn
class EzSockets; class EzSockets;
class PacketFunctions
{
public:
unsigned char Data[NETMAXBUFFERSIZE]; //Data
int Position; //Other info (Used for following functions)
//Commands used to operate on NetPackets
uint8_t Read1();
uint16_t Read2();
uint32_t Read4();
CString ReadNT();
void Write1(uint8_t Data);
void Write2(uint16_t Data);
void Write4(uint32_t Data);
void WriteNT(CString Data);
void ClearPacket();
};
class NetworkSyncManager class NetworkSyncManager
{ {
public: public:
@@ -77,32 +97,7 @@ private:
bool Listen(unsigned short port); bool Listen(unsigned short port);
PacketFunctions m_packet;
//This is it's own type, so that more info can
//be put at the end of it; after the data.
struct NetPacket
{
unsigned char Data[NETMAXBUFFERSIZE]; //Data
int Position; //Other info
};
//We only want to create this once per instance.
//No need to allocate and deallocate one all the time.
NetPacket m_packet;
//Commands used to operate on NetPackets
uint8_t Read1(NetPacket &Packet);
uint16_t Read2(NetPacket &Packet);
uint32_t Read4(NetPacket &Packet);
CString ReadNT(NetPacket &Packet);
void Write1(NetPacket &Packet, uint8_t Data);
void Write2(NetPacket &Packet, uint16_t Data);
void Write4(NetPacket &Packet, uint32_t Data);
void WriteNT(NetPacket &Packet, CString Data);
inline void ClearPacket(NetPacket &Packet) { memset((void*)(&Packet),0, sizeof(NetPacket)); }
#endif #endif
}; };