Added broadcast handling

This commit is contained in:
Kayne Ruse
2013-06-10 18:06:24 +10:00
parent c21a95f3e9
commit 7ca7f7f015
3 changed files with 65 additions and 53 deletions
+1
View File
@@ -1 +1,2 @@
serverport=1991
servername=foobar
+44 -34
View File
@@ -28,9 +28,11 @@ void ServerApplication::Init() {
}
void ServerApplication::Proc() {
Clock::duration delta = Clock::now() - lastTick;
lastTick = Clock::now();
while(running) {
HandleNetwork();
UpdateWorld();
UpdateWorld(double(delta.count()) / Clock::duration::period::den);
SDL_Delay(10);
}
}
@@ -49,51 +51,52 @@ void ServerApplication::HandleNetwork() {
while(netUtil.Receive()) {
memcpy(&p, netUtil.GetInData(), sizeof(Packet));
switch(p.type) {
case PacketType::PING:
//
case PacketType::PING: {
//quick pong
Packet p;
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:
//
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:
//
Broadcast(p.broadcastRequest);
break;
// case PacketType::BROADCAST_RESPONSE:
// //
// 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 ServerApplication::UpdateWorld() {
Clock::duration delta = Clock::now() - lastTick;
lastTick = Clock::now();
double d = double(delta.count()) / Clock::duration::period::den;
void ServerApplication::UpdateWorld(double delta) {
for (auto it : players) {
it.second.Update(d);
it.second.Update(delta);
}
}
@@ -101,4 +104,11 @@ void ServerApplication::UpdateWorld() {
//Network loop
//-------------------------
//...
void ServerApplication::Broadcast(BroadcastRequest& bcast) {
//respond to a broadcast request with the server's data
Packet p;
p.type = PacketType::BROADCAST_RESPONSE;
snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil.CString("servername"));
//TODO version information
netUtil.Send(&netUtil.GetInPacket()->address, &p, sizeof(Packet));
}
+13 -12
View File
@@ -11,21 +11,12 @@
#include <map>
#include <chrono>
class ServerApplication {
public:
ServerApplication();
~ServerApplication();
void Init();
void Proc();
void Quit();
ServerApplication(ServerApplication const&) = delete;
private:
struct ClientData {
int index;
int channel;
int playerIndex;
};
struct PlayerData {
int index;
int clientIndex;
@@ -37,12 +28,22 @@ private:
}
};
class ServerApplication {
public:
ServerApplication();
~ServerApplication();
void Init();
void Proc();
void Quit();
ServerApplication(ServerApplication const&) = delete;
private:
//game loop
void HandleNetwork();
void UpdateWorld();
void UpdateWorld(double delta);
//network loop
//...
void Broadcast(BroadcastRequest&);
Clock::time_point lastTick = Clock::now();