2004-03-16 22:34:48 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
Class: Network Sync Manager
|
|
|
|
|
|
|
|
|
|
Desc: See Header.
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2003-2004 by the person(s) listed below. All rights reserved.
|
|
|
|
|
Charles Lohr
|
|
|
|
|
Joshua Allen
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
2004-03-31 04:18:44 +00:00
|
|
|
|
2004-03-16 22:34:48 +00:00
|
|
|
#include "global.h"
|
2004-04-05 05:22:32 +00:00
|
|
|
#include <cstring>
|
2004-03-16 22:34:48 +00:00
|
|
|
#include "NetworkSyncManager.h"
|
2004-04-14 20:39:58 +00:00
|
|
|
#include "ProfileManager.h"
|
2004-03-16 22:34:48 +00:00
|
|
|
#include "ezsockets.h"
|
|
|
|
|
#include "RageLog.h"
|
2004-04-15 20:21:39 +00:00
|
|
|
#include "StepMania.h"
|
2004-04-14 20:39:58 +00:00
|
|
|
#include "RageUtil.h"
|
2004-04-15 20:21:39 +00:00
|
|
|
|
2004-04-14 20:39:58 +00:00
|
|
|
|
2004-04-15 15:31:59 +00:00
|
|
|
NetworkSyncManager::NetworkSyncManager()
|
2004-03-16 22:34:48 +00:00
|
|
|
{
|
2004-03-17 06:05:52 +00:00
|
|
|
NetPlayerClient = new EzSockets;
|
2004-03-25 15:08:07 +00:00
|
|
|
m_ServerVersion = 0;
|
2004-04-15 15:31:59 +00:00
|
|
|
|
|
|
|
|
useSMserver = false;
|
2004-03-25 15:08:07 +00:00
|
|
|
|
2004-04-15 20:21:39 +00:00
|
|
|
StartUp();
|
2004-04-15 15:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetworkSyncManager::~NetworkSyncManager ()
|
|
|
|
|
{
|
|
|
|
|
//Close Connection to server nicely.
|
|
|
|
|
if (useSMserver)
|
|
|
|
|
NetPlayerClient->close();
|
|
|
|
|
delete NetPlayerClient;
|
|
|
|
|
}
|
2004-04-14 20:39:58 +00:00
|
|
|
|
2004-04-15 20:21:39 +00:00
|
|
|
void NetworkSyncManager::StartUp()
|
2004-04-15 15:31:59 +00:00
|
|
|
{
|
2004-04-15 20:21:39 +00:00
|
|
|
CString ServerIP;
|
|
|
|
|
if( GetCommandlineArgument( "netip", &ServerIP ) )
|
|
|
|
|
{
|
|
|
|
|
if( !Connect(ServerIP.c_str(),8765) )
|
2004-04-15 15:31:59 +00:00
|
|
|
{
|
2004-04-15 20:21:39 +00:00
|
|
|
LOG->Warn( "Network Sync Manager failed to connect" );
|
|
|
|
|
return;
|
2004-04-14 20:39:58 +00:00
|
|
|
}
|
2004-04-15 20:21:39 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if( GetCommandlineArgument( "listen" ) )
|
|
|
|
|
{
|
|
|
|
|
if( !Listen(8765) )
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn( "Listen() failed");
|
|
|
|
|
return;
|
2004-04-14 20:39:58 +00:00
|
|
|
}
|
2004-04-15 20:21:39 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useSMserver = true;
|
|
|
|
|
|
|
|
|
|
int ClientCommand=3;
|
|
|
|
|
NetPlayerClient->send((char*) &ClientCommand, 4);
|
|
|
|
|
|
|
|
|
|
NetPlayerClient->receive(m_ServerVersion);
|
|
|
|
|
// If network play is desired and the connection works,
|
|
|
|
|
// halt until we know what server version we're dealing with
|
|
|
|
|
vector <CString> ProfileNames;
|
|
|
|
|
PROFILEMAN->GetLocalProfileNames(ProfileNames);
|
|
|
|
|
|
|
|
|
|
netName PlayerName;
|
|
|
|
|
if( ProfileNames.size() > 0 )
|
|
|
|
|
{
|
|
|
|
|
PlayerName.m_packID = 30;
|
|
|
|
|
for( unsigned i=0; i < strlen(ProfileNames[0]); i++ )
|
|
|
|
|
PlayerName.m_data[i] = ProfileNames[0][i];
|
|
|
|
|
NetPlayerClient->send((char*) &PlayerName,20);
|
|
|
|
|
}
|
|
|
|
|
if( ProfileNames.size() > 1 )
|
|
|
|
|
{
|
|
|
|
|
PlayerName.m_packID = 31;
|
|
|
|
|
for( unsigned i=0; i < strlen(ProfileNames[1]); i++ )
|
|
|
|
|
PlayerName.m_data[i] = ProfileNames[1][i];
|
|
|
|
|
NetPlayerClient->send( (char*) &PlayerName,20 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG->Info("Server Version: %d",m_ServerVersion);
|
2004-03-16 22:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-17 13:31:42 +00:00
|
|
|
bool NetworkSyncManager::Connect(const CString& addy, unsigned short port)
|
2004-03-16 22:34:48 +00:00
|
|
|
{
|
2004-03-30 20:13:22 +00:00
|
|
|
LOG->Info("Beginning to connect");
|
2004-03-16 22:34:48 +00:00
|
|
|
if (port!=8765)
|
2004-03-17 13:31:42 +00:00
|
|
|
return false;
|
2004-03-16 22:34:48 +00:00
|
|
|
//Make sure using port 8765
|
|
|
|
|
//This may change in future versions
|
|
|
|
|
//It is this way now for protocol's purpose.
|
|
|
|
|
//If there is a new protocol developed down the road
|
|
|
|
|
|
|
|
|
|
//Since under non-lan conditions, the client
|
|
|
|
|
//will be connecting to localhost, this port does not matter.
|
2004-03-17 13:31:42 +00:00
|
|
|
/* What do you mean it doesn't matter if you are connecting to localhost?
|
|
|
|
|
* Also, when does it ever connect to localhost?
|
|
|
|
|
* -- Steve
|
|
|
|
|
*/
|
2004-03-17 13:38:48 +00:00
|
|
|
NetPlayerClient->create(); // Initilize Socket
|
2004-03-17 13:31:42 +00:00
|
|
|
useSMserver = NetPlayerClient->connect(addy, port);
|
|
|
|
|
|
|
|
|
|
return useSMserver;
|
2004-03-16 22:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-30 20:13:22 +00:00
|
|
|
|
|
|
|
|
//Listen (Wait for connection in-bound)
|
|
|
|
|
//NOTE: Right now, StepMania cannot connect back to StepMania!
|
|
|
|
|
|
|
|
|
|
bool NetworkSyncManager::Listen(unsigned short port)
|
|
|
|
|
{
|
|
|
|
|
LOG->Info("Beginning to Listen");
|
|
|
|
|
if (port!=8765)
|
|
|
|
|
return false;
|
|
|
|
|
//Make sure using port 8765
|
|
|
|
|
//This may change in future versions
|
|
|
|
|
//It is this way now for protocol's purpose.
|
|
|
|
|
//If there is a new protocol developed down the road
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EzSockets * EZListener = new EzSockets;
|
|
|
|
|
|
|
|
|
|
EZListener->create();
|
|
|
|
|
NetPlayerClient->create(); // Initilize Socket
|
|
|
|
|
|
|
|
|
|
EZListener->bind(8765);
|
|
|
|
|
|
|
|
|
|
useSMserver = EZListener->listen();
|
|
|
|
|
useSMserver = EZListener->accept( *NetPlayerClient); //Wait for someone to connect
|
|
|
|
|
|
|
|
|
|
EZListener->close(); //Kill Listener
|
|
|
|
|
delete EZListener;
|
|
|
|
|
|
2004-03-31 04:18:44 +00:00
|
|
|
//LOG->Info("Accept Responce: ",useSMserver);
|
2004-03-30 20:13:22 +00:00
|
|
|
useSMserver=true;
|
|
|
|
|
return useSMserver;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-03-17 06:05:52 +00:00
|
|
|
void NetworkSyncManager::ReportScore(int playerID, int step, int score, int combo)
|
2004-03-16 22:34:48 +00:00
|
|
|
{
|
2004-03-17 13:31:42 +00:00
|
|
|
if (!useSMserver) //Make sure that we are using the network
|
2004-03-17 06:05:52 +00:00
|
|
|
return;
|
2004-03-16 22:34:48 +00:00
|
|
|
|
|
|
|
|
netHolder SendNetPack; //Create packet to send to server
|
|
|
|
|
|
|
|
|
|
SendNetPack.m_playerID = playerID;
|
|
|
|
|
SendNetPack.m_combo=combo;
|
2004-03-30 07:43:12 +00:00
|
|
|
SendNetPack.m_score=score; //Load packet with appropriate info
|
2004-03-16 22:34:48 +00:00
|
|
|
SendNetPack.m_step=step-1;
|
2004-03-30 05:50:36 +00:00
|
|
|
SendNetPack.m_life=m_playerLife[playerID];
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-03-17 06:05:52 +00:00
|
|
|
//Send packet to server
|
2004-04-01 16:25:36 +00:00
|
|
|
NetPlayerClient->send((char*)&SendNetPack, sizeof(netHolder));
|
2004-03-30 05:50:36 +00:00
|
|
|
|
2004-03-16 22:34:48 +00:00
|
|
|
}
|
2004-03-31 04:18:44 +00:00
|
|
|
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-03-17 06:05:52 +00:00
|
|
|
void NetworkSyncManager::ReportSongOver()
|
2004-03-16 22:34:48 +00:00
|
|
|
{
|
2004-03-17 13:31:42 +00:00
|
|
|
if (!useSMserver) //Make sure that we are using the network
|
2004-03-16 22:34:48 +00:00
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
netHolder SendNetPack; //Create packet to send to server
|
|
|
|
|
|
2004-03-30 07:43:12 +00:00
|
|
|
SendNetPack.m_playerID = 21; // Song over Packet player ID
|
2004-03-16 22:34:48 +00:00
|
|
|
SendNetPack.m_combo=0;
|
2004-03-30 07:43:12 +00:00
|
|
|
SendNetPack.m_score=0;
|
2004-03-16 22:34:48 +00:00
|
|
|
SendNetPack.m_step=0;
|
2004-03-30 05:50:36 +00:00
|
|
|
SendNetPack.m_life=0;
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-03-30 05:50:36 +00:00
|
|
|
|
2004-04-01 16:25:36 +00:00
|
|
|
NetPlayerClient->send((char*)&SendNetPack, sizeof(netHolder));
|
2004-03-16 22:34:48 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-17 06:05:52 +00:00
|
|
|
void NetworkSyncManager::StartRequest()
|
2004-03-16 22:34:48 +00:00
|
|
|
{
|
2004-03-17 13:31:42 +00:00
|
|
|
if (!useSMserver)
|
2004-03-16 22:34:48 +00:00
|
|
|
return ;
|
|
|
|
|
|
2004-03-17 06:05:52 +00:00
|
|
|
vector <char> tmp; //Temporary vector used by receive function when waiting
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-03-30 20:13:22 +00:00
|
|
|
LOG->Info("Requesting Start from Server.");
|
2004-03-16 22:34:48 +00:00
|
|
|
|
|
|
|
|
netHolder SendNetPack;
|
|
|
|
|
|
2004-03-30 07:43:12 +00:00
|
|
|
SendNetPack.m_playerID = 20; // Song Start Request Packet player ID
|
2004-03-16 22:34:48 +00:00
|
|
|
SendNetPack.m_combo=0;
|
2004-03-30 07:43:12 +00:00
|
|
|
SendNetPack.m_score=0;
|
2004-03-16 22:34:48 +00:00
|
|
|
SendNetPack.m_step=0;
|
2004-03-30 05:50:36 +00:00
|
|
|
SendNetPack.m_life=0;
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-04-01 16:25:36 +00:00
|
|
|
NetPlayerClient->send((char*)&SendNetPack, sizeof(netHolder));
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-03-30 20:13:22 +00:00
|
|
|
LOG->Info("Waiting for RECV");
|
2004-03-16 22:34:48 +00:00
|
|
|
|
|
|
|
|
//Block until go is recieved.
|
2004-03-17 06:05:52 +00:00
|
|
|
NetPlayerClient->receive(tmp);
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-03-30 20:13:22 +00:00
|
|
|
LOG->Info("Starting Game.");
|
2004-03-17 04:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-17 06:05:52 +00:00
|
|
|
//Global and accessable from anywhere
|
|
|
|
|
NetworkSyncManager *NSMAN;
|