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 serverport=1991
servername=foobar
+44 -34
View File
@@ -28,9 +28,11 @@ void ServerApplication::Init() {
} }
void ServerApplication::Proc() { void ServerApplication::Proc() {
Clock::duration delta = Clock::now() - lastTick;
lastTick = Clock::now();
while(running) { while(running) {
HandleNetwork(); HandleNetwork();
UpdateWorld(); UpdateWorld(double(delta.count()) / Clock::duration::period::den);
SDL_Delay(10); SDL_Delay(10);
} }
} }
@@ -49,51 +51,52 @@ 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
Packet p;
p.type = PacketType::PONG;
netUtil.Send(&netUtil.GetInPacket()->address, &p, sizeof(Packet));
}
break; break;
case PacketType::PONG: case PacketType::PONG:
// //
break; break;
case PacketType::BROADCAST_REQUEST: case PacketType::BROADCAST_REQUEST:
// 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; 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: default:
throw(runtime_error("Failed to recognize the packet type")); throw(runtime_error("Failed to recognize the packet type"));
} }
} }
} }
void ServerApplication::UpdateWorld() { void ServerApplication::UpdateWorld(double delta) {
Clock::duration delta = Clock::now() - lastTick;
lastTick = Clock::now();
double d = double(delta.count()) / Clock::duration::period::den;
for (auto it : players) { for (auto it : players) {
it.second.Update(d); it.second.Update(delta);
} }
} }
@@ -101,4 +104,11 @@ void ServerApplication::UpdateWorld() {
//Network loop //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));
}
+19 -18
View File
@@ -11,6 +11,23 @@
#include <map> #include <map>
#include <chrono> #include <chrono>
struct ClientData {
int index;
int channel;
int playerIndex;
};
struct PlayerData {
int index;
int clientIndex;
Vector2 position;
Vector2 motion;
void Update(double delta) {
position += motion * delta;
}
};
class ServerApplication { class ServerApplication {
public: public:
ServerApplication(); ServerApplication();
@@ -21,28 +38,12 @@ public:
ServerApplication(ServerApplication const&) = delete; ServerApplication(ServerApplication const&) = delete;
private: private:
struct ClientData {
int index;
int channel;
int playerIndex;
};
struct PlayerData {
int index;
int clientIndex;
Vector2 position;
Vector2 motion;
void Update(double delta) {
position += motion * delta;
}
};
//game loop //game loop
void HandleNetwork(); void HandleNetwork();
void UpdateWorld(); void UpdateWorld(double delta);
//network loop //network loop
//... void Broadcast(BroadcastRequest&);
Clock::time_point lastTick = Clock::now(); Clock::time_point lastTick = Clock::now();