Connections and disconnections between client and server are functioning

This commit is contained in:
Kayne Ruse
2013-05-24 15:39:42 +10:00
parent 0b2af1d80f
commit 1ab963099d
10 changed files with 63 additions and 15 deletions
+9 -1
View File
@@ -8,16 +8,24 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) { InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil, int* ID) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering InGame" << endl; cout << "entering InGame" << endl;
#endif #endif
configUtil = cUtil; configUtil = cUtil;
surfaceMgr = sMgr; surfaceMgr = sMgr;
netUtil = nUtil; netUtil = nUtil;
playerID = ID;
cout << "playerID: " << *playerID << endl;
} }
InGame::~InGame() { InGame::~InGame() {
PacketData p;
p.type = PacketList::DISCONNECT;
p.disconnect.playerID = *playerID;
*playerID = -1;
netUtil->Send(0, &p, sizeof(PacketData));
netUtil->Unbind(0);
#ifdef DEBUG #ifdef DEBUG
cout << "leaving InGame" << endl; cout << "leaving InGame" << endl;
#endif #endif
+2 -1
View File
@@ -11,7 +11,7 @@
class InGame : public BaseScene { class InGame : public BaseScene {
public: public:
//Public access members //Public access members
InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*); InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
virtual ~InGame(); virtual ~InGame();
protected: protected:
@@ -32,6 +32,7 @@ protected:
ConfigUtility* configUtil = nullptr; ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr = nullptr; SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr; UDPNetworkUtility* netUtil = nullptr;
int* playerID;
}; };
#endif #endif
+10 -1
View File
@@ -9,7 +9,7 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) { Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil, int* ID) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering Lobby" << endl; cout << "entering Lobby" << endl;
#endif #endif
@@ -17,6 +17,7 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti
configUtil = cUtil; configUtil = cUtil;
surfaceMgr = sMgr; surfaceMgr = sMgr;
netUtil = nUtil; netUtil = nUtil;
playerID = ID;
//members //members
font.SetSurface(surfaceMgr->Get("font")); font.SetSurface(surfaceMgr->Get("font"));
@@ -72,6 +73,11 @@ void Lobby::Receive() {
break; break;
case PacketList::JOINCONFIRM: case PacketList::JOINCONFIRM:
//TODO: enter the game //TODO: enter the game
PacketData jc;
memcpy(&jc, netUtil->GetInData(), sizeof(PacketData));
*playerID = jc.joinConfirm.playerID;
netUtil->Bind(&netUtil->GetInPacket()->address, 0);
SetNextScene(SceneList::INGAME);
break; break;
} }
} }
@@ -164,6 +170,9 @@ void Lobby::PushServer(PacketData* packet) {
} }
void Lobby::JoinRequest(ServerData* server) { void Lobby::JoinRequest(ServerData* server) {
if (!server) {
return;
}
PacketData packet; PacketData packet;
packet.type = PacketList::JOINREQUEST; packet.type = PacketList::JOINREQUEST;
snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
+4 -2
View File
@@ -18,7 +18,7 @@
class Lobby : public BaseScene { class Lobby : public BaseScene {
public: public:
//Public access members //Public access members
Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*); Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
virtual ~Lobby(); virtual ~Lobby();
protected: protected:
@@ -46,11 +46,13 @@ protected:
void PushServer(PacketData*); void PushServer(PacketData*);
void JoinRequest(ServerData*); void JoinRequest(ServerData*);
//members //globals
ConfigUtility* configUtil = nullptr; ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr = nullptr; SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr; UDPNetworkUtility* netUtil = nullptr;
int* playerID;
//members
RasterFont font; RasterFont font;
std::map<std::string, Button*> buttonMap; std::map<std::string, Button*> buttonMap;
+2 -2
View File
@@ -104,10 +104,10 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
activeScene = new MainMenu(&configUtil, &surfaceMgr); activeScene = new MainMenu(&configUtil, &surfaceMgr);
break; break;
case SceneList::LOBBY: case SceneList::LOBBY:
activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil); activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil, &playerID);
break; break;
case SceneList::INGAME: case SceneList::INGAME:
activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil); activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil, &playerID);
break; break;
#ifdef DEBUG #ifdef DEBUG
+1
View File
@@ -32,6 +32,7 @@ private:
ConfigUtility configUtil; ConfigUtility configUtil;
SurfaceManager surfaceMgr; SurfaceManager surfaceMgr;
UDPNetworkUtility netUtil; UDPNetworkUtility netUtil;
int playerID = -1;
}; };
#endif #endif
+1
View File
@@ -51,6 +51,7 @@ struct JoinConfirm {
struct Disconnect { struct Disconnect {
PacketList type = PacketList::DISCONNECT; PacketList type = PacketList::DISCONNECT;
int playerID;
}; };
//------------------------- //-------------------------
+1
View File
@@ -96,6 +96,7 @@ int UDPNetworkUtility::Send(int channel, void* data, int len) {
} }
int UDPNetworkUtility::Receive() { int UDPNetworkUtility::Receive() {
memset(packIn->data, 0, packIn->maxlen);
int ret = SDLNet_UDP_Recv(socket, packIn); int ret = SDLNet_UDP_Recv(socket, packIn);
if (ret < 0) { if (ret < 0) {
+5
View File
@@ -23,6 +23,11 @@ struct ClientData {
Vector2 motion; Vector2 motion;
std::string handle; std::string handle;
std::string avatar; std::string avatar;
enum class Command {
NORMAL,
SYNCHRONIZE,
PING,
}command = Command::NORMAL;
}; };
#endif #endif
+28 -8
View File
@@ -84,16 +84,36 @@ void ServerApplication::Ping(PacketData* packet) {
void ServerApplication::JoinRequest(PacketData* packet) { void ServerApplication::JoinRequest(PacketData* packet) {
//TODO //TODO
cout << "Join request..." << endl; cout << "Join request..." << endl;
// if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) { if (clientMap.size() >= maxClients) {
// //rejection //rejection
// return; return;
// } }
// int ch = netUtil.Bind(&netUtil.GetInPacket()->address, -1); int playerID = uniqueIndex++;
// cout << ch << endl; clientMap[playerID].playerID = playerID;
clientMap[playerID].channel = netUtil.Bind(&netUtil.GetInPacket()->address, -1);
clientMap[playerID].handle = packet->joinRequest.handle;
clientMap[playerID].avatar = packet->joinRequest.avatar;
//debug
cout << "playerID: " << playerID << ", " << clientMap[playerID].playerID << endl;
cout << "channel: " << clientMap[playerID].channel << endl;
cout << "handle: " << clientMap[playerID].handle << endl;
cout << "avatar: " << clientMap[playerID].avatar << endl;
//join confirm
PacketData jc;
jc.type = PacketList::JOINCONFIRM;
jc.joinConfirm.playerID = clientMap[playerID].playerID;
netUtil.Send(clientMap[playerID].channel, &jc, sizeof(PacketData));
} }
void ServerApplication::Disconnect(PacketData* packet) { void ServerApplication::Disconnect(PacketData* packet) { //TODO: use playerID here
//TODO //TODO: Delete player
int playerID = packet->disconnect.playerID;
cout << "disconnecting: " << playerID << endl;
netUtil.Unbind(clientMap[playerID].channel);
clientMap.erase(playerID);
cout << "current players: " << clientMap.size() << endl;
} }
void ServerApplication::Movement(PacketData* packet) { void ServerApplication::Movement(PacketData* packet) {