very buggy system working, but only works properly with one client at a time due to recv blocking
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
#include "player.hpp"
|
||||
|
||||
Player::Player(int id) : clientID(id) {
|
||||
//
|
||||
}
|
||||
|
||||
void Player::Update(int delta) {
|
||||
position += motion * delta;
|
||||
}
|
||||
+25
-6
@@ -5,18 +5,37 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
//TODO
|
||||
|
||||
class Player {
|
||||
public:
|
||||
Player();
|
||||
~Player();
|
||||
Player(int id);
|
||||
~Player() = default;
|
||||
|
||||
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:
|
||||
int clientID;
|
||||
const int clientID;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
std::string avatarName;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
#include "player_manager.hpp"
|
||||
|
||||
@@ -1,4 +1,23 @@
|
||||
#ifndef 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
|
||||
|
||||
+19
-13
@@ -2,17 +2,10 @@
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
Server::Server() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
Server::~Server() {
|
||||
//
|
||||
}
|
||||
|
||||
void Server::Init() {
|
||||
NetworkInit();
|
||||
|
||||
@@ -34,7 +27,7 @@ void Server::Proc() {
|
||||
}
|
||||
|
||||
void Server::Quit() {
|
||||
for (auto it : sockVec) {
|
||||
for (auto it : socketList) {
|
||||
it->Close();
|
||||
delete it;
|
||||
}
|
||||
@@ -46,12 +39,25 @@ void Server::HandleInput() {
|
||||
//accept new connections
|
||||
TCPSocket* sock = new TCPSocket;
|
||||
if (servSock.Accept(sock)) {
|
||||
sockVec.push_back(sock);
|
||||
socketList.push_back(sock);
|
||||
}
|
||||
else {
|
||||
delete sock;
|
||||
}
|
||||
//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
|
||||
//...
|
||||
if (input.size()) {
|
||||
cout << "dumping input from the network" << endl;
|
||||
cout << input << endl;
|
||||
input.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void Server::UpdateWorld() {
|
||||
@@ -65,7 +71,7 @@ void Server::HandleOutput() {
|
||||
//...
|
||||
//selective updates to existing connectons
|
||||
string s = "hello world";
|
||||
for (auto it : sockVec) {
|
||||
for (auto it : socketList) {
|
||||
it->Send(s.c_str(), s.length());
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -4,12 +4,12 @@
|
||||
#include "config_utility.hpp"
|
||||
#include "network.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server();
|
||||
~Server();
|
||||
Server() = default;
|
||||
~Server() = default;
|
||||
|
||||
void Init();
|
||||
void Proc();
|
||||
@@ -19,10 +19,10 @@ public:
|
||||
void UpdateWorld();
|
||||
void HandleOutput();
|
||||
private:
|
||||
bool running;
|
||||
bool running = false;
|
||||
ConfigUtility config;
|
||||
TCPServerSocket servSock;
|
||||
std::vector<TCPSocket*> sockVec;
|
||||
std::list<TCPSocket*> socketList;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user