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
//-------------------------
InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr) {
InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
#ifdef DEBUG
cout << "entering InGame" << endl;
#endif
configUtil = cUtil;
surfaceMgr = sMgr;
netUtil = nUtil;
}
InGame::~InGame() {
+6 -3
View File
@@ -5,11 +5,13 @@
#include "config_utility.hpp"
#include "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
class InGame : public BaseScene {
public:
//Public access members
InGame(ConfigUtility*, SurfaceManager*);
InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~InGame();
protected:
@@ -27,8 +29,9 @@ protected:
virtual void KeyUp(SDL_KeyboardEvent const&);
//members
ConfigUtility* configUtil;
SurfaceManager* surfaceMgr;
ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr;
};
#endif
+33 -5
View File
@@ -9,18 +9,24 @@ using namespace std;
//Public access members
//-------------------------
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr) {
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
#ifdef DEBUG
cout << "entering Lobby" << endl;
#endif
//globals
configUtil = cUtil;
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
//generate the server list
//eventually
SetNextScene(SceneList::TESTSYSTEMS);
}
Lobby::~Lobby() {
@@ -42,11 +48,27 @@ void Lobby::FrameEnd() {
}
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) {
//
pingButton.DrawTo(screen);
}
//-------------------------
@@ -70,6 +92,12 @@ void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
case SDLK_ESCAPE:
QuitEvent();
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 "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
#include "raster_font.hpp"
#include "button.hpp"
class Lobby : public BaseScene {
public:
//Public access members
Lobby(ConfigUtility*, SurfaceManager*);
Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~Lobby();
protected:
@@ -17,6 +22,7 @@ protected:
virtual void FrameStart();
virtual void FrameEnd();
virtual void Update();
virtual void Receive();
virtual void Render(SDL_Surface* const);
//Event handlers
@@ -27,8 +33,12 @@ protected:
virtual void KeyUp(SDL_KeyboardEvent const&);
//members
ConfigUtility* configUtil;
SurfaceManager* surfaceMgr;
ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr;
RasterFont font;
Button pingButton;
};
#endif
+10 -4
View File
@@ -33,10 +33,15 @@ SceneManager::~SceneManager() {
}
void SceneManager::Init() {
if (SDL_Init(SDL_INIT_VIDEO))
if (SDL_Init(SDL_INIT_VIDEO)) {
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");
netUtil.Open(0, sizeof(Packet));
//set the screen from the config file
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
@@ -66,6 +71,7 @@ void SceneManager::Proc() {
void SceneManager::Quit() {
UnloadScene();
SDLNet_Quit();
SDL_Quit();
}
@@ -80,7 +86,7 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
//add scene creation calls here
#ifdef DEBUG
case SceneList::TESTSYSTEMS:
activeScene = new TestSystems(&configUtil, &surfaceMgr);
activeScene = new TestSystems(&configUtil, &surfaceMgr, &netUtil);
break;
#endif
@@ -92,10 +98,10 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
activeScene = new MainMenu(&configUtil, &surfaceMgr);
break;
case SceneList::INGAME:
activeScene = new InGame(&configUtil, &surfaceMgr);
activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil);
break;
case SceneList::LOBBY:
activeScene = new Lobby(&configUtil, &surfaceMgr);
activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil);
break;
#ifdef DEBUG
+3
View File
@@ -6,6 +6,8 @@
#include "config_utility.hpp"
#include "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
#include "SDL/SDL.h"
@@ -28,6 +30,7 @@ private:
ConfigUtility configUtil;
SurfaceManager surfaceMgr;
UDPNetworkUtility netUtil;
};
#endif
+2 -1
View File
@@ -8,12 +8,13 @@ using namespace std;
//Public access members
//-------------------------
TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) {
TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) {
#ifdef DEBUG
cout << "entering TestSystems" << endl;
#endif
configUtil = cUtil;
surfaceMgr = sMgr;
netUtil = nUtil;
playerCounter = currentPlayer = 0;
+6 -3
View File
@@ -5,6 +5,8 @@
#include "config_utility.hpp"
#include "surface_manager.hpp"
#include "udp_network_utility.hpp"
#include "packet_list.hpp"
#include "player_manager.hpp"
#include "delta.hpp"
@@ -16,7 +18,7 @@
class TestSystems : public BaseScene {
public:
//Public access members
TestSystems(ConfigUtility*, SurfaceManager*);
TestSystems(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~TestSystems();
protected:
@@ -39,8 +41,9 @@ protected:
void SendMessage(std::string);
//members
ConfigUtility* configUtil;
SurfaceManager* surfaceMgr;
ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr = nullptr;
UDPNetworkUtility* netUtil = nullptr;
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
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
debug = true
avatar = elliot2.bmp
handle = UserName
servername = foobar
+5 -2
View File
@@ -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
View File
@@ -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
+45
View File
@@ -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();
}
+24 -8
View File
@@ -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
View File
@@ -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
View File
@@ -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