Connections and disconnections between client and server are functioning
This commit is contained in:
+9
-1
@@ -8,16 +8,24 @@ using namespace std;
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
|
||||
InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil, int* ID) {
|
||||
#ifdef DEBUG
|
||||
cout << "entering InGame" << endl;
|
||||
#endif
|
||||
configUtil = cUtil;
|
||||
surfaceMgr = sMgr;
|
||||
netUtil = nUtil;
|
||||
playerID = ID;
|
||||
cout << "playerID: " << *playerID << endl;
|
||||
}
|
||||
|
||||
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
|
||||
cout << "leaving InGame" << endl;
|
||||
#endif
|
||||
|
||||
+2
-1
@@ -11,7 +11,7 @@
|
||||
class InGame : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
|
||||
InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
|
||||
virtual ~InGame();
|
||||
|
||||
protected:
|
||||
@@ -32,6 +32,7 @@ protected:
|
||||
ConfigUtility* configUtil = nullptr;
|
||||
SurfaceManager* surfaceMgr = nullptr;
|
||||
UDPNetworkUtility* netUtil = nullptr;
|
||||
int* playerID;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+10
-1
@@ -9,7 +9,7 @@ using namespace std;
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
|
||||
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil, int* ID) {
|
||||
#ifdef DEBUG
|
||||
cout << "entering Lobby" << endl;
|
||||
#endif
|
||||
@@ -17,6 +17,7 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti
|
||||
configUtil = cUtil;
|
||||
surfaceMgr = sMgr;
|
||||
netUtil = nUtil;
|
||||
playerID = ID;
|
||||
|
||||
//members
|
||||
font.SetSurface(surfaceMgr->Get("font"));
|
||||
@@ -72,6 +73,11 @@ void Lobby::Receive() {
|
||||
break;
|
||||
case PacketList::JOINCONFIRM:
|
||||
//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;
|
||||
}
|
||||
}
|
||||
@@ -164,6 +170,9 @@ void Lobby::PushServer(PacketData* packet) {
|
||||
}
|
||||
|
||||
void Lobby::JoinRequest(ServerData* server) {
|
||||
if (!server) {
|
||||
return;
|
||||
}
|
||||
PacketData packet;
|
||||
packet.type = PacketList::JOINREQUEST;
|
||||
snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
|
||||
|
||||
+4
-2
@@ -18,7 +18,7 @@
|
||||
class Lobby : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
|
||||
Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
|
||||
virtual ~Lobby();
|
||||
|
||||
protected:
|
||||
@@ -46,11 +46,13 @@ protected:
|
||||
void PushServer(PacketData*);
|
||||
void JoinRequest(ServerData*);
|
||||
|
||||
//members
|
||||
//globals
|
||||
ConfigUtility* configUtil = nullptr;
|
||||
SurfaceManager* surfaceMgr = nullptr;
|
||||
UDPNetworkUtility* netUtil = nullptr;
|
||||
int* playerID;
|
||||
|
||||
//members
|
||||
RasterFont font;
|
||||
std::map<std::string, Button*> buttonMap;
|
||||
|
||||
|
||||
@@ -104,10 +104,10 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
|
||||
activeScene = new MainMenu(&configUtil, &surfaceMgr);
|
||||
break;
|
||||
case SceneList::LOBBY:
|
||||
activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil);
|
||||
activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil, &playerID);
|
||||
break;
|
||||
case SceneList::INGAME:
|
||||
activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil);
|
||||
activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil, &playerID);
|
||||
break;
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
@@ -32,6 +32,7 @@ private:
|
||||
ConfigUtility configUtil;
|
||||
SurfaceManager surfaceMgr;
|
||||
UDPNetworkUtility netUtil;
|
||||
int playerID = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -51,6 +51,7 @@ struct JoinConfirm {
|
||||
|
||||
struct Disconnect {
|
||||
PacketList type = PacketList::DISCONNECT;
|
||||
int playerID;
|
||||
};
|
||||
|
||||
//-------------------------
|
||||
|
||||
@@ -96,6 +96,7 @@ int UDPNetworkUtility::Send(int channel, void* data, int len) {
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Receive() {
|
||||
memset(packIn->data, 0, packIn->maxlen);
|
||||
int ret = SDLNet_UDP_Recv(socket, packIn);
|
||||
|
||||
if (ret < 0) {
|
||||
|
||||
@@ -23,6 +23,11 @@ struct ClientData {
|
||||
Vector2 motion;
|
||||
std::string handle;
|
||||
std::string avatar;
|
||||
enum class Command {
|
||||
NORMAL,
|
||||
SYNCHRONIZE,
|
||||
PING,
|
||||
}command = Command::NORMAL;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -84,16 +84,36 @@ void ServerApplication::Ping(PacketData* packet) {
|
||||
void ServerApplication::JoinRequest(PacketData* packet) {
|
||||
//TODO
|
||||
cout << "Join request..." << endl;
|
||||
// if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) {
|
||||
// //rejection
|
||||
// return;
|
||||
// }
|
||||
// int ch = netUtil.Bind(&netUtil.GetInPacket()->address, -1);
|
||||
// cout << ch << endl;
|
||||
if (clientMap.size() >= maxClients) {
|
||||
//rejection
|
||||
return;
|
||||
}
|
||||
int playerID = uniqueIndex++;
|
||||
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) {
|
||||
//TODO
|
||||
void ServerApplication::Disconnect(PacketData* packet) { //TODO: use playerID here
|
||||
//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) {
|
||||
|
||||
Reference in New Issue
Block a user