packet is being sent and received, bare-bones ping-pong is working
This commit is contained in:
+5
-2
@@ -1,7 +1,10 @@
|
||||
#include "player.hpp"
|
||||
|
||||
Player::Player(int id) : clientID(id) {
|
||||
//
|
||||
Player::Player(int ID, int ch, std::string h, std::string a) {
|
||||
playerID = ID;
|
||||
channel = ch;
|
||||
handle = h;
|
||||
avatar = a;
|
||||
}
|
||||
|
||||
void Player::Update(int delta) {
|
||||
|
||||
+21
-26
@@ -7,38 +7,33 @@
|
||||
|
||||
class Player {
|
||||
public:
|
||||
Player(int id);
|
||||
~Player() = default;
|
||||
Player() = default;
|
||||
Player(int playerID, int channel, std::string handle, std::string avatar);
|
||||
void Update(int delta);
|
||||
|
||||
void Update(int);
|
||||
int SetPlayerID(int id) {return playerID = id;}
|
||||
int GetPlayerID() const {return playerID;}
|
||||
int SetChannel(int ch) {return channel = ch;}
|
||||
int GetChannel() const {return channel;}
|
||||
|
||||
int GetClientID() const {
|
||||
return clientID;
|
||||
}
|
||||
Vector2 SetPosition(Vector2 v) {return position = v;}
|
||||
Vector2 ShiftPosition(Vector2 v) {return position += v;}
|
||||
Vector2 GetPosition() const {return position;}
|
||||
Vector2 SetMotion(Vector2 v) {return motion = v;}
|
||||
Vector2 ShiftMotion(Vector2 v) {return motion += v;}
|
||||
Vector2 GetMotion() const {return motion;}
|
||||
|
||||
Vector2 SetPosition(Vector2 v) {
|
||||
return position = v;
|
||||
}
|
||||
Vector2 GetPosition() const {
|
||||
return position;
|
||||
}
|
||||
Vector2 SetMotion(Vector2 v) {
|
||||
return motion = v;
|
||||
}
|
||||
Vector2 GetMotion() const {
|
||||
return motion;
|
||||
}
|
||||
std::string SetAvatarName(std::string s) {
|
||||
return avatarName = s;
|
||||
}
|
||||
std::string GetAvatarName() {
|
||||
return avatarName;
|
||||
}
|
||||
std::string SetAvatar(std::string s) {return avatar = s;}
|
||||
std::string GetAvatar() const {return avatar;}
|
||||
std::string SetHandle(std::string s) {return handle = s;}
|
||||
std::string GetHandle() const {return handle;}
|
||||
private:
|
||||
const int clientID;
|
||||
int playerID = -1;
|
||||
int channel = -1; //for networking
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
std::string avatarName;
|
||||
std::string avatar;
|
||||
std::string handle;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,2 +1,47 @@
|
||||
#include "player_manager.hpp"
|
||||
|
||||
PlayerManager::PlayerManager(int max) {
|
||||
maxPlayers = max;
|
||||
}
|
||||
|
||||
PlayerManager::~PlayerManager() {
|
||||
DeleteAll();
|
||||
}
|
||||
|
||||
void PlayerManager::UpdateAll(int delta) {
|
||||
for (auto it : playerList) {
|
||||
it->Update(delta);
|
||||
}
|
||||
}
|
||||
|
||||
Player* PlayerManager::New(int playerID, int channel, std::string handle, std::string avatar) {
|
||||
Player* p = new Player(playerID, channel, handle, avatar);
|
||||
playerList.push_back(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
Player* PlayerManager::Get(int playerID) {
|
||||
for (auto it : playerList) {
|
||||
if (it->GetPlayerID() == playerID) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void PlayerManager::Delete(int playerID) {
|
||||
for (auto it : playerList) {
|
||||
if (it->GetPlayerID() == playerID) {
|
||||
delete it;
|
||||
playerList.remove(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerManager::DeleteAll() {
|
||||
for (auto it : playerList) {
|
||||
delete it;
|
||||
}
|
||||
playerList.clear();
|
||||
}
|
||||
|
||||
@@ -3,21 +3,37 @@
|
||||
|
||||
#include "player.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
class PlayerManager {
|
||||
public:
|
||||
PlayerManager() = default;
|
||||
~PlayerManager() = default;
|
||||
private:
|
||||
//utilities
|
||||
typedef std::map<std::string, Player*> PlayerMap;
|
||||
typedef std::list<Player*> PlayerList;
|
||||
public:
|
||||
PlayerManager() = default;
|
||||
PlayerManager(int maxPlayers);
|
||||
~PlayerManager();
|
||||
|
||||
void UpdateAll(int delta);
|
||||
|
||||
Player* New(int playerID, int channel, std::string handle, std::string avatar);
|
||||
Player* Get(int playerID);
|
||||
void Delete(int playerID);
|
||||
|
||||
void DeleteAll();
|
||||
|
||||
PlayerList* GetPlayerList() {
|
||||
return &playerList;
|
||||
}
|
||||
private:
|
||||
//utilities
|
||||
//...
|
||||
|
||||
//members
|
||||
PlayerMap playerMap;
|
||||
int maxPlayers;
|
||||
int ticker;
|
||||
PlayerList playerList;
|
||||
int maxPlayers = 0;
|
||||
int ticker = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+29
-5
@@ -9,8 +9,8 @@ void Server::Init() {
|
||||
if (SDLNet_Init()) {
|
||||
throw(runtime_error("Failed to initialize SDL_net"));
|
||||
}
|
||||
config.Load("config.cfg");
|
||||
netUtil.Open(config.Integer("port"), 512);
|
||||
configUtil.Load("config.cfg");
|
||||
netUtil.Open(configUtil.Integer("port"), sizeof(Packet));
|
||||
running = true;
|
||||
}
|
||||
|
||||
@@ -31,18 +31,42 @@ void Server::Quit() {
|
||||
}
|
||||
|
||||
void Server::HandleInput() {
|
||||
while(netUtil.Receive()) {
|
||||
cout << reinterpret_cast<char*>(netUtil.GetInData()) << endl;
|
||||
}
|
||||
//accept new connections
|
||||
//accept updates from the clients
|
||||
//read the updates from the clients into internal containers
|
||||
Packet packet;
|
||||
while(netUtil.Receive()) {
|
||||
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(Packet));
|
||||
switch(packet.type) {
|
||||
case PacketList::PING:
|
||||
//respond to pings with the server name
|
||||
cout << "responding to ping..." << endl;
|
||||
packet.type = PacketList::PONG;
|
||||
sprintf(packet.pong.buffer, "%s",configUtil.CString("servername"));
|
||||
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(&packet), sizeof(Packet));
|
||||
break;
|
||||
case PacketList::JOINREQUEST:
|
||||
//
|
||||
break;
|
||||
case PacketList::NEWPLAYER:
|
||||
//
|
||||
break;
|
||||
case PacketList::DELETEPLAYER:
|
||||
//
|
||||
break;
|
||||
case PacketList::MOTIONUPDATE:
|
||||
//
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::UpdateWorld() {
|
||||
//update internals ie.
|
||||
// ai
|
||||
// loot drops
|
||||
delta.Calculate();
|
||||
playerMgr.UpdateAll(delta.GetDelta());
|
||||
}
|
||||
|
||||
void Server::HandleOutput() {
|
||||
|
||||
+9
-2
@@ -1,8 +1,12 @@
|
||||
#ifndef SERVER_HPP_
|
||||
#define SERVER_HPP_
|
||||
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "delta.hpp"
|
||||
#include "packet_list.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "player_manager.hpp"
|
||||
|
||||
class Server {
|
||||
public:
|
||||
@@ -18,8 +22,11 @@ public:
|
||||
void HandleOutput();
|
||||
private:
|
||||
bool running = false;
|
||||
ConfigUtility config;
|
||||
Delta delta;
|
||||
|
||||
ConfigUtility configUtil;
|
||||
UDPNetworkUtility netUtil;
|
||||
PlayerManager playerMgr;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user