The server list is being populated from the network

This commit is contained in:
Kayne Ruse
2013-06-13 12:55:26 +10:00
parent 5110ebc1b2
commit a0fa874a29
3 changed files with 63 additions and 6 deletions
+59 -3
View File
@@ -44,7 +44,7 @@ void Lobby::FrameStart() {
} }
void Lobby::Update(double delta) { void Lobby::Update(double delta) {
// Receive();
} }
void Lobby::FrameEnd() { void Lobby::FrameEnd() {
@@ -126,6 +126,62 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
//Utilities //Utilities
//------------------------- //-------------------------
void Lobby::BroadcastNetwork() { void Lobby::Receive() {
// Packet p;
while(netUtil->Receive()) {
memcpy(&p, netUtil->GetInData(), sizeof(Packet));
switch(p.type) {
case PacketType::PING:
//quick pong
p.type = PacketType::PONG;
netUtil->Send(&netUtil->GetInPacket()->address, &p, sizeof(Packet));
break;
case PacketType::PONG:
//
break;
// case PacketType::BROADCAST_REQUEST:
// //
// break;
case PacketType::BROADCAST_RESPONSE:
PushServer(p.broadcastResponse);
break;
// case PacketType::JOIN_REQUEST:
// //
// break;
// case PacketType::JOIN_RESPONSE:
// //
// break;
// case PacketType::DISCONNECT:
// //
// break;
// case PacketType::SYNCHRONIZE:
// //
// break;
// case PacketType::PLAYER_NEW:
// //
// break;
// case PacketType::PLAYER_DELETE:
// //
// break;
// case PacketType::PLAYER_MOVE:
// //
// break;
default:
throw(runtime_error("Failed to recognize the packet type"));
}
}
}
void Lobby::BroadcastNetwork() {
Packet p;
p.type = PacketType::BROADCAST_REQUEST;
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet));
serverList.clear();
}
void Lobby::PushServer(BroadcastResponse& bcast) {
ServerEntry entry;
entry.name = bcast.name;
entry.address = netUtil->GetInPacket()->address;
serverList.push_back(entry);
} }
+3
View File
@@ -3,6 +3,7 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "service_locator.hpp" #include "service_locator.hpp"
#include "packet_type.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
@@ -39,7 +40,9 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//utilities //utilities
void Receive();
void BroadcastNetwork(); void BroadcastNetwork();
void PushServer(BroadcastResponse&);
//services //services
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get(); ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
+2 -4
View File
@@ -33,7 +33,7 @@ void ServerApplication::Init() {
netUtil.Open(configUtil.Int("server.port"), sizeof(Packet)); netUtil.Open(configUtil.Int("server.port"), sizeof(Packet));
//disabled for debugging //disabled for debugging
// running = true; running = true;
} }
void ServerApplication::Proc() { void ServerApplication::Proc() {
@@ -60,12 +60,10 @@ void ServerApplication::HandleNetwork() {
while(netUtil.Receive()) { while(netUtil.Receive()) {
memcpy(&p, netUtil.GetInData(), sizeof(Packet)); memcpy(&p, netUtil.GetInData(), sizeof(Packet));
switch(p.type) { switch(p.type) {
case PacketType::PING: { case PacketType::PING:
//quick pong //quick pong
Packet p;
p.type = PacketType::PONG; p.type = PacketType::PONG;
netUtil.Send(&netUtil.GetInPacket()->address, &p, sizeof(Packet)); netUtil.Send(&netUtil.GetInPacket()->address, &p, sizeof(Packet));
}
break; break;
case PacketType::PONG: case PacketType::PONG:
// //