Cleanup. Please try to follow the style of the rest of the project next time.

This commit is contained in:
Steve Checkoway
2004-09-06 20:37:12 +00:00
parent bc628082ba
commit adc7b5e364
2 changed files with 284 additions and 223 deletions
+181 -115
View File
@@ -1,10 +1,12 @@
/* NetworkSyncServer.cpp /* NetworkSyncServer.cpp
*/ */
#include "global.h"
#include "NetworkSyncServer.h" #include "NetworkSyncServer.h"
#include "RageLog.h" #include "RageLog.h"
#include <time.h>
LanPlayer::LanPlayer() { LanPlayer::LanPlayer()
{
score = 0; score = 0;
health = 0; health = 0;
feet = 0; feet = 0;
@@ -17,21 +19,25 @@ LanPlayer::LanPlayer() {
options = ""; options = "";
} }
StepManiaLanServer::StepManiaLanServer() { StepManiaLanServer::StepManiaLanServer()
{
stop = true; stop = true;
SecondSameSelect = false; SecondSameSelect = false;
AssignPlayerIDs(); AssignPlayerIDs();
} }
StepManiaLanServer::~StepManiaLanServer() { StepManiaLanServer::~StepManiaLanServer()
{
ServerStop(); ServerStop();
} }
bool StepManiaLanServer::ServerStart() { bool StepManiaLanServer::ServerStart()
{
server.blocking = 0; /* Turn off blocking */ server.blocking = 0; /* Turn off blocking */
if (server.create()) if (server.create())
if (server.bind(8765)) if (server.bind(8765))
if (server.listen()) { if (server.listen())
{
stop = false; stop = false;
statsTime = time(NULL); statsTime = time(NULL);
return true; return true;
@@ -48,9 +54,9 @@ bool StepManiaLanServer::ServerStart() {
return false; return false;
} }
void StepManiaLanServer::ServerStop() { void StepManiaLanServer::ServerStop()
int x; {
for (x = 0; x < NUMBERCLIENTS; x++) { for (int x = 0; x < NUMBERCLIENTS; ++x) {
Client[x].clientSocket.close(); Client[x].clientSocket.close();
Client[x].Used = false; Client[x].Used = false;
} }
@@ -58,12 +64,15 @@ void StepManiaLanServer::ServerStop() {
stop = true; stop = true;
} }
void StepManiaLanServer::ServerUpdate() { void StepManiaLanServer::ServerUpdate()
if (!stop) { {
if (!stop)
{
NewClientCheck(); /* See if there is another client wanting to play */ NewClientCheck(); /* See if there is another client wanting to play */
UpdateClients(); UpdateClients();
// CheckReady(); // CheckReady();
if (time(NULL) > statsTime) { if (time(NULL) > statsTime)
{
SendStatsToClients(); SendStatsToClients();
statsTime = time(NULL); statsTime = time(NULL);
} }
@@ -71,7 +80,8 @@ void StepManiaLanServer::ServerUpdate() {
} }
} }
void StepManiaLanServer::UpdateClients() { void StepManiaLanServer::UpdateClients()
{
/* Go through all the clients and check to see if it is being used. /* Go through all the clients and check to see if it is being used.
If so then try to get a backet and parse the data. */ If so then try to get a backet and parse the data. */
for (int x = 0; x < NUMBERCLIENTS; x++) for (int x = 0; x < NUMBERCLIENTS; x++)
@@ -80,7 +90,8 @@ void StepManiaLanServer::UpdateClients() {
ParseData(Packet, x); ParseData(Packet, x);
} }
GameClient::GameClient() { GameClient::GameClient()
{
Used = 0; Used = 0;
GotStartRequest = 0; GotStartRequest = 0;
clientSocket.blocking = 0; clientSocket.blocking = 0;
@@ -94,7 +105,8 @@ GameClient::GameClient() {
wasIngame = false; wasIngame = false;
} }
int GameClient::GetData(PacketFunctions &Packet) { int GameClient::GetData(PacketFunctions& Packet)
{
int length = -1; int length = -1;
CheckConnection(); CheckConnection();
Packet.ClearPacket(); Packet.ClearPacket();
@@ -102,9 +114,11 @@ int GameClient::GetData(PacketFunctions &Packet) {
return length; return length;
} }
void StepManiaLanServer::ParseData(PacketFunctions &Packet, int clientNum) { void StepManiaLanServer::ParseData(PacketFunctions& Packet, int clientNum)
{
int command = Packet.Read1(); int command = Packet.Read1();
switch (command) { switch (command)
{
case 0: case 0:
// No Operation // No Operation
SendValue(129, clientNum); SendValue(129, clientNum);
@@ -153,7 +167,8 @@ void StepManiaLanServer::ParseData(PacketFunctions &Packet, int clientNum) {
} }
} }
void StepManiaLanServer::Hello(PacketFunctions &Packet, int clientNum) { void StepManiaLanServer::Hello(PacketFunctions& Packet, int clientNum)
{
int ClientVersion = Packet.Read1(); int ClientVersion = Packet.Read1();
CString build = Packet.ReadNT(); CString build = Packet.ReadNT();
@@ -171,22 +186,26 @@ void StepManiaLanServer::Hello(PacketFunctions &Packet, int clientNum) {
} }
void GameClient::StyleUpdate(PacketFunctions &Packet) { void GameClient::StyleUpdate(PacketFunctions& Packet)
{
int playernumber = 0; int playernumber = 0;
Player[0].name = Player[1].name = ""; Player[0].name = Player[1].name = "";
twoPlayers = Packet.Read1()-1; twoPlayers = Packet.Read1()-1;
for (int x = 0; x < twoPlayers+1; x++) { for (int x = 0; x < twoPlayers+1; x++)
{
playernumber = Packet.Read1(); playernumber = Packet.Read1();
Player[playernumber].name = Packet.ReadNT(); Player[playernumber].name = Packet.ReadNT();
} }
} }
void GameClient::SetClientVersion(int ver, CString b) { void GameClient::SetClientVersion(int ver, const CString& b)
{
version = ver; version = ver;
build = b; build = b;
} }
void GameClient::StartRequest(PacketFunctions &Packet) { void GameClient::StartRequest(PacketFunctions& Packet)
{
int firstbyte = Packet.Read1(); int firstbyte = Packet.Read1();
int secondbyte = Packet.Read1(); int secondbyte = Packet.Read1();
int thirdbyte = Packet.Read1(); int thirdbyte = Packet.Read1();
@@ -205,31 +224,34 @@ void GameClient::StartRequest(PacketFunctions &Packet) {
gameInfo.artist = Packet.ReadNT(); gameInfo.artist = Packet.ReadNT();
gameInfo.course = Packet.ReadNT(); gameInfo.course = Packet.ReadNT();
for (int x = 0; x < 2; x++) { for (int x = 0; x < 2; ++x)
{
Player[x].score = 0; Player[x].score = 0;
Player[x].combo = 0; Player[x].combo = 0;
Player[x].projgrade = 0; Player[x].projgrade = 0;
Player[x].maxCombo = 0; Player[x].maxCombo = 0;
memset((void*)Player[x].steps, 0, sizeof(int)*9); memset(Player[x].steps, 0, sizeof(int)*9);
} }
GotStartRequest = true; GotStartRequest = true;
// InGame = true; // InGame = true;
} }
void StepManiaLanServer::CheckReady() { void StepManiaLanServer::CheckReady()
{
bool canStart = true; bool canStart = true;
int x; int x;
//Only check clients that are starting (after ScreenNetMusicSelect before InGame). //Only check clients that are starting (after ScreenNetMusicSelect before InGame).
for (x = 0; (x < NUMBERCLIENTS)&&(canStart == true); x++) for (x = 0; (x < NUMBERCLIENTS)&&(canStart == true); ++x)
if (Client[x].Used == true) if (Client[x].Used == true)
if (Client[x].isStarting) if (Client[x].isStarting)
if (Client[x].GotStartRequest == false) if (Client[x].GotStartRequest == false)
canStart = false; canStart = false;
if (canStart == true) { if (canStart == true)
{
//(Test this) //(Test this)
//For whatever reason we need to pause in a way //For whatever reason we need to pause in a way
//that will not use a lot of CPU. //that will not use a lot of CPU.
@@ -238,13 +260,16 @@ void StepManiaLanServer::CheckReady() {
usleep ( 2000000 ); usleep ( 2000000 );
//Only start clients waiting for start between ScreenNetMusicSelect and game. //Only start clients waiting for start between ScreenNetMusicSelect and game.
for (x = 0; x < NUMBERCLIENTS; x++) { for (x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].Used == true) {
if (Client[x].isStarting) { if (Client[x].Used)
if (Client[x].isStarting)
{
Client[x].clientSocket.blocking = true; Client[x].clientSocket.blocking = true;
SendValue(131, x); SendValue(131, x);
Client[x].GotStartRequest = false; Client[x].GotStartRequest = false;
if (Client[x].startPosition == 1) { if (Client[x].startPosition == 1)
{
Client[x].isStarting = false; Client[x].isStarting = false;
Client[x].InGame = true; Client[x].InGame = true;
//After we start the clients, clear each client's hasSong. //After we start the clients, clear each client's hasSong.
@@ -257,7 +282,8 @@ void StepManiaLanServer::CheckReady() {
} }
void StepManiaLanServer::GameOver(PacketFunctions &Packet, int clientNum) { void StepManiaLanServer::GameOver(PacketFunctions& Packet, int clientNum)
{
bool allOver = true; bool allOver = true;
int x; int x;
@@ -265,37 +291,39 @@ void StepManiaLanServer::GameOver(PacketFunctions &Packet, int clientNum) {
Client[clientNum].InGame = false; Client[clientNum].InGame = false;
Client[clientNum].wasIngame = true; Client[clientNum].wasIngame = true;
for (x = 0; (x < NUMBERCLIENTS)&&(allOver == true); x++) for (x = 0; (x < NUMBERCLIENTS)&&(allOver == true); ++x)
if (Client[x].Used == true) if (Client[x].Used == true)
if ((Client[x].InGame == true)) if ((Client[x].InGame == true))
allOver = false; allOver = false;
//Wait untill everyone is done before sending //Wait untill everyone is done before sending
if (allOver) { if (allOver)
{
numPlayers = SortStats(playersPtr); numPlayers = SortStats(playersPtr);
Reply.ClearPacket(); Reply.ClearPacket();
Reply.Write1( 4+128 ); Reply.Write1( 4+128 );
Reply.Write1( (uint8_t) numPlayers ); Reply.Write1( (uint8_t) numPlayers );
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.Write1((uint8_t)playersPtr[x]->PlayerID); Reply.Write1((uint8_t)playersPtr[x]->PlayerID);
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.Write4(playersPtr[x]->score); Reply.Write4(playersPtr[x]->score);
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.Write1( (uint8_t) playersPtr[x]->projgrade ); Reply.Write1( (uint8_t) playersPtr[x]->projgrade );
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.Write1( (uint8_t) playersPtr[x]->diff ); Reply.Write1( (uint8_t) playersPtr[x]->diff );
for (int y = 6; y >= 1; y--) for (int y = 6; y >= 1; --y)
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.Write2( (uint16_t) playersPtr[x]->steps[y] ); Reply.Write2( (uint16_t) playersPtr[x]->steps[y] );
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.Write2( (uint16_t) playersPtr[x]->steps[8] ); //Tack on OK Reply.Write2( (uint16_t) playersPtr[x]->steps[8] ); //Tack on OK
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.Write2( (uint16_t) playersPtr[x]->maxCombo ); Reply.Write2( (uint16_t) playersPtr[x]->maxCombo );
for (x = 0; x < numPlayers; x++) for (x = 0; x < numPlayers; ++x)
Reply.WriteNT( playersPtr[x]->options ); Reply.WriteNT( playersPtr[x]->options );
for (x = 0; x < NUMBERCLIENTS; x++) for (x = 0; x < NUMBERCLIENTS; ++x)
if(Client[x].wasIngame) { if(Client[x].wasIngame)
{
SendNetPacket(x, Reply); SendNetPacket(x, Reply);
Client[x].wasIngame = false; Client[x].wasIngame = false;
} }
@@ -304,36 +332,40 @@ void StepManiaLanServer::GameOver(PacketFunctions &Packet, int clientNum) {
} }
} }
void StepManiaLanServer::AssignPlayerIDs() { void StepManiaLanServer::AssignPlayerIDs()
{
int counter = 0; int counter = 0;
//Future: Figure out how to do dynamic numbering. //Future: Figure out how to do dynamic numbering.
for (int x = 0; x < NUMBERCLIENTS; x++) for (int x = 0; x < NUMBERCLIENTS; ++x)
for(int y = 0; y < 2; y++) for(int y = 0; y < 2; ++y)
Client[x].Player[y].PlayerID = counter++; Client[x].Player[y].PlayerID = counter++;
} }
int StepManiaLanServer::SortStats(LanPlayer *playersPtr[]) { int StepManiaLanServer::SortStats(LanPlayer *playersPtr[])
{
int counter = 0; int counter = 0;
LanPlayer *tmp; LanPlayer *tmp;
StatsNameChange = false; //Set it to no name so stats will not send if there is not a change StatsNameChange = false; //Set it to no name so stats will not send if there is not a change
bool allZero = true; bool allZero = true;
//Populate with in game players only //Populate with in game players only
for (int x = 0; x < NUMBERCLIENTS; x++) for (int x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].Used == true) if (Client[x].Used == true)
if (Client[x].InGame||Client[x].wasIngame) if (Client[x].InGame||Client[x].wasIngame)
for (int y = 0; y < 2; y++) for (int y = 0; y < 2; ++y)
if (Client[x].IsPlaying(y)) if (Client[x].IsPlaying(y))
playersPtr[counter++] = &Client[x].Player[y]; playersPtr[counter++] = &Client[x].Player[y];
for (int x = 0; x < counter; x++) { for (int x = 0; x < counter; ++x)
{
//See if all the players have zero. Used to send names if everyone is 0 //See if all the players have zero. Used to send names if everyone is 0
if ((playersPtr[x]->score > 0)&&(allZero == true)) if ((playersPtr[x]->score > 0) && allZero)
allZero = false; allZero = false;
if ((x+1) < counter) if ((x+1) < counter)
if ((playersPtr[x]->score) < (playersPtr[x+1]->score)) { if ((playersPtr[x]->score) < (playersPtr[x+1]->score))
{
tmp = playersPtr[x]; tmp = playersPtr[x];
playersPtr[x] = playersPtr[x+1]; playersPtr[x] = playersPtr[x+1];
playersPtr[x+1] = tmp; playersPtr[x+1] = tmp;
@@ -344,21 +376,22 @@ int StepManiaLanServer::SortStats(LanPlayer *playersPtr[]) {
} }
//If there are players all at 0, the name lsit won't be updated //If there are players all at 0, the name lsit won't be updated
//this make sure it is sent to the server eventually //this make sure it is sent to the server eventually
if ((StatsNameChange == false)&&(allZero == true)) if (!StatsNameChange && allZero)
StatsNameChange = true; StatsNameChange = true;
return counter; return counter;
} }
void StepManiaLanServer::SendStatsToClients() { void StepManiaLanServer::SendStatsToClients()
{
int x; int x;
numPlayers = SortStats(playersPtr); //Return number of players numPlayers = SortStats(playersPtr); //Return number of players
//If there was a chagne in the name data, send it to the clients. //If there was a chagne in the name data, send it to the clients.
//Used to save bandwidth and some processing time. //Used to save bandwidth and some processing time.
if (StatsNameChange) { if (StatsNameChange)
{
/* Write and Send name packet */ /* Write and Send name packet */
Reply.ClearPacket(); Reply.ClearPacket();
Reply.Write1(133); Reply.Write1(133);
@@ -367,8 +400,8 @@ void StepManiaLanServer::SendStatsToClients() {
StatsNameColumn(Reply, playersPtr, numPlayers); StatsNameColumn(Reply, playersPtr, numPlayers);
//Send to in game clients only. //Send to in game clients only.
for (x = 0; x < NUMBERCLIENTS; x++) for (x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].InGame == true) if (Client[x].InGame)
SendNetPacket(x, Reply); SendNetPacket(x, Reply);
} }
@@ -382,8 +415,8 @@ void StepManiaLanServer::SendStatsToClients() {
StatsComboColumn(Reply, playersPtr, numPlayers); StatsComboColumn(Reply, playersPtr, numPlayers);
//Send to in game clients only. //Send to in game clients only.
for (x = 0; x < NUMBERCLIENTS; x++) for (x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].InGame == true) if (Client[x].InGame)
SendNetPacket(x, Reply); SendNetPacket(x, Reply);
@@ -398,36 +431,41 @@ void StepManiaLanServer::SendStatsToClients() {
StatsProjgradeColumn(Reply, playersPtr, numPlayers); StatsProjgradeColumn(Reply, playersPtr, numPlayers);
//Send to in game clients only. //Send to in game clients only.
for (x = 0; x < NUMBERCLIENTS; x++) for (x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].InGame == true) if (Client[x].InGame)
SendNetPacket(x, Reply); SendNetPacket(x, Reply);
} }
void StepManiaLanServer::SendNetPacket(int client, PacketFunctions &Packet) { void StepManiaLanServer::SendNetPacket(int client, PacketFunctions& Packet)
{
/* If there is a connected client where we want to send then do so */ /* If there is a connected client where we want to send then do so */
if (Client[client].Used == true) if (Client[client].Used)
Client[client].clientSocket.SendPack((char*)Packet.Data, Packet.Position); Client[client].clientSocket.SendPack((char*)Packet.Data, Packet.Position);
} }
void StepManiaLanServer::StatsNameColumn(PacketFunctions &data, LanPlayer *playersPtr[], int numPlayers) { void StepManiaLanServer::StatsNameColumn(PacketFunctions &data, LanPlayer *playersPtr[], int numPlayers)
{
CString numname; CString numname;
for (int x = 0; x < numPlayers; x++) for (int x = 0; x < numPlayers; ++x)
data.Write1( (uint8_t) playersPtr[x]->PlayerID ); data.Write1( (uint8_t) playersPtr[x]->PlayerID );
} }
void StepManiaLanServer::StatsComboColumn(PacketFunctions &data, LanPlayer *playersPtr[], int numPlayers) { void StepManiaLanServer::StatsComboColumn(PacketFunctions &data, LanPlayer *playersPtr[], int numPlayers)
for( int x = 0; x < numPlayers; x++ ) {
for( int x = 0; x < numPlayers; ++x )
data.Write2( (uint16_t) playersPtr[x]->combo); data.Write2( (uint16_t) playersPtr[x]->combo);
} }
void StepManiaLanServer::StatsProjgradeColumn(PacketFunctions &data, LanPlayer *playersPtr[], int numPlayers) { void StepManiaLanServer::StatsProjgradeColumn(PacketFunctions& data, LanPlayer *playersPtr[], int numPlayers)
for( int x = 0; x < numPlayers; x++ ) {
for( int x = 0; x < numPlayers; ++x )
data.Write1( (uint8_t) playersPtr[x]->projgrade ); data.Write1( (uint8_t) playersPtr[x]->projgrade );
} }
bool GameClient::IsPlaying(int x) { bool GameClient::IsPlaying(int x)
{
/* If the feet setting is above 0, there must be a player. */ /* If the feet setting is above 0, there must be a player. */
if (Player[x].feet > 0) if (Player[x].feet > 0)
return true; return true;
@@ -435,7 +473,8 @@ bool GameClient::IsPlaying(int x) {
return false; return false;
} }
void GameClient::UpdateStats(PacketFunctions &Packet) { void GameClient::UpdateStats(PacketFunctions& Packet)
{
/* Get the Stats form a packet */ /* Get the Stats form a packet */
char firstbyte = Packet.Read1(); char firstbyte = Packet.Read1();
char secondbyte = Packet.Read1(); char secondbyte = Packet.Read1();
@@ -454,7 +493,8 @@ void GameClient::UpdateStats(PacketFunctions &Packet) {
Player[pID].steps[Player[pID].currstep]++; Player[pID].steps[Player[pID].currstep]++;
} }
void StepManiaLanServer::NewClientCheck() { void StepManiaLanServer::NewClientCheck()
{
/* Find the first available empty client. Then stick a new connection /* Find the first available empty client. Then stick a new connection
in that client socket.*/ in that client socket.*/
int CurrentEmptyClient = FindEmptyClient(); int CurrentEmptyClient = FindEmptyClient();
@@ -465,11 +505,13 @@ void StepManiaLanServer::NewClientCheck() {
} }
void StepManiaLanServer::SendValue(Uint8 value, int clientNum) { void StepManiaLanServer::SendValue(Uint8 value, int clientNum)
{
Client[clientNum].clientSocket.SendPack((char*)&value, sizeof(Uint8)); Client[clientNum].clientSocket.SendPack((char*)&value, sizeof(Uint8));
} }
void StepManiaLanServer::RelayChat(PacketFunctions &Packet, int clientNum) { void StepManiaLanServer::RelayChat(PacketFunctions& Packet, int clientNum)
{
Reply.ClearPacket(); Reply.ClearPacket();
CString message = ""; CString message = "";
@@ -489,12 +531,15 @@ void StepManiaLanServer::RelayChat(PacketFunctions &Packet, int clientNum) {
SendToAllClients(Reply); SendToAllClients(Reply);
} }
void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) { void StepManiaLanServer::SelectSong(PacketFunctions& Packet, int clientNum)
{
int use = Packet.Read1(); int use = Packet.Read1();
CString message; CString message;
if (use == 2) { if (use == 2)
if (clientNum == 0) { {
if (clientNum == 0)
{
SecondSameSelect = false; SecondSameSelect = false;
CurrentSongInfo.title = Packet.ReadNT(); CurrentSongInfo.title = Packet.ReadNT();
@@ -509,7 +554,7 @@ void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) {
Reply.WriteNT(CurrentSongInfo.subtitle); Reply.WriteNT(CurrentSongInfo.subtitle);
//Only send data to clients currently in ScreenNetMusicSelect //Only send data to clients currently in ScreenNetMusicSelect
for (int x = 0; x < NUMBERCLIENTS; x++) for (int x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].inNetMusicSelect) if (Client[x].inNetMusicSelect)
SendNetPacket(x, Reply); SendNetPacket(x, Reply);
// SendToAllClients(Reply); // SendToAllClients(Reply);
@@ -520,7 +565,8 @@ void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) {
if (strcmp(CurrentSongInfo.artist, LastSongInfo.artist) == 0) if (strcmp(CurrentSongInfo.artist, LastSongInfo.artist) == 0)
SecondSameSelect = true; SecondSameSelect = true;
if (SecondSameSelect == false) { if (SecondSameSelect == false)
{
LastSongInfo.title = CurrentSongInfo.title; LastSongInfo.title = CurrentSongInfo.title;
LastSongInfo.artist = CurrentSongInfo.artist; LastSongInfo.artist = CurrentSongInfo.artist;
LastSongInfo.subtitle = CurrentSongInfo.subtitle; LastSongInfo.subtitle = CurrentSongInfo.subtitle;
@@ -531,7 +577,9 @@ void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) {
ServerChat(message); ServerChat(message);
} }
} else { }
else
{
message = servername; message = servername;
message += ": You do not have permission to pick a song."; message += ": You do not have permission to pick a song.";
Reply.ClearPacket(); Reply.ClearPacket();
@@ -541,14 +589,16 @@ void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) {
} }
} }
if (use == 1) { if (use == 1)
{
//If user dosn't have song //If user dosn't have song
Client[clientNum].hasSong = false; Client[clientNum].hasSong = false;
message = servername; message = servername;
message += ": "; message += ": ";
message += Client[clientNum].Player[0].name; message += Client[clientNum].Player[0].name;
if (Client[clientNum].twoPlayers) { if (Client[clientNum].twoPlayers)
{
message += "&"; message += "&";
message += Client[clientNum].Player[1].name; message += Client[clientNum].Player[1].name;
} }
@@ -564,7 +614,8 @@ void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) {
Client[clientNum].hasSong = true; Client[clientNum].hasSong = true;
//Only play if everyone has the same song and the host has select the same song twice. //Only play if everyone has the same song and the host has select the same song twice.
if ( CheckHasSongState() && SecondSameSelect && (clientNum == 0) ) { if ( CheckHasSongState() && SecondSameSelect && (clientNum == 0) )
{
Reply.ClearPacket(); Reply.ClearPacket();
Reply.Write1(136); Reply.Write1(136);
Reply.Write1(2); Reply.Write1(2);
@@ -579,7 +630,8 @@ void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) {
//Only send data to clients currently in ScreenNetMusicSelect //Only send data to clients currently in ScreenNetMusicSelect
for (int x = 0; x < NUMBERCLIENTS; x++) for (int x = 0; x < NUMBERCLIENTS; x++)
if (Client[x].inNetMusicSelect) { if (Client[x].inNetMusicSelect)
{
SendNetPacket(x, Reply); SendNetPacket(x, Reply);
//Designate the client is starting, //Designate the client is starting,
//after ScreenNetMusicSelect but before game play (InGame). //after ScreenNetMusicSelect but before game play (InGame).
@@ -590,76 +642,86 @@ void StepManiaLanServer::SelectSong(PacketFunctions &Packet, int clientNum) {
} }
} }
bool StepManiaLanServer::CheckHasSongState() { bool StepManiaLanServer::CheckHasSongState()
for (int x = 0; x < NUMBERCLIENTS; x++) {
if (Client[x].Used == true) for (int x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].Used)
if (Client[x].inNetMusicSelect) if (Client[x].inNetMusicSelect)
if (Client[x].hasSong == false) if (!Client[x].hasSong)
return false; return false;
return true; return true;
} }
void StepManiaLanServer::ClearHasSong() { void StepManiaLanServer::ClearHasSong()
for (int x = 0; x < NUMBERCLIENTS; x++) {
if (Client[x].Used == true) for (int x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].Used)
Client[x].hasSong = false; Client[x].hasSong = false;
} }
void StepManiaLanServer::SendToAllClients(PacketFunctions &Packet) { void StepManiaLanServer::SendToAllClients(PacketFunctions& Packet)
for (int x = 0; x < NUMBERCLIENTS; x++) {
for (int x = 0; x < NUMBERCLIENTS; ++x)
SendNetPacket(x, Packet); SendNetPacket(x, Packet);
} }
void StepManiaLanServer::ServerChat(CString message) { void StepManiaLanServer::ServerChat(const CString& message)
{
Reply.ClearPacket(); Reply.ClearPacket();
Reply.Write1(135); Reply.Write1(135);
Reply.WriteNT(message); Reply.WriteNT(message);
SendToAllClients(Reply); SendToAllClients(Reply);
} }
int StepManiaLanServer::FindEmptyClient() { int StepManiaLanServer::FindEmptyClient()
{
//Look at Used flag, if zero then it's an empty client (return client index, -1 if none). //Look at Used flag, if zero then it's an empty client (return client index, -1 if none).
for (int x = 0; x < NUMBERCLIENTS; x++) for (int x = 0; x < NUMBERCLIENTS; ++x)
if (Client[x].Used == 0) if (Client[x].Used == 0)
return x; return x;
return -1; return -1;
} }
void GameClient::CheckConnection() { void GameClient::CheckConnection()
{
//If there is an error close the socket. //If there is an error close the socket.
if (clientSocket.IsError()) { if (clientSocket.IsError())
{
clientSocket.close(); clientSocket.close();
Used = GotStartRequest = hasSong = InGame = inNetMusicSelect = false; Used = GotStartRequest = hasSong = InGame = inNetMusicSelect = false;
Player[0].name = Player[1].name = ""; Player[0].name = Player[1].name = "";
} }
} }
void StepManiaLanServer::MoveClientToHost() { void StepManiaLanServer::MoveClientToHost()
if (Client[ClientHost].Used == false) {
for (int x = 1; x < NUMBERCLIENTS; x++) if (!Client[ClientHost].Used)
if (Client[x].Used == true) { for (int x = 1; x < NUMBERCLIENTS; ++x)
if (Client[x].Used)
{
ClientHost = x; ClientHost = x;
x = NUMBERCLIENTS+1; x = NUMBERCLIENTS+1;
} }
} }
void StepManiaLanServer::SendUserList() { void StepManiaLanServer::SendUserList()
{
Reply.ClearPacket(); Reply.ClearPacket();
Reply.Write1(137); Reply.Write1(137);
Reply.Write1(NUMBERCLIENTS*2); Reply.Write1(NUMBERCLIENTS*2);
Reply.Write1(NUMBERCLIENTS*2); Reply.Write1(NUMBERCLIENTS*2);
for (int x = 0; x < NUMBERCLIENTS; x++) { for (int x = 0; x < NUMBERCLIENTS; ++x)
for (int y = 0; y < 2; y++) { {
if (Client[x].Player[y].name.length() == 0) { for (int y = 0; y < 2; ++y)
{
if (Client[x].Player[y].name.length() == 0)
Reply.Write1(0); Reply.Write1(0);
} else { else
Reply.Write1(1); Reply.Write1(1);
}
Reply.WriteNT(Client[x].Player[y].name); Reply.WriteNT(Client[x].Player[y].name);
} }
} }
@@ -667,18 +729,22 @@ void StepManiaLanServer::SendUserList() {
SendToAllClients(Reply); SendToAllClients(Reply);
} }
void StepManiaLanServer::ScreenNetMusicSelectStatus(PacketFunctions &Packet, int clientNum) { void StepManiaLanServer::ScreenNetMusicSelectStatus(PacketFunctions& Packet, int clientNum)
{
LOG->Info("SNMS Change"); LOG->Info("SNMS Change");
CString message = servername; CString message = servername;
message += ": "; message += ": ";
if (Packet.Read1() == 1) { if (Packet.Read1() == 1)
{
Client[clientNum].inNetMusicSelect = true; Client[clientNum].inNetMusicSelect = true;
message += Client[clientNum].Player[0].name; message += Client[clientNum].Player[0].name;
if (Client[clientNum].twoPlayers) if (Client[clientNum].twoPlayers)
message += "&"; message += "&";
message += Client[clientNum].Player[1].name; message += Client[clientNum].Player[1].name;
message += " entered the game."; message += " entered the game.";
} else { }
else
{
Client[clientNum].inNetMusicSelect = false; Client[clientNum].inNetMusicSelect = false;
message += Client[clientNum].Player[0].name; message += Client[clientNum].Player[0].name;
if (Client[clientNum].twoPlayers) if (Client[clientNum].twoPlayers)
+25 -30
View File
@@ -1,23 +1,17 @@
#ifndef NetworkSyncServer_H #ifndef NetworkSyncServer_H
#define NetworkSyncServer_H #define NetworkSyncServer_H
#include "global.h"
#include "ezsockets.h" #include "ezsockets.h"
#include "NetworkSyncManager.h" #include "NetworkSyncManager.h"
#include <time.h>
#include "StdString.h"
using namespace std;
typedef unsigned char Uint8; typedef unsigned char Uint8;
#define NETMAXBUFFERSIZE 1020 #define NETMAXBUFFERSIZE 1020
#define NUMBERCLIENTS 16 #define NUMBERCLIENTS 16
class LanPlayer { class LanPlayer
public: {
public:
string name; string name;
long score; long score;
int health; int health;
@@ -33,25 +27,25 @@ class LanPlayer {
int diff; int diff;
string options; string options;
LanPlayer(); LanPlayer();
private:
}; };
class GameInfo { class GameInfo
public: {
public:
CString title; CString title;
CString subtitle; CString subtitle;
CString artist; CString artist;
CString course; CString course;
private:
}; };
class GameClient { class GameClient
public: {
public:
bool GotStartRequest; bool GotStartRequest;
EzSockets clientSocket; EzSockets clientSocket;
bool Used; bool Used;
void UpdateStats(PacketFunctions &Packet); void UpdateStats(PacketFunctions &Packet);
void SetClientVersion(int ver, CString b); void SetClientVersion(int ver, const CString& b);
void StartRequest(PacketFunctions &Packet); void StartRequest(PacketFunctions &Packet);
int GetData(PacketFunctions &Packet); int GetData(PacketFunctions &Packet);
GameClient(); GameClient();
@@ -67,14 +61,15 @@ class GameClient {
bool isStarting; bool isStarting;
bool wasIngame; bool wasIngame;
private: private:
string build; string build;
GameInfo gameInfo; GameInfo gameInfo;
int version; int version;
}; };
class StepManiaLanServer { class StepManiaLanServer
public: {
public:
bool ServerStart(); bool ServerStart();
void ServerStop(); void ServerStop();
void ServerUpdate(); void ServerUpdate();
@@ -83,7 +78,7 @@ class StepManiaLanServer {
CString servername; CString servername;
CString lastError; CString lastError;
int lastErrorCode; int lastErrorCode;
private: private:
bool stop; bool stop;
PacketFunctions Packet; PacketFunctions Packet;
PacketFunctions Reply; PacketFunctions Reply;
@@ -99,31 +94,31 @@ class StepManiaLanServer {
bool SecondSameSelect; bool SecondSameSelect;
int numPlayers; int numPlayers;
void Hello(PacketFunctions&Packet, int clientNum); void Hello(PacketFunctions& Packet, int clientNum);
void UpdateClients(); void UpdateClients();
int FindEmptyClient(); int FindEmptyClient();
void NewClientCheck(); void NewClientCheck();
void ParseData(PacketFunctions &Packet, int clientNum); void ParseData(PacketFunctions& Packet, int clientNum);
void SendValue(Uint8 value, int clientNum); void SendValue(Uint8 value, int clientNum);
void CheckReady(); void CheckReady();
void MoveClientToHost(); void MoveClientToHost();
void StatsComboColumn(PacketFunctions &data, LanPlayer *playersPtr[], void StatsComboColumn(PacketFunctions &data, LanPlayer *playersPtr[],
int numPlayers); int numPlayers);
void SendStatsToClients(); void SendStatsToClients();
void StatsProjgradeColumn(PacketFunctions &data, LanPlayer *playersPtr[], int numPlayers); void StatsProjgradeColumn(PacketFunctions& data, LanPlayer *playersPtr[], int numPlayers);
void StatsNameColumn(PacketFunctions &data, LanPlayer *playersPtr[], int numPlayers); void StatsNameColumn(PacketFunctions& data, LanPlayer *playersPtr[], int numPlayers);
void SendNetPacket(int client, PacketFunctions &Packet); void SendNetPacket(int client, PacketFunctions &Packet);
int SortStats(LanPlayer *playersPtr[]); int SortStats(LanPlayer *playersPtr[]);
void RelayChat(PacketFunctions &Packet, int clientNum); void RelayChat(PacketFunctions& Packet, int clientNum);
void SelectSong(PacketFunctions &Packet, int clientNum); void SelectSong(PacketFunctions& Packet, int clientNum);
void ServerChat(CString message); void ServerChat(const CString& message);
void SendToAllClients(PacketFunctions &Packet); void SendToAllClients(PacketFunctions& Packet);
bool CheckHasSongState(); bool CheckHasSongState();
void ClearHasSong(); void ClearHasSong();
void AssignPlayerIDs(); void AssignPlayerIDs();
void SendUserList(); void SendUserList();
void GameOver(PacketFunctions &Packet, int clientNum); void GameOver(PacketFunctions& Packet, int clientNum);
void ScreenNetMusicSelectStatus(PacketFunctions &Packet, int clientNum); void ScreenNetMusicSelectStatus(PacketFunctions& Packet, int clientNum);
}; };
#endif #endif