update networking
This commit is contained in:
parent
3f2d84e8d4
commit
bc0297a9dc
@ -32,10 +32,11 @@ private:
|
|||||||
std::string _url;
|
std::string _url;
|
||||||
MODE _mode;
|
MODE _mode;
|
||||||
int _client_socket;
|
int _client_socket;
|
||||||
std::function<void(SHAPE &S)> _update;
|
std::function<void(SHAPE &S, MODE &M)> _update;
|
||||||
struct sockaddr_in _server_address;
|
struct sockaddr_in _server_address;
|
||||||
struct hostent *_server;
|
struct hostent *_server;
|
||||||
std::map<std::string, std::string> lookup;
|
std::map<std::string, std::string> lookup;
|
||||||
|
std::vector<std::string> explode(std::string &message);
|
||||||
std::string encode(const SHAPE &shape);
|
std::string encode(const SHAPE &shape);
|
||||||
std::string crypt(const std::string &shape);
|
std::string crypt(const std::string &shape);
|
||||||
|
|
||||||
@ -55,7 +56,12 @@ private:
|
|||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Client(MODE mode, int port, std::string url, std::function<void(SHAPE &S)> &update);
|
Client(
|
||||||
|
MODE mode,
|
||||||
|
int port,
|
||||||
|
std::string url,
|
||||||
|
std::function<void(SHAPE &S, MODE &M)> &update);
|
||||||
|
~Client();
|
||||||
void kill();
|
void kill();
|
||||||
bool running();
|
bool running();
|
||||||
void send(bool encrypt, SHAPE shape);
|
void send(bool encrypt, SHAPE shape);
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
#ifndef __BETACORE_COMMON_HPP__
|
#ifndef __BETACORE_COMMON_HPP__
|
||||||
#define __BETACORE_COMMON_HPP__
|
#define __BETACORE_COMMON_HPP__
|
||||||
|
|
||||||
|
#define BUFFER_LENGTH 1024
|
||||||
|
|
||||||
|
#include <string>
|
||||||
namespace betacore
|
namespace betacore
|
||||||
{
|
{
|
||||||
enum SHAPE{
|
|
||||||
|
enum SHAPE
|
||||||
|
{
|
||||||
NONE,
|
NONE,
|
||||||
TRIANGLE,
|
TRIANGLE,
|
||||||
CIRCLE,
|
CIRCLE,
|
||||||
@ -16,8 +20,70 @@ enum MODE
|
|||||||
{
|
{
|
||||||
ALICE,
|
ALICE,
|
||||||
BOB,
|
BOB,
|
||||||
EVE
|
EVE,
|
||||||
|
END
|
||||||
};
|
};
|
||||||
|
class Parser
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static std::string mode(MODE &mode)
|
||||||
|
{
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case ALICE:
|
||||||
|
return "ALICE";
|
||||||
|
case BOB:
|
||||||
|
return "BOB";
|
||||||
|
default:
|
||||||
|
return "END";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static MODE mode(const std::string &mode)
|
||||||
|
{
|
||||||
|
if (mode == "ALICE")
|
||||||
|
return ALICE;
|
||||||
|
if (mode == "BOB")
|
||||||
|
return BOB;
|
||||||
|
return END;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
static SHAPE shape(const std::string &shape)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (shape == "TRIANGLE")
|
||||||
|
return TRIANGLE;
|
||||||
|
if (shape == "CIRCLE")
|
||||||
|
return CIRCLE;
|
||||||
|
if (shape == "SQUARE")
|
||||||
|
return SQUARE;
|
||||||
|
if (shape == "PENTAGON")
|
||||||
|
return PENTAGON;
|
||||||
|
return UNKOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string shape(const SHAPE &shape)
|
||||||
|
{
|
||||||
|
std::string message;
|
||||||
|
switch (shape)
|
||||||
|
{
|
||||||
|
case TRIANGLE:
|
||||||
|
message = "TRIANGLE";
|
||||||
|
break;
|
||||||
|
case CIRCLE:
|
||||||
|
message = "CIRCLE";
|
||||||
|
break;
|
||||||
|
case SQUARE:
|
||||||
|
message = "SQUARE";
|
||||||
|
break;
|
||||||
|
case PENTAGON:
|
||||||
|
message = "PENTAGON";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
message = "UNKOWN";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace betacore
|
||||||
#endif
|
#endif
|
@ -17,7 +17,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#define PI 3.14159265359
|
#define PI 3.14159265359
|
||||||
#include "../includes/common.hpp"
|
#include "common.hpp"
|
||||||
|
#include "client.hpp"
|
||||||
|
|
||||||
namespace betacore
|
namespace betacore
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -31,11 +33,12 @@ private:
|
|||||||
const int SCREEN_HEIGHT = 600;
|
const int SCREEN_HEIGHT = 600;
|
||||||
int MONITOR_WIDTH=1920;
|
int MONITOR_WIDTH=1920;
|
||||||
int MONITOR_HEIGHT=1080;
|
int MONITOR_HEIGHT=1080;
|
||||||
const char *TITLE = "Lesson 01";
|
const char *TITLE ;
|
||||||
bool KEEP_ALIVE = true;
|
bool KEEP_ALIVE = true;
|
||||||
bool SHOW_GRID = false;
|
bool SHOW_GRID = false;
|
||||||
SHAPE USER_CURRENT = TRIANGLE;
|
SHAPE USER_CURRENT = TRIANGLE;
|
||||||
SHAPE GUST_CURRENT = UNKOWN;
|
SHAPE GUST_CURRENT = UNKOWN;
|
||||||
|
MODE ME;
|
||||||
bool KEY_UP_ARROW_ACTIVE = false;
|
bool KEY_UP_ARROW_ACTIVE = false;
|
||||||
bool KEY_DOWN_ARROW_ACTIVE = false;
|
bool KEY_DOWN_ARROW_ACTIVE = false;
|
||||||
bool KEY_LEFT_ARROW_ACTIVE = false;
|
bool KEY_LEFT_ARROW_ACTIVE = false;
|
||||||
@ -43,8 +46,11 @@ private:
|
|||||||
bool KEY_SPACE_ACTIVE = false;
|
bool KEY_SPACE_ACTIVE = false;
|
||||||
bool KEY_RETURN_ACTIVE = false;
|
bool KEY_RETURN_ACTIVE = false;
|
||||||
bool FULL_SCREEN = false;
|
bool FULL_SCREEN = false;
|
||||||
|
Client* client = nullptr;
|
||||||
public:
|
public:
|
||||||
Game();
|
Game(MODE mode,
|
||||||
|
int port,
|
||||||
|
std::string url);
|
||||||
~Game();
|
~Game();
|
||||||
int init();
|
int init();
|
||||||
int initGL();
|
int initGL();
|
||||||
@ -78,6 +84,8 @@ public:
|
|||||||
void top_screen();
|
void top_screen();
|
||||||
void user_screen();
|
void user_screen();
|
||||||
void guest_screen();
|
void guest_screen();
|
||||||
|
|
||||||
|
void update(SHAPE &s,MODE &m);
|
||||||
};
|
};
|
||||||
} // namespace betacore
|
} // namespace betacore
|
||||||
#endif
|
#endif
|
@ -39,6 +39,7 @@ private:
|
|||||||
void shutdown();
|
void shutdown();
|
||||||
void listener();
|
void listener();
|
||||||
void read_socket(int client);
|
void read_socket(int client);
|
||||||
|
void forward_message(int client,char buffer[BUFFER_LENGTH] );
|
||||||
public:
|
public:
|
||||||
Server(int port);
|
Server(int port);
|
||||||
void off();
|
void off();
|
||||||
|
4
makefile
4
makefile
@ -11,12 +11,12 @@ all: game | driver
|
|||||||
|
|
||||||
driver: client_driver | server_driver
|
driver: client_driver | server_driver
|
||||||
|
|
||||||
game: $(SOURCE_DIR)game.cpp | make_dir
|
|
||||||
$(COMPILER) $(INCLUDES_DIR) $(SOURCE_DIR)game.cpp $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OUTPUT_DIR)/game.out
|
|
||||||
client_lib: $(SOURCE_DIR)client.cpp | make_dir
|
client_lib: $(SOURCE_DIR)client.cpp | make_dir
|
||||||
$(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)client.cpp -lpqxx -o $(CLIENT_LIB)
|
$(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)client.cpp -lpqxx -o $(CLIENT_LIB)
|
||||||
client_driver: $(SOURCE_DIR)client_driver.cpp | client_lib
|
client_driver: $(SOURCE_DIR)client_driver.cpp | client_lib
|
||||||
$(COMPILER) $(INCLUDES_DIR) $(CLIENT_LIB) $(SOURCE_DIR)client_driver.cpp $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OUTPUT_DIR)/client.out
|
$(COMPILER) $(INCLUDES_DIR) $(CLIENT_LIB) $(SOURCE_DIR)client_driver.cpp $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OUTPUT_DIR)/client.out
|
||||||
|
game: $(SOURCE_DIR)game.cpp | client_lib
|
||||||
|
$(COMPILER) $(INCLUDES_DIR) $(SOURCE_DIR)game.cpp $(CLIENT_LIB) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OUTPUT_DIR)/game.out
|
||||||
server_lib: $(SOURCE_DIR)server.cpp | make_dir
|
server_lib: $(SOURCE_DIR)server.cpp | make_dir
|
||||||
$(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)server.cpp -lpqxx -o $(SERVER_LIB)
|
$(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)server.cpp -lpqxx -o $(SERVER_LIB)
|
||||||
server_driver: $(SOURCE_DIR)server_driver.cpp | server_lib
|
server_driver: $(SOURCE_DIR)server_driver.cpp | server_lib
|
||||||
|
2
run.sh
2
run.sh
@ -1,2 +1,2 @@
|
|||||||
export SDL_VIDEO_X11_VISUALID=
|
export SDL_VIDEO_X11_VISUALID=
|
||||||
./bin/game
|
./bin/game.out
|
@ -2,32 +2,40 @@
|
|||||||
|
|
||||||
namespace betacore
|
namespace betacore
|
||||||
{
|
{
|
||||||
Client::Client(MODE mode, int port, std::string url, std::function<void(SHAPE &S)> &update) : _mode(mode),
|
Client::Client(
|
||||||
_port(port),
|
MODE mode,
|
||||||
_url(url),
|
int port,
|
||||||
_update(update)
|
std::string url,
|
||||||
|
std::function<void(SHAPE &S, MODE &M)> &update) : _mode(mode),
|
||||||
|
_port(port),
|
||||||
|
_url(url),
|
||||||
|
_update(update)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
lookup["TRIANGLE"] = "APPLE";
|
||||||
|
lookup["CIRCLE"] = "ORANGE";
|
||||||
|
lookup["SQUARE"] = "BANANA";
|
||||||
|
lookup["PENTAGON"] = "PINEAPPLE";
|
||||||
|
|
||||||
|
lookup["APPLE"] = "TRIANGLE";
|
||||||
|
lookup["ORANGE"] = "CIRCLE";
|
||||||
|
lookup["BANANA"] = "SQUARE";
|
||||||
|
lookup["PINEAPPLE"] = "PENTAGON";
|
||||||
this->start();
|
this->start();
|
||||||
lookup["TRIANGLE"]="APPLE";
|
}
|
||||||
lookup["CIRCLE"]="ORANGE";
|
Client::~Client(){
|
||||||
lookup["SQUARE"]="BANANA";
|
this->_running= false;
|
||||||
lookup["PENTAGON"]="PINEAPPLE";
|
this->_online=false;
|
||||||
|
|
||||||
lookup["APPLE"]="TRIANGLE";
|
|
||||||
lookup["ORANGE"]="CIRCLE";
|
|
||||||
lookup["BANANA"]="SQUARE";
|
|
||||||
lookup["PINEAPPLE"]="PENTAGON";
|
|
||||||
|
|
||||||
}
|
}
|
||||||
void Client::start()
|
void Client::start()
|
||||||
{
|
{
|
||||||
std::cout
|
std::cout
|
||||||
<< "Start"
|
<< "Start"
|
||||||
<< "\nConnecting to: " << _url << ":" <<_port
|
<< "\nConnecting to: " << _url << ":" << _port
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
//Create a socket point
|
//Create a socket point
|
||||||
this->_client_socket = socket(AF_INET, SOCK_STREAM, 0);
|
this->_client_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
_server = gethostbyname(_url.c_str());
|
_server = gethostbyname(_url.c_str());
|
||||||
if (_client_socket < 0)
|
if (_client_socket < 0)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR opening socket" << std::endl;
|
std::cout << "ERROR opening socket" << std::endl;
|
||||||
@ -39,25 +47,29 @@ void Client::start()
|
|||||||
bzero((char *)&_server_address, sizeof(_server_address));
|
bzero((char *)&_server_address, sizeof(_server_address));
|
||||||
_server_address.sin_family = AF_INET;
|
_server_address.sin_family = AF_INET;
|
||||||
|
|
||||||
bcopy((char *)_server->h_addr, (char *)&_server_address.sin_addr.s_addr, _server->h_length);
|
bcopy(
|
||||||
|
(char *)_server->h_addr,
|
||||||
|
(char *)&_server_address.sin_addr.s_addr,
|
||||||
|
_server->h_length);
|
||||||
_server_address.sin_port = htons(_port);
|
_server_address.sin_port = htons(_port);
|
||||||
|
|
||||||
|
|
||||||
// Now connect to the server
|
// Now connect to the server
|
||||||
if (connect(_client_socket, (struct sockaddr *)&_server_address, sizeof(_server_address)) < 0)
|
if (connect(
|
||||||
|
_client_socket,
|
||||||
|
(struct sockaddr *)&_server_address,
|
||||||
|
sizeof(_server_address)) < 0)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR connecting" << std::endl;
|
std::cout << "ERROR connecting" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this->_online = true;
|
this->_online = true;
|
||||||
this->_running = true;
|
this->_running = true;
|
||||||
std::thread listen_thread([this] { this->listener(); } );
|
std::thread listen_thread([this] { this->listener(); });
|
||||||
listen_thread.detach();
|
listen_thread.detach();
|
||||||
|
|
||||||
std::cout
|
std::cout
|
||||||
<< "Client Connected"
|
<< "Client Connected"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
void Client::stop()
|
void Client::stop()
|
||||||
{
|
{
|
||||||
@ -78,17 +90,18 @@ bool Client::running()
|
|||||||
return this->_running;
|
return this->_running;
|
||||||
}
|
}
|
||||||
|
|
||||||
SHAPE Client::parse(const std::string &message)
|
std::vector<std::string> Client::explode(std::string &message){
|
||||||
{
|
|
||||||
std::stringstream stream(message);
|
std::stringstream stream(message);
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
std::vector<std::string> words;
|
std::vector<std::string> words;
|
||||||
while (std::getline(stream, tmp, ':'))
|
while (std::getline(stream, tmp, ':'))
|
||||||
words.push_back(tmp);
|
words.push_back(tmp);
|
||||||
|
return words;
|
||||||
|
}
|
||||||
|
|
||||||
if (words.size() < 2)
|
SHAPE Client::parse(const std::string &message)
|
||||||
return NONE;
|
{
|
||||||
tmp = words.at(1);
|
std::string tmp = message;
|
||||||
SHAPE result = decode(tmp);
|
SHAPE result = decode(tmp);
|
||||||
if (result != UNKOWN)
|
if (result != UNKOWN)
|
||||||
return result;
|
return result;
|
||||||
@ -112,12 +125,11 @@ SHAPE Client::decode(const std::string &shape)
|
|||||||
std::string Client::crypt(const std::string &message)
|
std::string Client::crypt(const std::string &message)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
// std::string output = std::string(message);
|
// std::string output = std::string(message);
|
||||||
|
|
||||||
// for (int i = 0; i < message.size(); i++)
|
// for (int i = 0; i < message.size(); i++)
|
||||||
// output.at(i) = message[i] ^ key;
|
// output.at(i) = message[i] ^ key;
|
||||||
std::cout << "Crypt::" << message << " : " << lookup[message] << std::endl;
|
std::cout << "Crypt::" << message << " : " << lookup[message] << std::endl;
|
||||||
return lookup[message];
|
return lookup[message];
|
||||||
}
|
}
|
||||||
std::string Client::encode(const SHAPE &shape)
|
std::string Client::encode(const SHAPE &shape)
|
||||||
@ -146,45 +158,47 @@ std::string Client::encode(const SHAPE &shape)
|
|||||||
void Client::send(bool encrypt, SHAPE shape)
|
void Client::send(bool encrypt, SHAPE shape)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::string message = "SHAPE:" ;
|
std::string message = "SHAPE:";
|
||||||
message += encrypt ? crypt(encode(shape)) : encode(shape) ;
|
message += encrypt ? crypt(encode(shape)) : encode(shape);
|
||||||
message += ":END";
|
message += ":";
|
||||||
std::cout << "Sending Message: " << message << "\n" << std::endl;
|
message += Parser::mode(this->_mode);
|
||||||
char buffer[1024];
|
std::cout << "Sending Message: " << message << "\n"
|
||||||
bzero(buffer,1024);
|
<< std::endl;
|
||||||
// for(int i=0; i<1024-1 && i < message.length(); i++){
|
char buffer[BUFFER_LENGTH];
|
||||||
// buffer[i]= message.at(i);
|
bzero(buffer, BUFFER_LENGTH);
|
||||||
// }
|
strcpy(buffer, message.c_str());
|
||||||
strcpy(buffer,message.c_str());
|
buffer[BUFFER_LENGTH - 1] = '\0';
|
||||||
buffer[1024-1]='\0';
|
int x = write(this->_client_socket, buffer, strlen(buffer));
|
||||||
int x =write(this->_client_socket, buffer, strlen(buffer));
|
|
||||||
if (x < 0)
|
if (x < 0)
|
||||||
{
|
{
|
||||||
std::cout << "Failed send to server" << std::endl;
|
std::cout << "Failed send to server" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::listener()
|
void Client::listener()
|
||||||
{
|
{
|
||||||
std::cout << "listener" << std::endl;
|
std::cout << "listener" << std::endl;
|
||||||
char buffer[1024];
|
char buffer[BUFFER_LENGTH];
|
||||||
while (_online)
|
while (_online)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Now read server response
|
// Now read server response
|
||||||
|
|
||||||
bzero(buffer, 1024);
|
bzero(buffer, BUFFER_LENGTH);
|
||||||
std::cout << "Wating For message" << std::endl;
|
std::cout << "Wating For message" << std::endl;
|
||||||
if (read(_client_socket, buffer, 1024-1) < 0)
|
if (read(_client_socket, buffer, BUFFER_LENGTH - 1) < 0)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR reading from socket" << std::endl;
|
std::cout << "ERROR reading from socket" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string message(buffer);
|
std::string message(buffer);
|
||||||
std::cout << "Got Message: " << message << std::endl;
|
std::cout << "Got Message: " << message << std::endl;
|
||||||
SHAPE shape = parse(buffer);
|
auto parts = explode(message);
|
||||||
_update(shape);
|
if(parts.size() < 3)
|
||||||
|
return;
|
||||||
|
SHAPE shape = parse(parts.at(1));
|
||||||
|
MODE mode = Parser::mode(parts.at(2));
|
||||||
|
_update(shape,mode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace betacore
|
} // namespace betacore
|
@ -1,21 +1,30 @@
|
|||||||
#include "client.hpp"
|
#include "client.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void test(betacore::SHAPE &s){
|
void test(betacore::SHAPE &s, betacore::MODE &m)
|
||||||
std::cout << "It worked\n" << std::endl;
|
{
|
||||||
|
std::cout
|
||||||
|
<< "Got Shape::"
|
||||||
|
<< betacore::Parser::shape(s)
|
||||||
|
<< "\tFrom"
|
||||||
|
<< betacore::Parser::mode(m)
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
int main(int argc, char * argv[]){
|
int main(int argc, char *argv[])
|
||||||
std::function<void(betacore::SHAPE &S)> fn= test;
|
{
|
||||||
betacore::Client client(betacore::ALICE,4444, "localhost",fn);
|
std::function<void(betacore::SHAPE & s, betacore::MODE & m)> fn = test;
|
||||||
|
betacore::Client client(betacore::ALICE, 4444, "localhost", fn);
|
||||||
client.send(false, betacore::TRIANGLE);
|
client.send(false, betacore::TRIANGLE);
|
||||||
client.send(true, betacore::TRIANGLE);
|
client.send(true, betacore::TRIANGLE);
|
||||||
//s.off();
|
//s.off();
|
||||||
//while(client.running()){}
|
//while(client.running()){}
|
||||||
std::string a;
|
std::string a;
|
||||||
while(client.running()){
|
while (client.running())
|
||||||
|
{
|
||||||
std::cin >> a;
|
std::cin >> a;
|
||||||
client.send(false, betacore::TRIANGLE);
|
client.send(false, betacore::TRIANGLE);
|
||||||
if (a == "q") client.kill();
|
if (a == "q")
|
||||||
|
client.kill();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
145
source/game.cpp
145
source/game.cpp
@ -1,20 +1,38 @@
|
|||||||
#include "game.hpp"
|
#include "game.hpp"
|
||||||
|
void gluPerspective(double fovy,double aspect, double zNear, double zFar)
|
||||||
|
{
|
||||||
|
// Start in projection mode.
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
double xmin, xmax, ymin, ymax;
|
||||||
|
ymax = zNear * tan(fovy * M_PI / 360.0);
|
||||||
|
ymin = -ymax;
|
||||||
|
xmin = ymin * aspect;
|
||||||
|
xmax = ymax * aspect;
|
||||||
|
glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
|
||||||
|
}
|
||||||
namespace betacore
|
namespace betacore
|
||||||
{
|
{
|
||||||
Game::Game()
|
Game::Game(MODE mode,
|
||||||
|
int port,
|
||||||
|
std::string url): ME(mode)
|
||||||
{
|
{
|
||||||
|
this->TITLE = Parser::mode(mode).c_str();
|
||||||
|
|
||||||
printf("%d\t%d\n", this->MONITOR_WIDTH,this->MONITOR_HEIGHT);
|
printf("%s\t%d\t%d\n",this->TITLE, this->MONITOR_WIDTH, this->MONITOR_HEIGHT);
|
||||||
if (init())
|
if (init())
|
||||||
{
|
{
|
||||||
printf("Error loading init\n");
|
printf("Error loading init\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
using namespace std::placeholders;
|
||||||
|
std::function<void(SHAPE & s, MODE & m)> fn =
|
||||||
|
std::bind(&Game::update, this, _1, _2);
|
||||||
|
this->client = new Client(mode, port, url, fn);
|
||||||
// SDL_DisplayMode DM;
|
// SDL_DisplayMode DM;
|
||||||
// SDL_GetCurrentDisplayMode(0, &DM);
|
// SDL_GetCurrentDisplayMode(0, &DM);
|
||||||
this->MONITOR_HEIGHT=1080;
|
this->MONITOR_HEIGHT = 1080;
|
||||||
this->MONITOR_WIDTH=1920;
|
this->MONITOR_WIDTH = 1920;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
|
|
||||||
while (this->KEEP_ALIVE)
|
while (this->KEEP_ALIVE)
|
||||||
@ -25,8 +43,11 @@ Game::Game()
|
|||||||
}
|
}
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
Game::~Game() {
|
Game::~Game()
|
||||||
|
{
|
||||||
|
if (this->client != nullptr)
|
||||||
|
delete this->client;
|
||||||
|
this->client = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Game::init()
|
int Game::init()
|
||||||
@ -122,7 +143,11 @@ int Game::initGL()
|
|||||||
glViewport(0, 0, this->SCREEN_WIDTH, this->SCREEN_HEIGHT);
|
glViewport(0, 0, this->SCREEN_WIDTH, this->SCREEN_HEIGHT);
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
gluPerspective(
|
||||||
|
45.0f,
|
||||||
|
(GLfloat)this->SCREEN_WIDTH / (GLfloat)this->SCREEN_HEIGHT,
|
||||||
|
0.1f,
|
||||||
|
100.0f);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -240,8 +265,9 @@ void Game::rectangle()
|
|||||||
glVertex3f(-2.5f, -1.0f, 0.0f); // Bottom Left
|
glVertex3f(-2.5f, -1.0f, 0.0f); // Bottom Left
|
||||||
glEnd();
|
glEnd();
|
||||||
}
|
}
|
||||||
void Game::cross(bool filled){
|
void Game::cross(bool filled)
|
||||||
double width = 0.15;
|
{
|
||||||
|
double width = 0.15;
|
||||||
if (filled)
|
if (filled)
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
else
|
else
|
||||||
@ -273,43 +299,43 @@ void Game::compass()
|
|||||||
|
|
||||||
//MINI TRIANGLE
|
//MINI TRIANGLE
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glScalef(0.12f, 0.12f, 0.0f);
|
glScalef(0.12f, 0.12f, 0.0f);
|
||||||
glTranslatef(0.0f, 2.6f, 0.0f);
|
glTranslatef(0.0f, 2.6f, 0.0f);
|
||||||
this->triangle(this->KEY_UP_ARROW_ACTIVE);
|
this->triangle(this->KEY_UP_ARROW_ACTIVE);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
// MINI SQUARE
|
// MINI SQUARE
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glScalef(0.12f, 0.12f, 0.0f);
|
glScalef(0.12f, 0.12f, 0.0f);
|
||||||
glTranslatef(0.0f, -2.6f, 0.0f);
|
glTranslatef(0.0f, -2.6f, 0.0f);
|
||||||
this->square(this->KEY_DOWN_ARROW_ACTIVE);
|
this->square(this->KEY_DOWN_ARROW_ACTIVE);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
// MINI PENTAGON
|
// MINI PENTAGON
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glScalef(0.12f, 0.12f, 0.0f);
|
glScalef(0.12f, 0.12f, 0.0f);
|
||||||
glTranslatef(-2.56f, 0.0f, 0.0f);
|
glTranslatef(-2.56f, 0.0f, 0.0f);
|
||||||
this->pentagon(this->KEY_LEFT_ARROW_ACTIVE);
|
this->pentagon(this->KEY_LEFT_ARROW_ACTIVE);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
// MINI CIRCLE
|
// MINI CIRCLE
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glScalef(0.12f, 0.12f, 0.0f);
|
glScalef(0.12f, 0.12f, 0.0f);
|
||||||
glTranslatef(2.56f, 0.0f, 0.0f);
|
glTranslatef(2.56f, 0.0f, 0.0f);
|
||||||
glColor3f(0.0f, 1.0f, 0.0f);
|
glColor3f(0.0f, 1.0f, 0.0f);
|
||||||
this->circle(this->KEY_RIGHT_ARROW_ACTIVE);
|
this->circle(this->KEY_RIGHT_ARROW_ACTIVE);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glScalef(0.35f, 0.35f, 0.0f);
|
glScalef(0.35f, 0.35f, 0.0f);
|
||||||
glTranslatef(7.0f, 0.0f, 0.0f);
|
glTranslatef(7.0f, 0.0f, 0.0f);
|
||||||
glColor3f(0.0f, 1.0f, 1.0f);
|
glColor3f(0.0f, 1.0f, 1.0f);
|
||||||
this->circle(this->KEY_SPACE_ACTIVE);
|
this->circle(this->KEY_SPACE_ACTIVE);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glScalef(0.35f, 0.35f, 0.0f);
|
glScalef(0.35f, 0.35f, 0.0f);
|
||||||
glTranslatef(10.0f, 0.0f, 0.0f);
|
glTranslatef(10.0f, 0.0f, 0.0f);
|
||||||
glColor3f(1.0f, 1.0f, 0.0f);
|
glColor3f(1.0f, 1.0f, 0.0f);
|
||||||
this->circle(this->KEY_RETURN_ACTIVE);
|
this->circle(this->KEY_RETURN_ACTIVE);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
@ -320,15 +346,15 @@ void Game::circle()
|
|||||||
}
|
}
|
||||||
void Game::circle(bool filled)
|
void Game::circle(bool filled)
|
||||||
{
|
{
|
||||||
//glBegin(GL_LINE_LOOP);
|
//glBegin(GL_LINE_LOOP);
|
||||||
// for (int i = 0; i <= 300; i++)
|
// for (int i = 0; i <= 300; i++)
|
||||||
// {
|
// {
|
||||||
// double angle = 2 * PI * i / 300;
|
// double angle = 2 * PI * i / 300;
|
||||||
// double x = cos(angle);
|
// double x = cos(angle);
|
||||||
// double y = sin(angle);
|
// double y = sin(angle);
|
||||||
// glVertex2d(x, y);
|
// glVertex2d(x, y);
|
||||||
// }
|
// }
|
||||||
// glEnd();
|
// glEnd();
|
||||||
|
|
||||||
double radius = 1;
|
double radius = 1;
|
||||||
|
|
||||||
@ -336,8 +362,6 @@ void Game::circle(bool filled)
|
|||||||
|
|
||||||
glBegin(GL_POLYGON);
|
glBegin(GL_POLYGON);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
else
|
else
|
||||||
glBegin(GL_LINE_LOOP);
|
glBegin(GL_LINE_LOOP);
|
||||||
|
|
||||||
@ -363,8 +387,8 @@ void Game::pentagon(bool filled)
|
|||||||
glColor3f(1.0f, 0.0f, 1.0f);
|
glColor3f(1.0f, 0.0f, 1.0f);
|
||||||
float angleIncrement = 360.0f / 5.0f;
|
float angleIncrement = 360.0f / 5.0f;
|
||||||
angleIncrement *= PI / 180.0f;
|
angleIncrement *= PI / 180.0f;
|
||||||
if ( filled)
|
if (filled)
|
||||||
glBegin(GL_TRIANGLE_FAN);
|
glBegin(GL_TRIANGLE_FAN);
|
||||||
else
|
else
|
||||||
glBegin(GL_LINE_LOOP);
|
glBegin(GL_LINE_LOOP);
|
||||||
float angle = 0.0f;
|
float angle = 0.0f;
|
||||||
@ -438,7 +462,7 @@ void Game::guest_screen()
|
|||||||
break;
|
break;
|
||||||
case UNKOWN:
|
case UNKOWN:
|
||||||
float gray = 105.0 / 255.0;
|
float gray = 105.0 / 255.0;
|
||||||
glColor3f(gray,gray,gray);
|
glColor3f(gray, gray, gray);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glRotatef(-45.0f, 0.0f, 0.0f, 1.0f);
|
glRotatef(-45.0f, 0.0f, 0.0f, 1.0f);
|
||||||
glScalef(2.0f, 2.0f, 0.0f);
|
glScalef(2.0f, 2.0f, 0.0f);
|
||||||
@ -448,7 +472,6 @@ void Game::guest_screen()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
void Game::top_screen()
|
void Game::top_screen()
|
||||||
@ -481,7 +504,7 @@ void Game::resize(int width, int height)
|
|||||||
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -527,30 +550,34 @@ void Game::key_down(SDL_Keycode key_code)
|
|||||||
break;
|
break;
|
||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
this->KEY_SPACE_ACTIVE = true;
|
this->KEY_SPACE_ACTIVE = true;
|
||||||
|
this->on_key_space();
|
||||||
break;
|
break;
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
this->KEY_RETURN_ACTIVE = true;
|
this->KEY_RETURN_ACTIVE = true;
|
||||||
|
this->on_key_enter();
|
||||||
break;
|
break;
|
||||||
case SDLK_F11:
|
case SDLK_F11:
|
||||||
|
|
||||||
if(FULL_SCREEN){
|
if (FULL_SCREEN)
|
||||||
|
{
|
||||||
SDL_SetWindowPosition(this->WINDOW, 100, 100);
|
SDL_SetWindowPosition(this->WINDOW, 100, 100);
|
||||||
SDL_RestoreWindow(this->WINDOW);
|
SDL_RestoreWindow(this->WINDOW);
|
||||||
//SDL_SetWindowResizable(this->WINDOW, SDL_TRUE);
|
//SDL_SetWindowResizable(this->WINDOW, SDL_TRUE);
|
||||||
//SDL_SetWindowBordered(this->WINDOW, 1);
|
//SDL_SetWindowBordered(this->WINDOW, 1);
|
||||||
SDL_SetWindowSize(this->WINDOW,SCREEN_WIDTH, SCREEN_HEIGHT);
|
SDL_SetWindowSize(this->WINDOW, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
//SDL_SetWindowFullscreen(this->WINDOW,SDL_WINDOW_FULLSCREEN );
|
//SDL_SetWindowFullscreen(this->WINDOW,SDL_WINDOW_FULLSCREEN );
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
SDL_SetWindowPosition(this->WINDOW, 0, 0);
|
SDL_SetWindowPosition(this->WINDOW, 0, 0);
|
||||||
//SDL_SetWindowResizable(this->WINDOW, SDL_FALSE);
|
//SDL_SetWindowResizable(this->WINDOW, SDL_FALSE);
|
||||||
//SDL_SetWindowBordered(this->WINDOW, 0);
|
//SDL_SetWindowBordered(this->WINDOW, 0);
|
||||||
SDL_SetWindowSize(this->WINDOW,MONITOR_WIDTH, MONITOR_HEIGHT);
|
SDL_SetWindowSize(this->WINDOW, MONITOR_WIDTH, MONITOR_HEIGHT);
|
||||||
//SDL_SetWindowFullscreen(this->WINDOW,0 );
|
//SDL_SetWindowFullscreen(this->WINDOW,0 );
|
||||||
}
|
}
|
||||||
this->FULL_SCREEN = !this->FULL_SCREEN;
|
this->FULL_SCREEN = !this->FULL_SCREEN;
|
||||||
|
|
||||||
SDL_ShowCursor(this->FULL_SCREEN);
|
SDL_ShowCursor(this->FULL_SCREEN);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -662,15 +689,23 @@ void Game::on_key_right_arrow()
|
|||||||
}
|
}
|
||||||
void Game::on_key_space()
|
void Game::on_key_space()
|
||||||
{
|
{
|
||||||
|
this->client->send(false,this->USER_CURRENT);
|
||||||
}
|
}
|
||||||
void Game::on_key_enter()
|
void Game::on_key_enter()
|
||||||
{
|
{
|
||||||
|
this->client->send(true,this->USER_CURRENT);
|
||||||
|
}
|
||||||
|
void Game::update(SHAPE &s, MODE &m)
|
||||||
|
{
|
||||||
|
if (m == ME)
|
||||||
|
this->USER_CURRENT = s;
|
||||||
|
else
|
||||||
|
this->GUST_CURRENT = s;
|
||||||
}
|
}
|
||||||
} // namespace betacore
|
} // namespace betacore
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
betacore::Game game;
|
betacore::Game game(betacore::BOB, 4444, "localhost");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -1,6 +1,5 @@
|
|||||||
#include "../includes/server.hpp"
|
#include "../includes/server.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace betacore
|
namespace betacore
|
||||||
{
|
{
|
||||||
Server::Server(int port)
|
Server::Server(int port)
|
||||||
@ -14,10 +13,10 @@ void Server::start()
|
|||||||
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (this->server_socket < 0)
|
if (this->server_socket < 0)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR on opening socket"<< std::endl;
|
std::cout << "ERROR on opening socket" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::cout << "Socket Connected"<< std::endl;
|
std::cout << "Socket Connected" << std::endl;
|
||||||
|
|
||||||
bzero((char *)&server_address, sizeof(server_address));
|
bzero((char *)&server_address, sizeof(server_address));
|
||||||
|
|
||||||
@ -25,38 +24,41 @@ void Server::start()
|
|||||||
server_address.sin_addr.s_addr = INADDR_ANY;
|
server_address.sin_addr.s_addr = INADDR_ANY;
|
||||||
server_address.sin_port = htons(this->port);
|
server_address.sin_port = htons(this->port);
|
||||||
|
|
||||||
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
|
if (bind(
|
||||||
|
server_socket,
|
||||||
|
(struct sockaddr *)&server_address,
|
||||||
|
sizeof(server_address)) < 0)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR on binding "<< std::endl;
|
std::cout << "ERROR on binding " << std::endl;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::cout << "Bound"<< std::endl;
|
std::cout << "Bound" << std::endl;
|
||||||
online = true;
|
online = true;
|
||||||
server_running=true;
|
server_running = true;
|
||||||
std::cout << "Server is online"<< std::endl;
|
std::cout << "Server is online" << std::endl;
|
||||||
signal(SIGPIPE,SIG_IGN);
|
signal(SIGPIPE, SIG_IGN); // Prevents issue with sig kill on client
|
||||||
std::thread listen_thread([this] { this->listener(); } );
|
std::thread listen_thread([this] { this->listener(); });
|
||||||
listen_thread.detach();
|
listen_thread.detach();
|
||||||
}
|
}
|
||||||
void Server::listener()
|
void Server::listener()
|
||||||
{
|
{
|
||||||
std::cout << "Listener "<< online << std::endl;
|
std::cout << "Listener " << online << std::endl;
|
||||||
struct sockaddr_in client_address;
|
struct sockaddr_in client_address;
|
||||||
listen(this->server_socket, 10);
|
listen(this->server_socket, 10);
|
||||||
while (online) {
|
while (online)
|
||||||
|
{
|
||||||
|
|
||||||
socklen_t client_socket_length = sizeof(client_address);
|
socklen_t client_socket_length = sizeof(client_address);
|
||||||
int client_number =
|
int client_number =
|
||||||
accept(server_socket, (struct sockaddr *)&client_address, &client_socket_length);
|
accept(server_socket, (struct sockaddr *)&client_address, &client_socket_length);
|
||||||
this->clients.push_back(client_number);
|
this->clients.push_back(client_number);
|
||||||
std::cout << "New Conenction"<< std::endl;
|
std::cout << "New Conenction" << std::endl;
|
||||||
if (client_number < 0)
|
if (client_number < 0)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR on accept"<< std::endl;
|
std::cout << "ERROR on accept" << std::endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::thread socket_thread([this,client_number]{this->read_socket(client_number);});
|
std::thread socket_thread([this, client_number] { this->read_socket(client_number); });
|
||||||
socket_thread.detach();
|
socket_thread.detach();
|
||||||
}
|
}
|
||||||
this->shutdown();
|
this->shutdown();
|
||||||
@ -64,52 +66,71 @@ void Server::listener()
|
|||||||
void Server::read_socket(int client)
|
void Server::read_socket(int client)
|
||||||
{
|
{
|
||||||
std::cout
|
std::cout
|
||||||
<< "Ready To Read "
|
<< "Ready To Read "
|
||||||
<< client
|
<< client
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
int n;
|
int n;
|
||||||
char buffer[1024];
|
char buffer[BUFFER_LENGTH];
|
||||||
while (online &&
|
while (online &&
|
||||||
std::find(clients.begin(), clients.end(), client) != clients.end())
|
std::find(clients.begin(), clients.end(), client) != clients.end())
|
||||||
{
|
{
|
||||||
|
|
||||||
|
bzero(buffer, BUFFER_LENGTH);
|
||||||
|
|
||||||
bzero(buffer, 1024);
|
n = read(client, buffer, BUFFER_LENGTH - 1);
|
||||||
|
|
||||||
n = read(client, buffer, 1024-1);
|
|
||||||
|
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR reading from socket"<< std::endl;
|
std::cout << "ERROR reading from socket" << std::endl;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
printf("Here is the message: %s\n", buffer);
|
|
||||||
n = write(client, "I got your mesage\n", 18);
|
|
||||||
|
|
||||||
if (n < 0)
|
|
||||||
{
|
|
||||||
std::cout << "ERROR Writing socket"<< std::endl;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
this->forward_message(client, buffer);
|
||||||
}
|
}
|
||||||
|
std::cout << "Closing Connection for " << client << std::endl;
|
||||||
clients.erase(
|
clients.erase(
|
||||||
std::remove(clients.begin(), clients.end(), client), clients.end());
|
std::remove(clients.begin(), clients.end(), client), clients.end());
|
||||||
close(client);
|
close(client);
|
||||||
}
|
}
|
||||||
|
void Server::forward_message(int client, char buffer[BUFFER_LENGTH])
|
||||||
|
{
|
||||||
|
std::cout << "forward message form " << client << std::endl;
|
||||||
|
int n;
|
||||||
|
int error = 0;
|
||||||
|
|
||||||
|
for (int c : clients)
|
||||||
|
{
|
||||||
|
if (c == client)
|
||||||
|
continue;
|
||||||
|
std::cout
|
||||||
|
<< "forward message form "
|
||||||
|
<< client
|
||||||
|
<< "to "
|
||||||
|
<< c
|
||||||
|
<< std::endl;
|
||||||
|
n = write(c, buffer, strlen(buffer));
|
||||||
|
|
||||||
|
if (n < 0)
|
||||||
|
{
|
||||||
|
std::cout << "ERROR Writing socket to " << c << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void Server::shutdown()
|
void Server::shutdown()
|
||||||
{
|
{
|
||||||
std::cout << "Server is shuting down"<< std::endl;
|
std::cout << "Server is shuting down" << std::endl;
|
||||||
|
|
||||||
std::cout << "Closing server socket" << std::endl;
|
std::cout << "Closing server socket" << std::endl;
|
||||||
close(this->server_socket);
|
close(this->server_socket);
|
||||||
server_running = false;
|
server_running = false;
|
||||||
}
|
}
|
||||||
void Server::off(){
|
void Server::off()
|
||||||
this-> online = false;
|
{
|
||||||
|
this->online = false;
|
||||||
}
|
}
|
||||||
bool Server::running(){
|
bool Server::running()
|
||||||
|
{
|
||||||
return this->server_running;
|
return this->server_running;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user