The client is registering with the server
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
#ifndef INFORMATIONMANAGER_HPP_
|
||||
#define INFORMATIONMANAGER_HPP_
|
||||
|
||||
class InformationManager {
|
||||
public:
|
||||
int SetClientIndex(int i) { return clientIndex = i; }
|
||||
int GetClientIndex() { return clientIndex; }
|
||||
private:
|
||||
int clientIndex = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
+8
-3
@@ -160,7 +160,7 @@ int Lobby::HandlePacket(Packet p) {
|
||||
// //
|
||||
// break;
|
||||
case PacketType::JOIN_RESPONSE:
|
||||
//TODO
|
||||
BeginGame(p.joinResponse);
|
||||
break;
|
||||
// case PacketType::DISCONNECT:
|
||||
// //
|
||||
@@ -204,7 +204,12 @@ void Lobby::ConnectToServer(ServerEntry* server) {
|
||||
}
|
||||
Packet p;
|
||||
p.meta.type = PacketType::JOIN_REQUEST;
|
||||
snprintf(p.joinRequest.playerHandle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
|
||||
snprintf(p.joinRequest.playerAvatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
|
||||
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||
}
|
||||
|
||||
void Lobby::BeginGame(JoinResponse& response) {
|
||||
//should be downloading the resources here as well
|
||||
infoMgr->SetClientIndex(response.clientIndex);
|
||||
netUtil->Bind(&response.meta.address, GAME_CHANNEL);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
#ifndef LOBBY_HPP_
|
||||
#define LOBBY_HPP_
|
||||
|
||||
#include "defines.hpp"
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "network_queue.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
@@ -47,11 +49,13 @@ protected:
|
||||
void BroadcastNetwork();
|
||||
void PushServer(BroadcastResponse&);
|
||||
void ConnectToServer(ServerEntry*);
|
||||
void BeginGame(JoinResponse&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = ServiceLocator<InformationManager>::Get();
|
||||
|
||||
//members
|
||||
Button refreshButton;
|
||||
|
||||
@@ -92,6 +92,7 @@ void SceneManager::Init() {
|
||||
//instanciate the remaining services
|
||||
surfaceMgr = ServiceLocator<SurfaceManager>::Set(new SurfaceManager());
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(new UDPNetworkUtility());
|
||||
infoMgr = ServiceLocator<InformationManager>::Set(new InformationManager());
|
||||
|
||||
//initiate the remaining services
|
||||
netUtil->Open(0, sizeof(Packet));
|
||||
@@ -144,6 +145,7 @@ void SceneManager::Quit() {
|
||||
configUtil = ServiceLocator<ConfigUtility>::Set(nullptr);
|
||||
surfaceMgr = ServiceLocator<SurfaceManager>::Set(nullptr);
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(nullptr);
|
||||
infoMgr = ServiceLocator<InformationManager>::Set(nullptr);
|
||||
|
||||
//clean up the scene
|
||||
UnloadScene();
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
@@ -53,6 +54,7 @@ private:
|
||||
ConfigUtility* configUtil = nullptr;
|
||||
SurfaceManager* surfaceMgr = nullptr;
|
||||
UDPNetworkUtility* netUtil = nullptr;
|
||||
InformationManager* infoMgr = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#define GAME_CHANNEL 0
|
||||
#define CHAT_CHANNEL 1
|
||||
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -52,14 +52,11 @@ struct BroadcastResponse {
|
||||
|
||||
struct JoinRequest {
|
||||
Metadata meta;
|
||||
char playerHandle[PACKET_STRING_SIZE];
|
||||
char playerAvatar[PACKET_STRING_SIZE];
|
||||
//TODO: player data
|
||||
};
|
||||
|
||||
struct JoinResponse {
|
||||
Metadata meta;
|
||||
int playerIndex;
|
||||
int clientIndex;
|
||||
//resource list
|
||||
};
|
||||
|
||||
|
||||
@@ -173,32 +173,22 @@ void ServerApplication::HandleConnection(JoinRequest& request) {
|
||||
return;
|
||||
}
|
||||
//create the containers
|
||||
ClientData client = { uniqueIndex };
|
||||
PlayerData player = { uniqueIndex };
|
||||
|
||||
uniqueIndex++;
|
||||
|
||||
//link the containers
|
||||
client.playerIndex = player.index;
|
||||
player.clientIndex = client.index;
|
||||
|
||||
//fill the containers
|
||||
player.handle = request.playerHandle;
|
||||
player.avatar = request.playerAvatar;
|
||||
ClientData client = { uniqueIndex++ };
|
||||
|
||||
//bind the address
|
||||
client.channel = netUtil->Bind(&request.meta.address);
|
||||
|
||||
//push this information
|
||||
clients[client.index] = client;
|
||||
players[player.index] = player;
|
||||
|
||||
//send the player their information
|
||||
Packet p;
|
||||
p.meta.type = PacketType::JOIN_RESPONSE;
|
||||
p.joinResponse.playerIndex = player.index;
|
||||
p.joinResponse.clientIndex = client.index;
|
||||
//TODO: resource list
|
||||
netUtil->Send(client.channel, &p, sizeof(Packet));
|
||||
|
||||
//send it out to new players
|
||||
//TODO
|
||||
//pretty
|
||||
cout << "New connection" << endl;
|
||||
cout << "number of clients: " << clients.size() << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user