packet is being sent and received, bare-bones ping-pong is working

This commit is contained in:
Kayne Ruse
2013-05-20 01:02:15 +10:00
parent b587759203
commit 7866f46ed5
18 changed files with 342 additions and 63 deletions
+2 -1
View File
@@ -8,12 +8,13 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr) { InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering InGame" << endl; cout << "entering InGame" << endl;
#endif #endif
configUtil = cUtil; configUtil = cUtil;
surfaceMgr = sMgr; surfaceMgr = sMgr;
netUtil = nUtil;
} }
InGame::~InGame() { InGame::~InGame() {
+6 -3
View File
@@ -5,11 +5,13 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
class InGame : public BaseScene { class InGame : public BaseScene {
public: public:
//Public access members //Public access members
InGame(ConfigUtility*, SurfaceManager*); InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~InGame(); virtual ~InGame();
protected: protected:
@@ -27,8 +29,9 @@ protected:
virtual void KeyUp(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&);
//members //members
ConfigUtility* configUtil; ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr; SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr;
}; };
#endif #endif
+33 -5
View File
@@ -9,18 +9,24 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr) { Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering Lobby" << endl; cout << "entering Lobby" << endl;
#endif #endif
//globals
configUtil = cUtil; configUtil = cUtil;
surfaceMgr = sMgr; surfaceMgr = sMgr;
netUtil = nUtil;
//members
font.SetSurface(surfaceMgr->Get("font"));
pingButton.GetImage()->SetSurface(surfaceMgr->Get("button"));
pingButton.SetX(50);
pingButton.SetY(50);
//ping the network, ping the preset "phone home" servers //ping the network, ping the preset "phone home" servers
//generate the server list //generate the server list
//eventually //eventually
SetNextScene(SceneList::TESTSYSTEMS);
} }
Lobby::~Lobby() { Lobby::~Lobby() {
@@ -42,11 +48,27 @@ void Lobby::FrameEnd() {
} }
void Lobby::Update() { void Lobby::Update() {
// Receive();
}
void Lobby::Receive() {
//dump to the console
Packet packet;
while(netUtil->Receive()) {
memcpy(&packet, netUtil->GetInData(), sizeof(Packet));
switch(packet.type) {
case PacketList::PONG:
cout << "dumping..." << endl;
cout << packet.pong.buffer << endl;
break;
//...
}
}
} }
void Lobby::Render(SDL_Surface* const screen) { void Lobby::Render(SDL_Surface* const screen) {
// pingButton.DrawTo(screen);
} }
//------------------------- //-------------------------
@@ -70,6 +92,12 @@ void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
QuitEvent(); QuitEvent();
break; break;
case SDLK_SPACE:
//debugging
Packet packet;
packet.type = PacketList::PING;
netUtil->Send("127.0.0.1", 2000, reinterpret_cast<void*>(&packet), sizeof(Packet));
break;
} }
} }
+13 -3
View File
@@ -5,11 +5,16 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
#include "raster_font.hpp"
#include "button.hpp"
class Lobby : public BaseScene { class Lobby : public BaseScene {
public: public:
//Public access members //Public access members
Lobby(ConfigUtility*, SurfaceManager*); Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~Lobby(); virtual ~Lobby();
protected: protected:
@@ -17,6 +22,7 @@ protected:
virtual void FrameStart(); virtual void FrameStart();
virtual void FrameEnd(); virtual void FrameEnd();
virtual void Update(); virtual void Update();
virtual void Receive();
virtual void Render(SDL_Surface* const); virtual void Render(SDL_Surface* const);
//Event handlers //Event handlers
@@ -27,8 +33,12 @@ protected:
virtual void KeyUp(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&);
//members //members
ConfigUtility* configUtil; ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr; SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr;
RasterFont font;
Button pingButton;
}; };
#endif #endif
+10 -4
View File
@@ -33,10 +33,15 @@ SceneManager::~SceneManager() {
} }
void SceneManager::Init() { void SceneManager::Init() {
if (SDL_Init(SDL_INIT_VIDEO)) if (SDL_Init(SDL_INIT_VIDEO)) {
throw(std::runtime_error("Failed to initialize SDL")); throw(std::runtime_error("Failed to initialize SDL"));
}
if (SDLNet_Init()) {
throw(std::runtime_error("Failed to initialize SDL_net"));
}
configUtil.Load("rsc/config.cfg"); configUtil.Load("rsc/config.cfg");
netUtil.Open(0, sizeof(Packet));
//set the screen from the config file //set the screen from the config file
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
@@ -66,6 +71,7 @@ void SceneManager::Proc() {
void SceneManager::Quit() { void SceneManager::Quit() {
UnloadScene(); UnloadScene();
SDLNet_Quit();
SDL_Quit(); SDL_Quit();
} }
@@ -80,7 +86,7 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
//add scene creation calls here //add scene creation calls here
#ifdef DEBUG #ifdef DEBUG
case SceneList::TESTSYSTEMS: case SceneList::TESTSYSTEMS:
activeScene = new TestSystems(&configUtil, &surfaceMgr); activeScene = new TestSystems(&configUtil, &surfaceMgr, &netUtil);
break; break;
#endif #endif
@@ -92,10 +98,10 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
activeScene = new MainMenu(&configUtil, &surfaceMgr); activeScene = new MainMenu(&configUtil, &surfaceMgr);
break; break;
case SceneList::INGAME: case SceneList::INGAME:
activeScene = new InGame(&configUtil, &surfaceMgr); activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil);
break; break;
case SceneList::LOBBY: case SceneList::LOBBY:
activeScene = new Lobby(&configUtil, &surfaceMgr); activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil);
break; break;
#ifdef DEBUG #ifdef DEBUG
+3
View File
@@ -6,6 +6,8 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
#include "SDL/SDL.h" #include "SDL/SDL.h"
@@ -28,6 +30,7 @@ private:
ConfigUtility configUtil; ConfigUtility configUtil;
SurfaceManager surfaceMgr; SurfaceManager surfaceMgr;
UDPNetworkUtility netUtil;
}; };
#endif #endif
+2 -1
View File
@@ -8,12 +8,13 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) { TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering TestSystems" << endl; cout << "entering TestSystems" << endl;
#endif #endif
configUtil = cUtil; configUtil = cUtil;
surfaceMgr = sMgr; surfaceMgr = sMgr;
netUtil = nUtil;
playerCounter = currentPlayer = 0; playerCounter = currentPlayer = 0;
+6 -3
View File
@@ -5,6 +5,8 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
#include "player_manager.hpp" #include "player_manager.hpp"
#include "delta.hpp" #include "delta.hpp"
@@ -16,7 +18,7 @@
class TestSystems : public BaseScene { class TestSystems : public BaseScene {
public: public:
//Public access members //Public access members
TestSystems(ConfigUtility*, SurfaceManager*); TestSystems(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~TestSystems(); virtual ~TestSystems();
protected: protected:
@@ -39,8 +41,9 @@ protected:
void SendMessage(std::string); void SendMessage(std::string);
//members //members
ConfigUtility* configUtil; ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr; SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr;
PlayerManager playerMgr; PlayerManager playerMgr;
+96
View File
@@ -0,0 +1,96 @@
#ifndef PACKETLIST_HPP_
#define PACKETLIST_HPP_
#include "vector2.hpp"
#include <string>
#define PACKET_STRING_SIZE 256
enum class PacketList {
//networking systems
PING, PONG,
JOINREQUEST,
JOINCONFIRM,
//connections
NEWPLAYER,
DELETEPLAYER,
//play updates
MOTIONUPDATE,
};
//-------------------------
//networking systems
//-------------------------
struct Ping {
PacketList type = PacketList::PING;
char buffer[PACKET_STRING_SIZE];
};
struct Pong {
PacketList type = PacketList::PONG;
char buffer[PACKET_STRING_SIZE];
};
struct JoinRequest {
PacketList type = PacketList::JOINREQUEST;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
};
struct JoinConfirm {
PacketList type = PacketList::JOINCONFIRM;
int playerID;
};
//-------------------------
//connections
//-------------------------
struct NewPlayer {
PacketList type = PacketList::NEWPLAYER;
int playerID;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
};
struct DeletePlayer {
PacketList type = PacketList::DELETEPLAYER;
int playerID;
};
//-------------------------
//play updates
//-------------------------
struct MotionUpdate {
PacketList type = PacketList::MOTIONUPDATE;
int playerID;
Vector2 position;
Vector2 motion;
};
//-------------------------
//this state of this is great
//-------------------------
union Packet {
Packet() {};
~Packet() {};
PacketList type;
//networking systems
Ping ping;
Pong pong;
JoinRequest joinRequest;
JoinConfirm joinConfirm;
//connections
NewPlayer newPlayer;
DeletePlayer deletePlayer;
//play updates
MotionUpdate motionUpdate;
};
#endif
+4
View File
@@ -0,0 +1,4 @@
Button:
SetImageSurface()
SetFontSurface()
end
+32
View File
@@ -46,3 +46,35 @@ UDPNetworkUtility:
UDPpacket packIn UDPpacket packIn
end end
-------------------------
packet_list.hpp:
Ping:
end
Pong:
end
JoinRequest:
avatarName
...
end
JoinConfirm:
yourID
end
NewPlayer:
id
position
motion
avatarName
end
MotionUpdate:
id
position
motion
end
union Packet:
MotionUpdate
end
end
-------------------------
+2
View File
@@ -15,3 +15,5 @@ interface = rsc/graphics/interface
#debugging #debugging
debug = true debug = true
avatar = elliot2.bmp avatar = elliot2.bmp
handle = UserName
servername = foobar
+5 -2
View File
@@ -1,7 +1,10 @@
#include "player.hpp" #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) { void Player::Update(int delta) {
+21 -26
View File
@@ -7,38 +7,33 @@
class Player { class Player {
public: 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 { Vector2 SetPosition(Vector2 v) {return position = v;}
return clientID; 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) { std::string SetAvatar(std::string s) {return avatar = s;}
return position = v; std::string GetAvatar() const {return avatar;}
} std::string SetHandle(std::string s) {return handle = s;}
Vector2 GetPosition() const { std::string GetHandle() const {return handle;}
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;
}
private: private:
const int clientID; int playerID = -1;
int channel = -1; //for networking
Vector2 position; Vector2 position;
Vector2 motion; Vector2 motion;
std::string avatarName; std::string avatar;
std::string handle;
}; };
#endif #endif
+45
View File
@@ -1,2 +1,47 @@
#include "player_manager.hpp" #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();
}
+24 -8
View File
@@ -3,21 +3,37 @@
#include "player.hpp" #include "player.hpp"
#include <map> #include <list>
#include <string> #include <string>
class PlayerManager { class PlayerManager {
public:
PlayerManager() = default;
~PlayerManager() = default;
private: private:
//utilities //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 //members
PlayerMap playerMap; PlayerList playerList;
int maxPlayers; int maxPlayers = 0;
int ticker; int ticker = 0;
}; };
#endif #endif
+29 -5
View File
@@ -9,8 +9,8 @@ void Server::Init() {
if (SDLNet_Init()) { if (SDLNet_Init()) {
throw(runtime_error("Failed to initialize SDL_net")); throw(runtime_error("Failed to initialize SDL_net"));
} }
config.Load("config.cfg"); configUtil.Load("config.cfg");
netUtil.Open(config.Integer("port"), 512); netUtil.Open(configUtil.Integer("port"), sizeof(Packet));
running = true; running = true;
} }
@@ -31,18 +31,42 @@ void Server::Quit() {
} }
void Server::HandleInput() { void Server::HandleInput() {
while(netUtil.Receive()) {
cout << reinterpret_cast<char*>(netUtil.GetInData()) << endl;
}
//accept new connections //accept new connections
//accept updates from the clients //accept updates from the clients
//read the updates from the clients into internal containers //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() { void Server::UpdateWorld() {
//update internals ie. //update internals ie.
// ai // ai
// loot drops // loot drops
delta.Calculate();
playerMgr.UpdateAll(delta.GetDelta());
} }
void Server::HandleOutput() { void Server::HandleOutput() {
+9 -2
View File
@@ -1,8 +1,12 @@
#ifndef SERVER_HPP_ #ifndef SERVER_HPP_
#define SERVER_HPP_ #define SERVER_HPP_
#include "udp_network_utility.hpp" #include "delta.hpp"
#include "packet_list.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include "player_manager.hpp"
class Server { class Server {
public: public:
@@ -18,8 +22,11 @@ public:
void HandleOutput(); void HandleOutput();
private: private:
bool running = false; bool running = false;
ConfigUtility config; Delta delta;
ConfigUtility configUtil;
UDPNetworkUtility netUtil; UDPNetworkUtility netUtil;
PlayerManager playerMgr;
}; };
#endif #endif