working on the server's skeleton, still slow going

This commit is contained in:
Kayne Ruse
2013-06-10 17:23:26 +10:00
parent 0a48131de4
commit c21a95f3e9
7 changed files with 155 additions and 19 deletions
+4
View File
@@ -1,4 +1,8 @@
#ifndef DEFINES_HPP_ #ifndef DEFINES_HPP_
#define DEFINES_HPP #define DEFINES_HPP
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
#endif #endif
+12 -13
View File
@@ -23,66 +23,65 @@ enum class PacketType {
PLAYER_NEW = 9, PLAYER_NEW = 9,
PLAYER_DELETE = 10, PLAYER_DELETE = 10,
PLAYER_MOVE = 11, PLAYER_MOVE = 11,
}; };
struct Ping { struct Ping {
PacketType type = PacketType::PING; PacketType type;
}; };
struct Pong { struct Pong {
PacketType type = PacketType::PONG; PacketType type;
}; };
struct BroadcastRequest { struct BroadcastRequest {
PacketType type = PacketType::BROADCAST_REQUEST; PacketType type;
}; };
struct BroadcastResponse { struct BroadcastResponse {
PacketType type = PacketType::BROADCAST_RESPONSE; PacketType type;
char name[PACKET_STRING_SIZE]; char name[PACKET_STRING_SIZE];
//TODO: version //TODO: version
}; };
struct JoinRequest { struct JoinRequest {
PacketType type = PacketType::JOIN_REQUEST; PacketType type;
//TODO: player data //TODO: player data
}; };
struct JoinResponse { struct JoinResponse {
PacketType type = PacketType::JOIN_RESPONSE; PacketType type;
int playerIndex; int playerIndex;
//resource list //resource list
}; };
struct Disconnect { struct Disconnect {
PacketType type = PacketType::DISCONNECT; PacketType type;
}; };
struct Synchronize { struct Synchronize {
PacketType type = PacketType::SYNCHRONIZE; PacketType type;
}; };
struct PlayerNew { struct PlayerNew {
PacketType type = PacketType::PLAYER_NEW; PacketType type;
int playerIndex; int playerIndex;
//TODO Playerdata //TODO Playerdata
}; };
struct PlayerDelete { struct PlayerDelete {
PacketType type = PacketType::PLAYER_DELETE; PacketType type;
int playerIndex; int playerIndex;
}; };
struct PlayerMove { struct PlayerMove {
PacketType type = PacketType::PLAYER_MOVE; PacketType type;
int playerIndex; int playerIndex;
Vector2 position; Vector2 position;
Vector2 motion; Vector2 motion;
}; };
union Packet { union Packet {
Packet() {} Packet() {};
PacketType type = PacketType::NONE; PacketType type = PacketType::NONE;
Ping ping; Ping ping;
+1
View File
@@ -0,0 +1 @@
serverport=1991
+2 -1
View File
@@ -1,6 +1,7 @@
#config #config
LIBDIR=../libs LIBDIR=../libs
LIB=-lmingw32 -lSDLmain -lSDL $(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libSDL_net.a LOCALLIBS=$(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libSDL_net.a
LIB=$(LOCALLIBS) -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL
INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net ../common INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net ../common
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
+86 -3
View File
@@ -1,5 +1,14 @@
#include "server_application.hpp" #include "server_application.hpp"
#include <stdexcept>
#include <iostream>
using namespace std;
//-------------------------
//Public access members
//-------------------------
ServerApplication::ServerApplication() { ServerApplication::ServerApplication() {
// //
} }
@@ -9,13 +18,87 @@ ServerApplication::~ServerApplication() {
} }
void ServerApplication::Init() { void ServerApplication::Init() {
// if (SDLNet_Init()) {
throw(runtime_error("Failed to initialide SDL_net"));
}
configUtil.Load("rsc/config.cfg");
netUtil.Open(configUtil.Int("serverport"), sizeof(Packet));
running = true;
} }
void ServerApplication::Proc() { void ServerApplication::Proc() {
// while(running) {
HandleNetwork();
UpdateWorld();
SDL_Delay(10);
}
} }
void ServerApplication::Quit() { 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:
//
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:
//
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;
for (auto it : players) {
it.second.Update(d);
}
}
//-------------------------
//Network loop
//-------------------------
//...
+43 -1
View File
@@ -1,6 +1,16 @@
#ifndef SERVERAPPLICATION_HPP_ #ifndef SERVERAPPLICATION_HPP_
#define SERVERAPPLICATION_HPP_ #define SERVERAPPLICATION_HPP_
#include "defines.hpp"
#include "packet_type.hpp"
#include "config_Utility.hpp"
#include "udp_network_utility.hpp"
#include "vector2.hpp"
#include <map>
#include <chrono>
class ServerApplication { class ServerApplication {
public: public:
ServerApplication(); ServerApplication();
@@ -8,8 +18,40 @@ public:
void Init(); void Init();
void Proc(); void Proc();
void Quit(); void Quit();
ServerApplication(ServerApplication const&) = delete;
private: private:
bool running = true; 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
void HandleNetwork();
void UpdateWorld();
//network loop
//...
Clock::time_point lastTick = Clock::now();
std::map<int, ClientData> clients;
std::map<int, PlayerData> players;
ConfigUtility configUtil;
UDPNetworkUtility netUtil;
bool running = false;
}; };
#endif #endif
+7 -1
View File
@@ -1,13 +1,19 @@
//#include "packet_type.hpp" #include "packet_type.hpp"
#include "service_locator.hpp" #include "service_locator.hpp"
#include "foobar.hpp" #include "foobar.hpp"
#include "vector2.hpp"
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
ServiceLocator<int>::Set(new int(42)); ServiceLocator<int>::Set(new int(42));
ServiceLocator<Packet>::Set(new Packet());
cout << FooBar() << endl; cout << FooBar() << endl;
ServiceLocator<int>::Set(nullptr); ServiceLocator<int>::Set(nullptr);
ServiceLocator<Packet>::Set(nullptr);
return 0; return 0;
} }