very buggy system working, but only works properly with one client at a time due to recv blocking

This commit is contained in:
Kayne Ruse
2013-05-17 19:32:45 +10:00
parent 738320e88e
commit 24e48dec53
12 changed files with 153 additions and 42 deletions
+18 -1
View File
@@ -1,5 +1,6 @@
#include "lobby.hpp" #include "lobby.hpp"
#include <exception>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
@@ -8,10 +9,26 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
Lobby::Lobby() { Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, TCPSocket* sock) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering Lobby" << endl; cout << "entering Lobby" << endl;
#endif #endif
configUtil = cUtil;
surfaceMgr = sMgr;
socket = sock;
//ping the network, ping the preset "phone home" servers
//generate the server list
//eventually
try {
socket->Open(configUtil->CString("ip"), configUtil->Integer("port"));
}
catch(exception& e) {
cerr << "Network Error: " << e.what() << endl;
}
SetNextScene(SceneList::TESTSYSTEMS);
} }
Lobby::~Lobby() { Lobby::~Lobby() {
+13 -4
View File
@@ -3,25 +3,34 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "config_utility.hpp"
#include "surface_manager.hpp"
#include "network.hpp"
class Lobby : public BaseScene { class Lobby : public BaseScene {
public: public:
/* Public access members */ //Public access members
Lobby(); Lobby(ConfigUtility*, SurfaceManager*, TCPSocket*);
virtual ~Lobby(); virtual ~Lobby();
protected: protected:
/* Frame loop */ //Frame loop
virtual void FrameStart(); virtual void FrameStart();
virtual void FrameEnd(); virtual void FrameEnd();
virtual void Update(); virtual void Update();
virtual void Render(SDL_Surface* const); virtual void Render(SDL_Surface* const);
/* Event handlers */ //Event handlers
virtual void MouseMotion(SDL_MouseMotionEvent const&); virtual void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&); virtual void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&); virtual void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&);
//members
ConfigUtility* configUtil;
SurfaceManager* surfaceMgr;
TCPSocket* socket;
}; };
#endif #endif
+1 -1
View File
@@ -70,7 +70,7 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (buttonMap["start"]->MouseButtonUp(button) == Button::State::HOVER) { if (buttonMap["start"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO //TODO
SetNextScene(SceneList::TESTSYSTEMS); SetNextScene(SceneList::LOBBY);
cout << "start" << endl; cout << "start" << endl;
} }
if (buttonMap["options"]->MouseButtonUp(button) == Button::State::HOVER) { if (buttonMap["options"]->MouseButtonUp(button) == Button::State::HOVER) {
+7 -4
View File
@@ -38,6 +38,8 @@ void SceneManager::Init() {
configUtil.Load("rsc/config.cfg"); configUtil.Load("rsc/config.cfg");
NetworkInit();
//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;
if (configUtil.Boolean("screen.f")) { if (configUtil.Boolean("screen.f")) {
@@ -66,6 +68,7 @@ void SceneManager::Proc() {
void SceneManager::Quit() { void SceneManager::Quit() {
UnloadScene(); UnloadScene();
NetworkQuit();
SDL_Quit(); SDL_Quit();
} }
@@ -80,7 +83,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, &socket);
break; break;
#endif #endif
@@ -94,11 +97,11 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
case SceneList::INGAME: case SceneList::INGAME:
activeScene = new InGame(&configUtil, &surfaceMgr); activeScene = new InGame(&configUtil, &surfaceMgr);
break; break;
case SceneList::LOBBY:
activeScene = new Lobby(&configUtil, &surfaceMgr, &socket);
break;
#ifdef DEBUG #ifdef DEBUG
case SceneList::LOBBY:
activeScene = new Lobby();
break;
case SceneList::COMBAT: case SceneList::COMBAT:
activeScene = new Combat(); activeScene = new Combat();
break; break;
+22 -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, TCPSocket* sock) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering TestSystems" << endl; cout << "entering TestSystems" << endl;
#endif #endif
configUtil = cUtil; configUtil = cUtil;
surfaceMgr = sMgr; surfaceMgr = sMgr;
socket = sock;
playerCounter = currentPlayer = 0; playerCounter = currentPlayer = 0;
@@ -21,10 +22,13 @@ TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) {
playerMgr.New(playerCounter++, surfaceMgr->Get("elliot")); playerMgr.New(playerCounter++, surfaceMgr->Get("elliot"));
playerMgr.New(playerCounter++, surfaceMgr->Get("coa")); playerMgr.New(playerCounter++, surfaceMgr->Get("coa"));
playerMgr.New(playerCounter++, surfaceMgr->Get("coa")); playerMgr.New(playerCounter++, surfaceMgr->Get("coa"));
font.SetSurface(surfaceMgr->Get("font"));
} }
TestSystems::~TestSystems() { TestSystems::~TestSystems() {
playerMgr.DeleteAll(); playerMgr.DeleteAll();
socket->Close();
#ifdef DEBUG #ifdef DEBUG
cout << "leaving TestSystems" << endl; cout << "leaving TestSystems" << endl;
#endif #endif
@@ -47,8 +51,16 @@ void TestSystems::Update() {
playerMgr.UpdateAll(delta.GetDelta()); playerMgr.UpdateAll(delta.GetDelta());
} }
string IToS(int i) {
char buffer[20];
memset(buffer, 0, 20);
sprintf(buffer, "%d", i);
return string(buffer);
}
void TestSystems::Render(SDL_Surface* const screen) { void TestSystems::Render(SDL_Surface* const screen) {
playerMgr.DrawAllTo(screen); playerMgr.DrawAllTo(screen);
font.DrawStringTo("FPS: " + IToS(frameRate.GetFrameRate()), screen, 16, 16);
} }
//------------------------- //-------------------------
@@ -75,15 +87,19 @@ void TestSystems::KeyDown(SDL_KeyboardEvent const& key) {
case SDLK_w: case SDLK_w:
playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH); playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH);
SendMessage("move up");
break; break;
case SDLK_s: case SDLK_s:
playerMgr[currentPlayer]->WalkInDirection(Direction::SOUTH); playerMgr[currentPlayer]->WalkInDirection(Direction::SOUTH);
SendMessage("move down");
break; break;
case SDLK_a: case SDLK_a:
playerMgr[currentPlayer]->WalkInDirection(Direction::WEST); playerMgr[currentPlayer]->WalkInDirection(Direction::WEST);
SendMessage("move left");
break; break;
case SDLK_d: case SDLK_d:
playerMgr[currentPlayer]->WalkInDirection(Direction::EAST); playerMgr[currentPlayer]->WalkInDirection(Direction::EAST);
SendMessage("move right");
break; break;
case SDLK_1: case SDLK_1:
@@ -147,3 +163,8 @@ void TestSystems::SwitchToPlayer(int index) {
playerMgr[currentPlayer]->WalkInDirection(Direction::EAST); playerMgr[currentPlayer]->WalkInDirection(Direction::EAST);
} }
} }
void TestSystems::SendMessage(std::string s) {
s += ';';
socket->Send(s.c_str(), s.length());
}
+13 -7
View File
@@ -3,19 +3,21 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "player_manager.hpp"
#include "delta.hpp"
#include "frame_rate.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include "network.hpp"
#include "player_manager.hpp"
#include "delta.hpp"
#include "frame_rate.hpp"
#include "raster_font.hpp"
#include <string> #include <string>
class TestSystems : public BaseScene { class TestSystems : public BaseScene {
public: public:
//Public access members //Public access members
TestSystems(ConfigUtility*, SurfaceManager*); TestSystems(ConfigUtility*, SurfaceManager*, TCPSocket*);
virtual ~TestSystems(); virtual ~TestSystems();
protected: protected:
@@ -35,14 +37,18 @@ protected:
//utilities //utilities
void NewPlayer(int index, std::string avatarName, int x, int y); void NewPlayer(int index, std::string avatarName, int x, int y);
void SwitchToPlayer(int index); void SwitchToPlayer(int index);
void SendMessage(std::string);
//members //members
ConfigUtility* configUtil;
SurfaceManager* surfaceMgr;
TCPSocket* socket;
PlayerManager playerMgr; PlayerManager playerMgr;
Delta delta; Delta delta;
FrameRate frameRate; FrameRate frameRate;
ConfigUtility* configUtil; RasterFont font;
SurfaceManager* surfaceMgr;
int playerCounter; int playerCounter;
int currentPlayer; int currentPlayer;
+9
View File
@@ -0,0 +1,9 @@
#include "player.hpp"
Player::Player(int id) : clientID(id) {
//
}
void Player::Update(int delta) {
position += motion * delta;
}
+25 -6
View File
@@ -5,18 +5,37 @@
#include <string> #include <string>
//TODO
class Player { class Player {
public: public:
Player(); Player(int id);
~Player(); ~Player() = default;
void Update(int); void Update(int);
Vector2 GetPosition(); int GetClientID() const {
return clientID;
}
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;
}
private: private:
int clientID; const int clientID;
Vector2 position; Vector2 position;
Vector2 motion; Vector2 motion;
std::string avatarName; std::string avatarName;
+2
View File
@@ -0,0 +1,2 @@
#include "player_manager.hpp"
+19
View File
@@ -1,4 +1,23 @@
#ifndef PLAYERMANAGER_H_ #ifndef PLAYERMANAGER_H_
#define PLAYERMANAGER_H_ #define PLAYERMANAGER_H_
#include "player.hpp"
#include <map>
#include <string>
class PlayerManager {
public:
PlayerManager() = default;
~PlayerManager() = default;
private:
//utilities
typedef std::map<std::string, Player*> PlayerMap;
//members
PlayerMap playerMap;
int maxPlayers;
int ticker;
};
#endif #endif
+19 -13
View File
@@ -2,17 +2,10 @@
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
#include <algorithm>
using namespace std; using namespace std;
Server::Server() {
running = false;
}
Server::~Server() {
//
}
void Server::Init() { void Server::Init() {
NetworkInit(); NetworkInit();
@@ -34,7 +27,7 @@ void Server::Proc() {
} }
void Server::Quit() { void Server::Quit() {
for (auto it : sockVec) { for (auto it : socketList) {
it->Close(); it->Close();
delete it; delete it;
} }
@@ -46,12 +39,25 @@ void Server::HandleInput() {
//accept new connections //accept new connections
TCPSocket* sock = new TCPSocket; TCPSocket* sock = new TCPSocket;
if (servSock.Accept(sock)) { if (servSock.Accept(sock)) {
sockVec.push_back(sock); socketList.push_back(sock);
}
else {
delete sock;
} }
//accept updates from the clients //accept updates from the clients
//... string input;
for_each(socketList.begin(), socketList.end(), [&input](TCPSocket* sock){ //why use for_each & lamdas?? to give logan a brain hemorrhage
char buffer[512];
memset(buffer, 0, 512);
sock->Recv(buffer, 512);
input += buffer;
});
//read the updates from the clients into internal containers //read the updates from the clients into internal containers
//... if (input.size()) {
cout << "dumping input from the network" << endl;
cout << input << endl;
input.clear();
}
} }
void Server::UpdateWorld() { void Server::UpdateWorld() {
@@ -65,7 +71,7 @@ void Server::HandleOutput() {
//... //...
//selective updates to existing connectons //selective updates to existing connectons
string s = "hello world"; string s = "hello world";
for (auto it : sockVec) { for (auto it : socketList) {
it->Send(s.c_str(), s.length()); it->Send(s.c_str(), s.length());
} }
} }
+5 -5
View File
@@ -4,12 +4,12 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "network.hpp" #include "network.hpp"
#include <vector> #include <list>
class Server { class Server {
public: public:
Server(); Server() = default;
~Server(); ~Server() = default;
void Init(); void Init();
void Proc(); void Proc();
@@ -19,10 +19,10 @@ public:
void UpdateWorld(); void UpdateWorld();
void HandleOutput(); void HandleOutput();
private: private:
bool running; bool running = false;
ConfigUtility config; ConfigUtility config;
TCPServerSocket servSock; TCPServerSocket servSock;
std::vector<TCPSocket*> sockVec; std::list<TCPSocket*> socketList;
}; };
#endif #endif