working on the server's skeleton, still slow going
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
#ifndef DEFINES_HPP_
|
||||
#define DEFINES_HPP
|
||||
|
||||
#include <chrono>
|
||||
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
#endif
|
||||
|
||||
+12
-13
@@ -23,66 +23,65 @@ enum class PacketType {
|
||||
PLAYER_NEW = 9,
|
||||
PLAYER_DELETE = 10,
|
||||
PLAYER_MOVE = 11,
|
||||
|
||||
};
|
||||
|
||||
struct Ping {
|
||||
PacketType type = PacketType::PING;
|
||||
PacketType type;
|
||||
};
|
||||
|
||||
struct Pong {
|
||||
PacketType type = PacketType::PONG;
|
||||
PacketType type;
|
||||
};
|
||||
|
||||
struct BroadcastRequest {
|
||||
PacketType type = PacketType::BROADCAST_REQUEST;
|
||||
PacketType type;
|
||||
};
|
||||
|
||||
struct BroadcastResponse {
|
||||
PacketType type = PacketType::BROADCAST_RESPONSE;
|
||||
PacketType type;
|
||||
char name[PACKET_STRING_SIZE];
|
||||
//TODO: version
|
||||
};
|
||||
|
||||
struct JoinRequest {
|
||||
PacketType type = PacketType::JOIN_REQUEST;
|
||||
PacketType type;
|
||||
//TODO: player data
|
||||
};
|
||||
|
||||
struct JoinResponse {
|
||||
PacketType type = PacketType::JOIN_RESPONSE;
|
||||
PacketType type;
|
||||
int playerIndex;
|
||||
//resource list
|
||||
};
|
||||
|
||||
struct Disconnect {
|
||||
PacketType type = PacketType::DISCONNECT;
|
||||
PacketType type;
|
||||
};
|
||||
|
||||
struct Synchronize {
|
||||
PacketType type = PacketType::SYNCHRONIZE;
|
||||
PacketType type;
|
||||
};
|
||||
|
||||
struct PlayerNew {
|
||||
PacketType type = PacketType::PLAYER_NEW;
|
||||
PacketType type;
|
||||
int playerIndex;
|
||||
//TODO Playerdata
|
||||
};
|
||||
|
||||
struct PlayerDelete {
|
||||
PacketType type = PacketType::PLAYER_DELETE;
|
||||
PacketType type;
|
||||
int playerIndex;
|
||||
};
|
||||
|
||||
struct PlayerMove {
|
||||
PacketType type = PacketType::PLAYER_MOVE;
|
||||
PacketType type;
|
||||
int playerIndex;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
};
|
||||
|
||||
union Packet {
|
||||
Packet() {}
|
||||
Packet() {};
|
||||
PacketType type = PacketType::NONE;
|
||||
|
||||
Ping ping;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
serverport=1991
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
#config
|
||||
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
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
#include "server_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
ServerApplication::ServerApplication() {
|
||||
//
|
||||
}
|
||||
@@ -9,13 +18,87 @@ ServerApplication::~ServerApplication() {
|
||||
}
|
||||
|
||||
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() {
|
||||
//
|
||||
while(running) {
|
||||
HandleNetwork();
|
||||
UpdateWorld();
|
||||
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:
|
||||
//
|
||||
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
|
||||
//-------------------------
|
||||
|
||||
//...
|
||||
@@ -1,6 +1,16 @@
|
||||
#ifndef 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 {
|
||||
public:
|
||||
ServerApplication();
|
||||
@@ -8,8 +18,40 @@ public:
|
||||
void Init();
|
||||
void Proc();
|
||||
void Quit();
|
||||
|
||||
ServerApplication(ServerApplication const&) = delete;
|
||||
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
|
||||
|
||||
+7
-1
@@ -1,13 +1,19 @@
|
||||
//#include "packet_type.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "foobar.hpp"
|
||||
|
||||
#include "vector2.hpp"
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
ServiceLocator<int>::Set(new int(42));
|
||||
ServiceLocator<Packet>::Set(new Packet());
|
||||
|
||||
cout << FooBar() << endl;
|
||||
|
||||
ServiceLocator<int>::Set(nullptr);
|
||||
ServiceLocator<Packet>::Set(nullptr);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user