This repository has been archived on 2026-04-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Tortuga/server/server_application.cpp
T
2013-06-13 12:26:27 +10:00

123 lines
2.7 KiB
C++

#include "server_application.hpp"
#include <stdexcept>
#include <iostream>
using namespace std;
//-------------------------
//Public access members
//-------------------------
ServerApplication::ServerApplication() {
//
}
ServerApplication::~ServerApplication() {
//
}
void ServerApplication::Init() {
if (SDLNet_Init()) {
throw(runtime_error("Failed to initialide SDL_net"));
}
configUtil.Load("rsc/config.cfg");
//check the port is valid
if (configUtil.Int("server.port") <= 0) {
throw(runtime_error("Cannot open the server on an invalid port or port 0"));
}
cout << configUtil["server.name"] << endl;
cout << "Opening on port " << configUtil["server.port"] << endl;
netUtil.Open(configUtil.Int("server.port"), sizeof(Packet));
//disabled for debugging
// running = true;
}
void ServerApplication::Proc() {
Clock::duration delta = Clock::now() - lastTick;
lastTick = Clock::now();
while(running) {
HandleNetwork();
UpdateWorld(double(delta.count()) / Clock::duration::period::den);
SDL_Delay(10);
}
}
void ServerApplication::Quit() {
netUtil.Close();
SDLNet_Quit();
}
//-------------------------
//Game loop
//-------------------------
void ServerApplication::HandleNetwork() {
Packet p;
while(netUtil.Receive()) {
memcpy(&p, netUtil.GetInData(), sizeof(Packet));
switch(p.type) {
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:
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(double delta) {
for (auto it : players) {
it.second.Update(delta);
}
}
//-------------------------
//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("server.name"));
//TODO version information
netUtil.Send(&netUtil.GetInPacket()->address, &p, sizeof(Packet));
}