From 3ad3d73a2fe832c8bc9ad11cbf841be833d97438 Mon Sep 17 00:00:00 2001 From: beta Date: Sat, 27 Apr 2019 16:05:49 -0500 Subject: [PATCH 1/5] networking stuff --- .vscode/settings.json | 40 +++++++++- includes/client.hpp | 63 ++++++++++++++++ includes/common.hpp | 23 ++++++ includes/game.hpp | 10 +-- includes/server.hpp | 38 ++++++++++ install.sh | 0 makefile | 19 +++-- source/client.cpp | 159 +++++++++++++++++++++++++++++++++++++++ source/client_driver.cpp | 15 ++++ source/game.cpp | 1 + source/server.cpp | 115 ++++++++++++++++++++++++++++ source/server_driver.cpp | 8 ++ thanks.md | 1 + 13 files changed, 478 insertions(+), 14 deletions(-) create mode 100644 includes/client.hpp create mode 100644 includes/common.hpp create mode 100644 includes/server.hpp create mode 100644 install.sh create mode 100644 source/client.cpp create mode 100644 source/client_driver.cpp create mode 100644 source/server.cpp create mode 100644 source/server_driver.cpp create mode 100644 thanks.md diff --git a/.vscode/settings.json b/.vscode/settings.json index 62e0df7..ad60a18 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,44 @@ "*.cl": "c", "*.tpl": "html", "*.cool": "java", - "cmath": "cpp" + "cmath": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "*.tcc": "cpp", + "chrono": "cpp", + "cstdint": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "fstream": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "memory": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "system_error": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "type_traits": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "cstring": "cpp", + "algorithm": "cpp" } } \ No newline at end of file diff --git a/includes/client.hpp b/includes/client.hpp new file mode 100644 index 0000000..d0d38c9 --- /dev/null +++ b/includes/client.hpp @@ -0,0 +1,63 @@ +#ifndef __BETACORE_CLIENT_HPP__ +#define __BETACORE_CLIENT_HPP__ +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include "common.hpp" + +namespace betacore{ + + +class Client{ + private: + bool _online = false; + bool _running= false; + char key = 'Z'; + int _port; + std::string _address; + MODE _mode; + int _client_socket; + std::function _update; + struct sockaddr_in _server_address; + struct hostent * _server; + + + std::string encode(const SHAPE &shape); + std::string crypt(const std::string &shape); + + + std::string raw(const SHAPE &shape); + SHAPE parse(const std::string &message); + + SHAPE decode(const std::string &message); + /** + * Function for Thread for getting information from server + */ + void listener(); + /** + * Function for Thread for sending to server + */ + void sender(); + void start(); + void stop(); + public: + Client(MODE mode, int port, std::string address, std::function &update); + void kill(); + bool running(); + void send(bool encrypt, SHAPE shape); + + +}; +} +#endif \ No newline at end of file diff --git a/includes/common.hpp b/includes/common.hpp new file mode 100644 index 0000000..de71aa1 --- /dev/null +++ b/includes/common.hpp @@ -0,0 +1,23 @@ +#ifndef __BETACORE_COMMON_HPP__ +#define __BETACORE_COMMON_HPP__ + +#define BUFFER_SIZE 1024 +namespace betacore +{ +enum SHAPE{ + NONE, + TRIANGLE, + CIRCLE, + SQUARE, + PENTAGON, + UNKOWN +}; +enum MODE +{ + ALICE, + BOB, + EVE +}; + +} +#endif \ No newline at end of file diff --git a/includes/game.hpp b/includes/game.hpp index f679910..d0ebaa1 100644 --- a/includes/game.hpp +++ b/includes/game.hpp @@ -17,16 +17,10 @@ #include #include #define PI 3.14159265359 +#include "../includes/common.hpp" namespace betacore { -enum SHAPE{ - NONE, - TRIANGLE, - CIRCLE, - SQUARE, - PENTAGON, - UNKOWN -}; + class Game { private: diff --git a/includes/server.hpp b/includes/server.hpp new file mode 100644 index 0000000..c020745 --- /dev/null +++ b/includes/server.hpp @@ -0,0 +1,38 @@ +#ifndef __BETACORE_SERVER_HPP__ +#define __BETACORE_SERVER_HPP__ +#include + + +#include +#include +#include +#include +#include +#include +namespace betacore +{ + + +class Server +{ +private: + bool online = false; + bool server_running= false; + int port; + int server_socket; + std::vector clients; + char buffer[1024]; + struct sockaddr_in server_address; + std::vector client_socket_collection; + void start(); + void shutdown(); + void listener(); + void read_socket(int client); +public: + Server(int port); + void off(); + bool running(); +}; +} // namespace betacore + +#endif \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..e69de29 diff --git a/makefile b/makefile index c9b577b..9279738 100644 --- a/makefile +++ b/makefile @@ -3,12 +3,21 @@ COMPILER = g++ COMPILER_FLAGS=-w SOURCE_DIR = source/ INCLUDES_DIR =-I includes/ -LINKER_FLAGS =-lSDL2 -lGL -lGLU - -all: game +LINKER_FLAGS =-lSDL2 -lGL -lGLU -lpthread +LIBRARY_FLAGS= -std=c++11 -c -fPIC -shared +SERVER_LIB=$(OUTPUT_DIR)/server.so +CLIENT_LIB=$(OUTPUT_DIR)/client.so +all: game | 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 - + $(COMPILER) $(INCLUDES_DIR) $(SOURCE_DIR)game.cpp $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OUTPUT_DIR)/game.out +client_lib: $(SOURCE_DIR)client.cpp | make_dir + $(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)client.cpp -lpqxx -o $(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 +server_lib: $(SOURCE_DIR)server.cpp | make_dir + $(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)server.cpp -lpqxx -o $(SERVER_LIB) +server_driver: $(SOURCE_DIR)server_driver.cpp | server_lib + $(COMPILER) $(INCLUDES_DIR) $(SERVER_LIB) $(SOURCE_DIR)server_driver.cpp $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OUTPUT_DIR)/server.out make_dir: mkdir -p $(OUTPUT_DIR) diff --git a/source/client.cpp b/source/client.cpp new file mode 100644 index 0000000..82232cd --- /dev/null +++ b/source/client.cpp @@ -0,0 +1,159 @@ +#include "../includes/client.hpp" + +namespace betacore +{ +Client::Client(MODE mode, int port, std::string address, std::function &update) : _mode(mode), + _port(port), + _address(address), + _update(update) +{ + this->start(); +} +void Client::start() +{ + std::cout + << "Start" + << "\nConnecting to: " << _address << ":" <<_port + << std::endl; + //Create a socket point + _client_socket = socket(AF_INET, SOCK_STREAM, 0); + _server = gethostbyname(_address.c_str()); + if (_client_socket < 0) + { + std::cout << "ERROR opening socket" << std::endl; + return; + } + std::cout + << "Socket Open" + << std::endl; + bzero((char *)&_server_address, sizeof(_server_address)); + _server_address.sin_family = AF_INET; + std::cout + << "AF_INT SET" + << std::endl; + bcopy((char *)_server->h_addr, (char *)&_server_address.sin_addr.s_addr, _server->h_length); + _server_address.sin_port = htons(_port); + + // Now connect to the server + if (connect(_client_socket, (struct sockaddr *)&_server_address, sizeof(_server_address)) < 0) + { + std::cout << "ERROR connecting" << std::endl; + return; + } + std::cout + << "Client Connected" + << std::endl; +} +void Client::stop() +{ + std::cout + << "Stop" + << std::endl; +} + +void Client::kill() +{ + std::cout + << "Kill" + << std::endl; + this->_online = false; +} +bool Client::running() +{ + return this->_running; +} + +SHAPE Client::parse(const std::string &message) +{ + std::stringstream stream(message); + std::string tmp; + std::vector words; + while (std::getline(stream, tmp, ':')) + words.push_back(tmp); + + if (words.size() < 2) + return NONE; + tmp = words.at(1); + SHAPE result = decode(tmp); + if (result != UNKOWN) + return result; + if (_mode == EVE) + return UNKOWN; + return decode(crypt(tmp)); +} +SHAPE Client::decode(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; +} +std::string Client::crypt(const std::string &message) +{ + + std::string output = message; + for (int i = 0; i < message.size(); i++) + output[i] = message[i] ^ key; + return output; +} +std::string Client::encode(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; +} +void Client::send(bool encrypt, SHAPE shape) +{ + + std::string message = "SHAPE:" + encrypt ? crypt(encode(shape)) : encode(shape) + ":END"; + std::cout << "Sending Message: " << message << std::endl; + char buffer[BUFFER_SIZE]; + bzero(buffer, BUFFER_SIZE); + strcpy(buffer, message.c_str()); + if (write(_client_socket, buffer, strlen(buffer) < 0)) + { + std::cout << "Failed send to server" << std::endl; + } +} + +void Client::listener() +{ + while (_online) + { + // Now read server response + char buffer[BUFFER_SIZE]; + bzero(buffer, BUFFER_SIZE); + + if (read(_client_socket, buffer, BUFFER_SIZE-1) < 0) + { + std::cout << "ERROR reading from socket" << std::endl; + } + std::string message(buffer); + SHAPE shape = parse(buffer); + _update(shape); + } +} +} // namespace betacore \ No newline at end of file diff --git a/source/client_driver.cpp b/source/client_driver.cpp new file mode 100644 index 0000000..0da5aed --- /dev/null +++ b/source/client_driver.cpp @@ -0,0 +1,15 @@ +#include "client.hpp" +#include + +void test(betacore::SHAPE &s){ + std::cout << "It worked" << std::endl; +} +int main(int argc, char * argv[]){ + std::function fn= test; + betacore::Client client(betacore::ALICE,4444, "localhost",fn); + client.send(false, betacore::TRIANGLE); + client.send(true, betacore::TRIANGLE); + //s.off(); + //while(client.running()){} + return 0; +} diff --git a/source/game.cpp b/source/game.cpp index be442c4..7d890e0 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -668,6 +668,7 @@ void Game::on_key_right_arrow() } void Game::on_key_space() { + } void Game::on_key_enter() { diff --git a/source/server.cpp b/source/server.cpp new file mode 100644 index 0000000..9b440c1 --- /dev/null +++ b/source/server.cpp @@ -0,0 +1,115 @@ +#include "../includes/server.hpp" + +#include + +#include +#include +#include +#include + +namespace betacore +{ +Server::Server(int port) +{ + this->port = port; + std::cout << "And so it begins..." << std::endl; + this->start(); +} +void Server::start() +{ + this->server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (this->server_socket < 0) + { + std::cout << "ERROR on opening socket"<< std::endl; + return; + } + std::cout << "Socket Connected"<< std::endl; + + bzero((char *)&server_address, sizeof(server_address)); + + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = INADDR_ANY; + server_address.sin_port = htons(this->port); + + if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) + { + std::cout << "ERROR on binding "<< std::endl; + + return; + } + std::cout << "Bound"<< std::endl; + online = true; + server_running=true; + std::cout << "Server is online"<< std::endl; + std::thread listen_thread([this] { this->listener(); } ); + listen_thread.detach(); +} +void Server::listener() +{ + std::cout << "Listener "<< online << std::endl; + while (online) { + struct sockaddr_in client_address; + listen(this->server_socket, 5); + socklen_t client_socket_length = sizeof(client_address); + int client_number = + accept(server_socket, (struct sockaddr *)&client_address, &client_socket_length); + this->client_socket_collection.push_back(client_address); + this->clients.push_back(client_number); + std::cout << "New Conenction"<< std::endl; + std::thread socket_thread([this,client_number]{this->read_socket(client_number);}); + socket_thread.detach(); + } + this->shutdown(); +} +void Server::read_socket(int client) +{ + std::cout + << "Ready To Read " + << client + << std::endl; + int n; + while (online) + { + if (client < 0) + { + std::cout << "ERROR on accept"<< std::endl; + } + bzero(buffer, 256); + n = read(client, buffer, 1024); + + if (n < 0) + { + std::cout << "ERROR reading from socket"<< std::endl; + } + + 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; + } + } +} +void Server::shutdown() +{ + std::cout << "Server is shuting down"<< std::endl; + + for (auto socket : clients){ + std::cout + << "Closing new socket :: " + << socket + << std::endl; + close(socket); + } + std::cout << "Closing server socket" << std::endl; + close(this->server_socket); + server_running = false; +} +void Server::off(){ + this-> online = false; +} +bool Server::running(){ + return this->server_running; +} +} // namespace betacore \ No newline at end of file diff --git a/source/server_driver.cpp b/source/server_driver.cpp new file mode 100644 index 0000000..4637f31 --- /dev/null +++ b/source/server_driver.cpp @@ -0,0 +1,8 @@ +#include "server.hpp" + +int main(int argc, char * argv[]){ + betacore::Server s(4444); + //s.off(); + while(s.running()){} + return 0; +} diff --git a/thanks.md b/thanks.md new file mode 100644 index 0000000..2efb3c6 --- /dev/null +++ b/thanks.md @@ -0,0 +1 @@ +https://stackoverflow.com/questions/10673585/start-thread-with-member-function -- 2.43.0 From 3f2d84e8d4706ea047d4b57689f92557f58c724b Mon Sep 17 00:00:00 2001 From: beta Date: Sat, 27 Apr 2019 22:29:33 -0500 Subject: [PATCH 2/5] working tcp comm --- .vscode/c_cpp_properties.json | 1 + .vscode/settings.json | 20 ++++++++- includes/client.hpp | 75 +++++++++++++++++---------------- includes/common.hpp | 2 +- includes/server.hpp | 18 ++++++-- makefile | 6 ++- source/client.cpp | 79 ++++++++++++++++++++++++----------- source/client_driver.cpp | 9 +++- source/game.cpp | 8 +--- source/server.cpp | 49 +++++++++++----------- 10 files changed, 167 insertions(+), 100 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index d3174b0..4401be7 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,6 +6,7 @@ "c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include", "L:/SDL2-2.0.5/include", "L:/SDL2-2.0.5/include/SDL2", + "${workspaceRoot}/includes/", "C:/Program Files (x86)/Windows Kits/10/Include/10.0.10150.0/ucrt", "C:/Program Files (x86)/Windows Kits/8.1/Include/um/gl" ], diff --git a/.vscode/settings.json b/.vscode/settings.json index ad60a18..2e45670 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -41,6 +41,24 @@ "typeinfo": "cpp", "utility": "cpp", "cstring": "cpp", - "algorithm": "cpp" + "algorithm": "cpp", + "ios": "cpp", + "new": "cpp", + "string": "cpp", + "xfacet": "cpp", + "xfunctional": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xlocnum": "cpp", + "xmemory": "cpp", + "xmemory0": "cpp", + "xstddef": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xutility": "cpp", + "xthread": "cpp", + "map": "cpp", + "xtree": "cpp" } } \ No newline at end of file diff --git a/includes/client.hpp b/includes/client.hpp index d0d38c9..b1933b9 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -5,59 +5,60 @@ #include #include #include - +#include +#include #include #include #include #include + #include #include #include -#include "common.hpp" - -namespace betacore{ +#include "common.hpp" -class Client{ - private: - bool _online = false; - bool _running= false; - char key = 'Z'; - int _port; - std::string _address; - MODE _mode; - int _client_socket; - std::function _update; - struct sockaddr_in _server_address; - struct hostent * _server; - +namespace betacore +{ - std::string encode(const SHAPE &shape); - std::string crypt(const std::string &shape); +class Client +{ +private: + bool _online = false; + bool _running = false; + char key = 'Z'; + int _port; + std::string _url; + MODE _mode; + int _client_socket; + std::function _update; + struct sockaddr_in _server_address; + struct hostent *_server; + std::map lookup; + std::string encode(const SHAPE &shape); + std::string crypt(const std::string &shape); + std::string raw(const SHAPE &shape); + SHAPE parse(const std::string &message); - std::string raw(const SHAPE &shape); - SHAPE parse(const std::string &message); - - SHAPE decode(const std::string &message); - /** + SHAPE decode(const std::string &message); + /** * Function for Thread for getting information from server */ - void listener(); - /** + void listener(); + /** * Function for Thread for sending to server */ - void sender(); - void start(); - void stop(); - public: - Client(MODE mode, int port, std::string address, std::function &update); - void kill(); - bool running(); - void send(bool encrypt, SHAPE shape); - - + void sender(); + void start(); + void stop(); + +public: + Client(MODE mode, int port, std::string url, std::function &update); + void kill(); + bool running(); + void send(bool encrypt, SHAPE shape); }; -} +} // namespace betacore #endif \ No newline at end of file diff --git a/includes/common.hpp b/includes/common.hpp index de71aa1..37497f2 100644 --- a/includes/common.hpp +++ b/includes/common.hpp @@ -1,7 +1,7 @@ #ifndef __BETACORE_COMMON_HPP__ #define __BETACORE_COMMON_HPP__ -#define BUFFER_SIZE 1024 + namespace betacore { enum SHAPE{ diff --git a/includes/server.hpp b/includes/server.hpp index c020745..5e359a3 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -1,14 +1,26 @@ #ifndef __BETACORE_SERVER_HPP__ #define __BETACORE_SERVER_HPP__ #include - +#include +#include #include #include +#include #include + +#include + +#include +#include +#include +#include + #include #include #include +#include "common.hpp" + namespace betacore { @@ -16,14 +28,13 @@ namespace betacore class Server { private: + fd_set rset; bool online = false; bool server_running= false; int port; int server_socket; std::vector clients; - char buffer[1024]; struct sockaddr_in server_address; - std::vector client_socket_collection; void start(); void shutdown(); void listener(); @@ -32,6 +43,7 @@ public: Server(int port); void off(); bool running(); + }; } // namespace betacore diff --git a/makefile b/makefile index 9279738..d1b72a1 100644 --- a/makefile +++ b/makefile @@ -1,13 +1,15 @@ OUTPUT_DIR= bin COMPILER = g++ -COMPILER_FLAGS=-w +COMPILER_FLAGS=-w -std=c++11 SOURCE_DIR = source/ INCLUDES_DIR =-I includes/ LINKER_FLAGS =-lSDL2 -lGL -lGLU -lpthread LIBRARY_FLAGS= -std=c++11 -c -fPIC -shared SERVER_LIB=$(OUTPUT_DIR)/server.so CLIENT_LIB=$(OUTPUT_DIR)/client.so -all: game | client_driver | server_driver +all: game | 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 diff --git a/source/client.cpp b/source/client.cpp index 82232cd..bce9f11 100644 --- a/source/client.cpp +++ b/source/client.cpp @@ -2,22 +2,32 @@ namespace betacore { -Client::Client(MODE mode, int port, std::string address, std::function &update) : _mode(mode), - _port(port), - _address(address), - _update(update) +Client::Client(MODE mode, int port, std::string url, std::function &update) : _mode(mode), + _port(port), + _url(url), + _update(update) { this->start(); + lookup["TRIANGLE"]="APPLE"; + lookup["CIRCLE"]="ORANGE"; + lookup["SQUARE"]="BANANA"; + lookup["PENTAGON"]="PINEAPPLE"; + + lookup["APPLE"]="TRIANGLE"; + lookup["ORANGE"]="CIRCLE"; + lookup["BANANA"]="SQUARE"; + lookup["PINEAPPLE"]="PENTAGON"; + } void Client::start() { std::cout << "Start" - << "\nConnecting to: " << _address << ":" <<_port + << "\nConnecting to: " << _url << ":" <<_port << std::endl; //Create a socket point - _client_socket = socket(AF_INET, SOCK_STREAM, 0); - _server = gethostbyname(_address.c_str()); + this->_client_socket = socket(AF_INET, SOCK_STREAM, 0); + _server = gethostbyname(_url.c_str()); if (_client_socket < 0) { std::cout << "ERROR opening socket" << std::endl; @@ -28,21 +38,26 @@ void Client::start() << std::endl; bzero((char *)&_server_address, sizeof(_server_address)); _server_address.sin_family = AF_INET; - std::cout - << "AF_INT SET" - << std::endl; + bcopy((char *)_server->h_addr, (char *)&_server_address.sin_addr.s_addr, _server->h_length); _server_address.sin_port = htons(_port); - + + // Now connect to the server if (connect(_client_socket, (struct sockaddr *)&_server_address, sizeof(_server_address)) < 0) { std::cout << "ERROR connecting" << std::endl; return; } + this->_online = true; + this->_running = true; + std::thread listen_thread([this] { this->listener(); } ); + listen_thread.detach(); + std::cout << "Client Connected" << std::endl; + } void Client::stop() { @@ -97,10 +112,13 @@ SHAPE Client::decode(const std::string &shape) std::string Client::crypt(const std::string &message) { - std::string output = message; - for (int i = 0; i < message.size(); i++) - output[i] = message[i] ^ key; - return output; + + // std::string output = std::string(message); + + // for (int i = 0; i < message.size(); i++) + // output.at(i) = message[i] ^ key; + std::cout << "Crypt::" << message << " : " << lookup[message] << std::endl; + return lookup[message]; } std::string Client::encode(const SHAPE &shape) { @@ -128,30 +146,43 @@ std::string Client::encode(const SHAPE &shape) void Client::send(bool encrypt, SHAPE shape) { - std::string message = "SHAPE:" + encrypt ? crypt(encode(shape)) : encode(shape) + ":END"; - std::cout << "Sending Message: " << message << std::endl; - char buffer[BUFFER_SIZE]; - bzero(buffer, BUFFER_SIZE); - strcpy(buffer, message.c_str()); - if (write(_client_socket, buffer, strlen(buffer) < 0)) + std::string message = "SHAPE:" ; + message += encrypt ? crypt(encode(shape)) : encode(shape) ; + message += ":END"; + std::cout << "Sending Message: " << message << "\n" << std::endl; + char buffer[1024]; + bzero(buffer,1024); + // for(int i=0; i<1024-1 && i < message.length(); i++){ + // buffer[i]= message.at(i); + // } + strcpy(buffer,message.c_str()); + buffer[1024-1]='\0'; + int x =write(this->_client_socket, buffer, strlen(buffer)); + if (x < 0) { std::cout << "Failed send to server" << std::endl; } + } void Client::listener() { + std::cout << "listener" << std::endl; + char buffer[1024]; while (_online) { + // Now read server response - char buffer[BUFFER_SIZE]; - bzero(buffer, BUFFER_SIZE); - if (read(_client_socket, buffer, BUFFER_SIZE-1) < 0) + bzero(buffer, 1024); + std::cout << "Wating For message" << std::endl; + if (read(_client_socket, buffer, 1024-1) < 0) { std::cout << "ERROR reading from socket" << std::endl; } + std::string message(buffer); + std::cout << "Got Message: " << message << std::endl; SHAPE shape = parse(buffer); _update(shape); } diff --git a/source/client_driver.cpp b/source/client_driver.cpp index 0da5aed..a8750fe 100644 --- a/source/client_driver.cpp +++ b/source/client_driver.cpp @@ -2,7 +2,7 @@ #include void test(betacore::SHAPE &s){ - std::cout << "It worked" << std::endl; + std::cout << "It worked\n" << std::endl; } int main(int argc, char * argv[]){ std::function fn= test; @@ -11,5 +11,12 @@ int main(int argc, char * argv[]){ client.send(true, betacore::TRIANGLE); //s.off(); //while(client.running()){} + std::string a; + while(client.running()){ + std::cin >> a; + client.send(false, betacore::TRIANGLE); + if (a == "q") client.kill(); + } + return 0; } diff --git a/source/game.cpp b/source/game.cpp index 7d890e0..c98f54c 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -111,7 +111,7 @@ int Game::initGL() if (error != GL_NO_ERROR) { - printf("OpenGL Error %s\n", gluErrorString(error)); + printf("OpenGL Error %s\n"); result = -1; return result; } @@ -123,11 +123,6 @@ int Game::initGL() glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective( - 45.0f, - (GLfloat)this->SCREEN_WIDTH / (GLfloat)this->SCREEN_HEIGHT, - 0.1f, - 100.0f); glMatrixMode(GL_MODELVIEW); return result; @@ -487,7 +482,6 @@ void Game::resize(int width, int height) glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); } diff --git a/source/server.cpp b/source/server.cpp index 9b440c1..d67ffc1 100644 --- a/source/server.cpp +++ b/source/server.cpp @@ -1,11 +1,5 @@ #include "../includes/server.hpp" -#include - -#include -#include -#include -#include namespace betacore { @@ -41,21 +35,27 @@ void Server::start() online = true; server_running=true; std::cout << "Server is online"<< std::endl; + signal(SIGPIPE,SIG_IGN); std::thread listen_thread([this] { this->listener(); } ); listen_thread.detach(); } void Server::listener() { std::cout << "Listener "<< online << std::endl; + struct sockaddr_in client_address; + listen(this->server_socket, 10); while (online) { - struct sockaddr_in client_address; - listen(this->server_socket, 5); + socklen_t client_socket_length = sizeof(client_address); int client_number = accept(server_socket, (struct sockaddr *)&client_address, &client_socket_length); - this->client_socket_collection.push_back(client_address); this->clients.push_back(client_number); std::cout << "New Conenction"<< std::endl; + if (client_number < 0) + { + std::cout << "ERROR on accept"<< std::endl; + continue; + } std::thread socket_thread([this,client_number]{this->read_socket(client_number);}); socket_thread.detach(); } @@ -68,40 +68,40 @@ void Server::read_socket(int client) << client << std::endl; int n; - while (online) - { - if (client < 0) - { - std::cout << "ERROR on accept"<< std::endl; - } - bzero(buffer, 256); - n = read(client, buffer, 1024); + char buffer[1024]; + while (online && + std::find(clients.begin(), clients.end(), client) != clients.end()) + { + + + bzero(buffer, 1024); + + n = read(client, buffer, 1024-1); if (n < 0) { 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; } } + clients.erase( + std::remove(clients.begin(), clients.end(), client), clients.end()); + close(client); } void Server::shutdown() { std::cout << "Server is shuting down"<< std::endl; - for (auto socket : clients){ - std::cout - << "Closing new socket :: " - << socket - << std::endl; - close(socket); - } std::cout << "Closing server socket" << std::endl; close(this->server_socket); server_running = false; @@ -112,4 +112,5 @@ void Server::off(){ bool Server::running(){ return this->server_running; } + } // namespace betacore \ No newline at end of file -- 2.43.0 From bc0297a9dc45660238bceebcc7db8818ffa8e5d9 Mon Sep 17 00:00:00 2001 From: beta Date: Sun, 28 Apr 2019 13:06:00 -0500 Subject: [PATCH 3/5] update networking --- includes/client.hpp | 10 ++- includes/common.hpp | 72 +++++++++++++++++- includes/game.hpp | 16 +++- includes/server.hpp | 1 + makefile | 4 +- run.sh | 2 +- source/client.cpp | 108 ++++++++++++++------------ source/client_driver.cpp | 27 ++++--- source/game.cpp | 159 ++++++++++++++++++++++++--------------- source/server.cpp | 109 ++++++++++++++++----------- 10 files changed, 334 insertions(+), 174 deletions(-) diff --git a/includes/client.hpp b/includes/client.hpp index b1933b9..4fc5146 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -32,10 +32,11 @@ private: std::string _url; MODE _mode; int _client_socket; - std::function _update; + std::function _update; struct sockaddr_in _server_address; struct hostent *_server; std::map lookup; + std::vector explode(std::string &message); std::string encode(const SHAPE &shape); std::string crypt(const std::string &shape); @@ -55,7 +56,12 @@ private: void stop(); public: - Client(MODE mode, int port, std::string url, std::function &update); + Client( + MODE mode, + int port, + std::string url, + std::function &update); + ~Client(); void kill(); bool running(); void send(bool encrypt, SHAPE shape); diff --git a/includes/common.hpp b/includes/common.hpp index 37497f2..6940944 100644 --- a/includes/common.hpp +++ b/includes/common.hpp @@ -1,10 +1,14 @@ #ifndef __BETACORE_COMMON_HPP__ #define __BETACORE_COMMON_HPP__ +#define BUFFER_LENGTH 1024 +#include namespace betacore { -enum SHAPE{ + +enum SHAPE +{ NONE, TRIANGLE, CIRCLE, @@ -16,8 +20,70 @@ enum MODE { ALICE, 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 \ No newline at end of file diff --git a/includes/game.hpp b/includes/game.hpp index d0ebaa1..f9ad597 100644 --- a/includes/game.hpp +++ b/includes/game.hpp @@ -17,7 +17,9 @@ #include #include #define PI 3.14159265359 -#include "../includes/common.hpp" +#include "common.hpp" +#include "client.hpp" + namespace betacore { @@ -31,11 +33,12 @@ private: const int SCREEN_HEIGHT = 600; int MONITOR_WIDTH=1920; int MONITOR_HEIGHT=1080; - const char *TITLE = "Lesson 01"; + const char *TITLE ; bool KEEP_ALIVE = true; bool SHOW_GRID = false; SHAPE USER_CURRENT = TRIANGLE; SHAPE GUST_CURRENT = UNKOWN; + MODE ME; bool KEY_UP_ARROW_ACTIVE = false; bool KEY_DOWN_ARROW_ACTIVE = false; bool KEY_LEFT_ARROW_ACTIVE = false; @@ -43,8 +46,11 @@ private: bool KEY_SPACE_ACTIVE = false; bool KEY_RETURN_ACTIVE = false; bool FULL_SCREEN = false; + Client* client = nullptr; public: - Game(); + Game(MODE mode, + int port, + std::string url); ~Game(); int init(); int initGL(); @@ -67,7 +73,7 @@ public: void rectangle(); void cross(bool filled); void compass(); - + void on_key_up_arrow(); void on_key_down_arrow(); void on_key_left_arrow(); @@ -78,6 +84,8 @@ public: void top_screen(); void user_screen(); void guest_screen(); + + void update(SHAPE &s,MODE &m); }; } // namespace betacore #endif \ No newline at end of file diff --git a/includes/server.hpp b/includes/server.hpp index 5e359a3..1ba6c7d 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -39,6 +39,7 @@ private: void shutdown(); void listener(); void read_socket(int client); + void forward_message(int client,char buffer[BUFFER_LENGTH] ); public: Server(int port); void off(); diff --git a/makefile b/makefile index d1b72a1..aed9b15 100644 --- a/makefile +++ b/makefile @@ -11,12 +11,12 @@ all: game | 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 $(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)client.cpp -lpqxx -o $(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 +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 $(COMPILER) $(LIBRARY_FLAGS) $(INCLUDES_DIR) $(SOURCE_DIR)server.cpp -lpqxx -o $(SERVER_LIB) server_driver: $(SOURCE_DIR)server_driver.cpp | server_lib diff --git a/run.sh b/run.sh index d139251..3c32e76 100644 --- a/run.sh +++ b/run.sh @@ -1,2 +1,2 @@ export SDL_VIDEO_X11_VISUALID= -./bin/game \ No newline at end of file +./bin/game.out \ No newline at end of file diff --git a/source/client.cpp b/source/client.cpp index bce9f11..a1174cf 100644 --- a/source/client.cpp +++ b/source/client.cpp @@ -2,32 +2,40 @@ namespace betacore { -Client::Client(MODE mode, int port, std::string url, std::function &update) : _mode(mode), - _port(port), - _url(url), - _update(update) +Client::Client( + MODE mode, + int port, + std::string url, + std::function &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(); - lookup["TRIANGLE"]="APPLE"; - lookup["CIRCLE"]="ORANGE"; - lookup["SQUARE"]="BANANA"; - lookup["PENTAGON"]="PINEAPPLE"; - - lookup["APPLE"]="TRIANGLE"; - lookup["ORANGE"]="CIRCLE"; - lookup["BANANA"]="SQUARE"; - lookup["PINEAPPLE"]="PENTAGON"; - +} +Client::~Client(){ + this->_running= false; + this->_online=false; } void Client::start() { std::cout << "Start" - << "\nConnecting to: " << _url << ":" <<_port + << "\nConnecting to: " << _url << ":" << _port << std::endl; //Create a socket point this->_client_socket = socket(AF_INET, SOCK_STREAM, 0); - _server = gethostbyname(_url.c_str()); + _server = gethostbyname(_url.c_str()); if (_client_socket < 0) { std::cout << "ERROR opening socket" << std::endl; @@ -39,25 +47,29 @@ void Client::start() bzero((char *)&_server_address, sizeof(_server_address)); _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); - // 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; return; } this->_online = true; this->_running = true; - std::thread listen_thread([this] { this->listener(); } ); + std::thread listen_thread([this] { this->listener(); }); listen_thread.detach(); std::cout << "Client Connected" << std::endl; - } void Client::stop() { @@ -78,17 +90,18 @@ bool Client::running() return this->_running; } -SHAPE Client::parse(const std::string &message) -{ +std::vector Client::explode(std::string &message){ std::stringstream stream(message); std::string tmp; std::vector words; while (std::getline(stream, tmp, ':')) words.push_back(tmp); + return words; +} - if (words.size() < 2) - return NONE; - tmp = words.at(1); +SHAPE Client::parse(const std::string &message) +{ + std::string tmp = message; SHAPE result = decode(tmp); if (result != UNKOWN) return result; @@ -112,12 +125,11 @@ SHAPE Client::decode(const std::string &shape) std::string Client::crypt(const std::string &message) { - // std::string output = std::string(message); // for (int i = 0; i < message.size(); i++) // 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]; } 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) { - std::string message = "SHAPE:" ; - message += encrypt ? crypt(encode(shape)) : encode(shape) ; - message += ":END"; - std::cout << "Sending Message: " << message << "\n" << std::endl; - char buffer[1024]; - bzero(buffer,1024); - // for(int i=0; i<1024-1 && i < message.length(); i++){ - // buffer[i]= message.at(i); - // } - strcpy(buffer,message.c_str()); - buffer[1024-1]='\0'; - int x =write(this->_client_socket, buffer, strlen(buffer)); + std::string message = "SHAPE:"; + message += encrypt ? crypt(encode(shape)) : encode(shape); + message += ":"; + message += Parser::mode(this->_mode); + std::cout << "Sending Message: " << message << "\n" + << std::endl; + char buffer[BUFFER_LENGTH]; + bzero(buffer, BUFFER_LENGTH); + strcpy(buffer, message.c_str()); + buffer[BUFFER_LENGTH - 1] = '\0'; + int x = write(this->_client_socket, buffer, strlen(buffer)); if (x < 0) { std::cout << "Failed send to server" << std::endl; } - } void Client::listener() { std::cout << "listener" << std::endl; - char buffer[1024]; + char buffer[BUFFER_LENGTH]; while (_online) { - + // Now read server response - bzero(buffer, 1024); + bzero(buffer, BUFFER_LENGTH); 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::string message(buffer); std::cout << "Got Message: " << message << std::endl; - SHAPE shape = parse(buffer); - _update(shape); + auto parts = explode(message); + if(parts.size() < 3) + return; + SHAPE shape = parse(parts.at(1)); + MODE mode = Parser::mode(parts.at(2)); + _update(shape,mode); } } } // namespace betacore \ No newline at end of file diff --git a/source/client_driver.cpp b/source/client_driver.cpp index a8750fe..e53841c 100644 --- a/source/client_driver.cpp +++ b/source/client_driver.cpp @@ -1,22 +1,31 @@ #include "client.hpp" #include -void test(betacore::SHAPE &s){ - std::cout << "It worked\n" << std::endl; +void test(betacore::SHAPE &s, betacore::MODE &m) +{ + std::cout + << "Got Shape::" + << betacore::Parser::shape(s) + << "\tFrom" + << betacore::Parser::mode(m) + << std::endl; } -int main(int argc, char * argv[]){ - std::function fn= test; - betacore::Client client(betacore::ALICE,4444, "localhost",fn); +int main(int argc, char *argv[]) +{ + std::function fn = test; + betacore::Client client(betacore::ALICE, 4444, "localhost", fn); client.send(false, betacore::TRIANGLE); client.send(true, betacore::TRIANGLE); //s.off(); //while(client.running()){} std::string a; - while(client.running()){ + while (client.running()) + { std::cin >> a; client.send(false, betacore::TRIANGLE); - if (a == "q") client.kill(); - } + if (a == "q") + client.kill(); + } return 0; -} +} diff --git a/source/game.cpp b/source/game.cpp index c98f54c..692420d 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,20 +1,38 @@ #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 { -Game::Game() +Game::Game(MODE mode, + int port, + std::string url): ME(mode) { - - printf("%d\t%d\n", this->MONITOR_WIDTH,this->MONITOR_HEIGHT); + this->TITLE = Parser::mode(mode).c_str(); + + printf("%s\t%d\t%d\n",this->TITLE, this->MONITOR_WIDTH, this->MONITOR_HEIGHT); if (init()) { printf("Error loading init\n"); return; } + using namespace std::placeholders; + std::function fn = + std::bind(&Game::update, this, _1, _2); + this->client = new Client(mode, port, url, fn); // SDL_DisplayMode DM; // SDL_GetCurrentDisplayMode(0, &DM); - this->MONITOR_HEIGHT=1080; - this->MONITOR_WIDTH=1920; + this->MONITOR_HEIGHT = 1080; + this->MONITOR_WIDTH = 1920; SDL_Event event; while (this->KEEP_ALIVE) @@ -25,8 +43,11 @@ Game::Game() } close(); } -Game::~Game() { - +Game::~Game() +{ + if (this->client != nullptr) + delete this->client; + this->client = nullptr; } int Game::init() @@ -122,7 +143,11 @@ int Game::initGL() glViewport(0, 0, this->SCREEN_WIDTH, this->SCREEN_HEIGHT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - + gluPerspective( + 45.0f, + (GLfloat)this->SCREEN_WIDTH / (GLfloat)this->SCREEN_HEIGHT, + 0.1f, + 100.0f); glMatrixMode(GL_MODELVIEW); return result; @@ -240,8 +265,9 @@ void Game::rectangle() glVertex3f(-2.5f, -1.0f, 0.0f); // Bottom Left glEnd(); } -void Game::cross(bool filled){ - double width = 0.15; +void Game::cross(bool filled) +{ + double width = 0.15; if (filled) glBegin(GL_QUADS); else @@ -273,43 +299,43 @@ void Game::compass() //MINI TRIANGLE glPushMatrix(); - glScalef(0.12f, 0.12f, 0.0f); - glTranslatef(0.0f, 2.6f, 0.0f); - this->triangle(this->KEY_UP_ARROW_ACTIVE); + glScalef(0.12f, 0.12f, 0.0f); + glTranslatef(0.0f, 2.6f, 0.0f); + this->triangle(this->KEY_UP_ARROW_ACTIVE); glPopMatrix(); // MINI SQUARE glPushMatrix(); - glScalef(0.12f, 0.12f, 0.0f); - glTranslatef(0.0f, -2.6f, 0.0f); - this->square(this->KEY_DOWN_ARROW_ACTIVE); + glScalef(0.12f, 0.12f, 0.0f); + glTranslatef(0.0f, -2.6f, 0.0f); + this->square(this->KEY_DOWN_ARROW_ACTIVE); glPopMatrix(); // MINI PENTAGON glPushMatrix(); - glScalef(0.12f, 0.12f, 0.0f); - glTranslatef(-2.56f, 0.0f, 0.0f); - this->pentagon(this->KEY_LEFT_ARROW_ACTIVE); + glScalef(0.12f, 0.12f, 0.0f); + glTranslatef(-2.56f, 0.0f, 0.0f); + this->pentagon(this->KEY_LEFT_ARROW_ACTIVE); glPopMatrix(); // MINI CIRCLE glPushMatrix(); - glScalef(0.12f, 0.12f, 0.0f); - glTranslatef(2.56f, 0.0f, 0.0f); - glColor3f(0.0f, 1.0f, 0.0f); - this->circle(this->KEY_RIGHT_ARROW_ACTIVE); + glScalef(0.12f, 0.12f, 0.0f); + glTranslatef(2.56f, 0.0f, 0.0f); + glColor3f(0.0f, 1.0f, 0.0f); + this->circle(this->KEY_RIGHT_ARROW_ACTIVE); glPopMatrix(); glPushMatrix(); - glScalef(0.35f, 0.35f, 0.0f); - glTranslatef(7.0f, 0.0f, 0.0f); - glColor3f(0.0f, 1.0f, 1.0f); - this->circle(this->KEY_SPACE_ACTIVE); + glScalef(0.35f, 0.35f, 0.0f); + glTranslatef(7.0f, 0.0f, 0.0f); + glColor3f(0.0f, 1.0f, 1.0f); + this->circle(this->KEY_SPACE_ACTIVE); glPopMatrix(); - glPushMatrix(); - glScalef(0.35f, 0.35f, 0.0f); - glTranslatef(10.0f, 0.0f, 0.0f); - glColor3f(1.0f, 1.0f, 0.0f); - this->circle(this->KEY_RETURN_ACTIVE); + glPushMatrix(); + glScalef(0.35f, 0.35f, 0.0f); + glTranslatef(10.0f, 0.0f, 0.0f); + glColor3f(1.0f, 1.0f, 0.0f); + this->circle(this->KEY_RETURN_ACTIVE); glPopMatrix(); glPopMatrix(); @@ -320,27 +346,25 @@ void Game::circle() } void Game::circle(bool filled) { - //glBegin(GL_LINE_LOOP); - // for (int i = 0; i <= 300; i++) - // { - // double angle = 2 * PI * i / 300; - // double x = cos(angle); - // double y = sin(angle); - // glVertex2d(x, y); - // } - // glEnd(); + //glBegin(GL_LINE_LOOP); + // for (int i = 0; i <= 300; i++) + // { + // double angle = 2 * PI * i / 300; + // double x = cos(angle); + // double y = sin(angle); + // glVertex2d(x, y); + // } + // glEnd(); double radius = 1; if (filled) - - glBegin(GL_POLYGON); - + glBegin(GL_POLYGON); - else + else glBegin(GL_LINE_LOOP); - + double angle1 = 0.0; glVertex2d(radius * cos(0.0), radius * sin(0.0)); static const int circle_points = 100; @@ -363,8 +387,8 @@ void Game::pentagon(bool filled) glColor3f(1.0f, 0.0f, 1.0f); float angleIncrement = 360.0f / 5.0f; angleIncrement *= PI / 180.0f; - if ( filled) - glBegin(GL_TRIANGLE_FAN); + if (filled) + glBegin(GL_TRIANGLE_FAN); else glBegin(GL_LINE_LOOP); float angle = 0.0f; @@ -438,17 +462,16 @@ void Game::guest_screen() break; case UNKOWN: float gray = 105.0 / 255.0; - glColor3f(gray,gray,gray); + glColor3f(gray, gray, gray); glPushMatrix(); glRotatef(-45.0f, 0.0f, 0.0f, 1.0f); glScalef(2.0f, 2.0f, 0.0f); - + this->cross(true); glPopMatrix(); break; } - glPopMatrix(); } void Game::top_screen() @@ -481,7 +504,7 @@ void Game::resize(int width, int height) glMatrixMode(GL_PROJECTION); glLoadIdentity(); - + gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); } @@ -527,31 +550,35 @@ void Game::key_down(SDL_Keycode key_code) break; case SDLK_SPACE: this->KEY_SPACE_ACTIVE = true; + this->on_key_space(); break; case SDLK_RETURN: this->KEY_RETURN_ACTIVE = true; + this->on_key_enter(); break; case SDLK_F11: - - if(FULL_SCREEN){ + + if (FULL_SCREEN) + { SDL_SetWindowPosition(this->WINDOW, 100, 100); SDL_RestoreWindow(this->WINDOW); //SDL_SetWindowResizable(this->WINDOW, SDL_TRUE); //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 ); } - else { + else + { SDL_SetWindowPosition(this->WINDOW, 0, 0); //SDL_SetWindowResizable(this->WINDOW, SDL_FALSE); //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 ); } this->FULL_SCREEN = !this->FULL_SCREEN; - SDL_ShowCursor(this->FULL_SCREEN); - + SDL_ShowCursor(this->FULL_SCREEN); + break; default: //Do nothing @@ -662,15 +689,23 @@ void Game::on_key_right_arrow() } void Game::on_key_space() { - + this->client->send(false,this->USER_CURRENT); } 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 int main(int argc, char *argv[]) { - betacore::Game game; + betacore::Game game(betacore::BOB, 4444, "localhost"); return 0; } \ No newline at end of file diff --git a/source/server.cpp b/source/server.cpp index d67ffc1..0dc27e9 100644 --- a/source/server.cpp +++ b/source/server.cpp @@ -1,6 +1,5 @@ #include "../includes/server.hpp" - namespace betacore { Server::Server(int port) @@ -14,10 +13,10 @@ void Server::start() this->server_socket = socket(AF_INET, SOCK_STREAM, 0); if (this->server_socket < 0) { - std::cout << "ERROR on opening socket"<< std::endl; + std::cout << "ERROR on opening socket" << std::endl; return; } - std::cout << "Socket Connected"<< std::endl; + std::cout << "Socket Connected" << std::endl; bzero((char *)&server_address, sizeof(server_address)); @@ -25,91 +24,113 @@ void Server::start() server_address.sin_addr.s_addr = INADDR_ANY; 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; } - std::cout << "Bound"<< std::endl; + std::cout << "Bound" << std::endl; online = true; - server_running=true; - std::cout << "Server is online"<< std::endl; - signal(SIGPIPE,SIG_IGN); - std::thread listen_thread([this] { this->listener(); } ); + server_running = true; + std::cout << "Server is online" << std::endl; + signal(SIGPIPE, SIG_IGN); // Prevents issue with sig kill on client + std::thread listen_thread([this] { this->listener(); }); listen_thread.detach(); } void Server::listener() { - std::cout << "Listener "<< online << std::endl; + std::cout << "Listener " << online << std::endl; struct sockaddr_in client_address; listen(this->server_socket, 10); - while (online) { - + while (online) + { + socklen_t client_socket_length = sizeof(client_address); 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); - std::cout << "New Conenction"<< std::endl; + std::cout << "New Conenction" << std::endl; if (client_number < 0) { - std::cout << "ERROR on accept"<< std::endl; + std::cout << "ERROR on accept" << std::endl; 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(); } this->shutdown(); } void Server::read_socket(int client) { - std::cout - << "Ready To Read " - << client - << std::endl; + std::cout + << "Ready To Read " + << client + << std::endl; int n; - char buffer[1024]; + char buffer[BUFFER_LENGTH]; while (online && - std::find(clients.begin(), clients.end(), client) != clients.end()) - { - - - bzero(buffer, 1024); - - n = read(client, buffer, 1024-1); + std::find(clients.begin(), clients.end(), client) != clients.end()) + { + + bzero(buffer, BUFFER_LENGTH); + + n = read(client, buffer, BUFFER_LENGTH - 1); if (n < 0) { - std::cout << "ERROR reading from socket"<< std::endl; + std::cout << "ERROR reading from socket" << std::endl; break; } + this->forward_message(client, buffer); + } + std::cout << "Closing Connection for " << client << std::endl; + clients.erase( + std::remove(clients.begin(), clients.end(), client), clients.end()); + 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; - - printf("Here is the message: %s\n", buffer); - n = write(client, "I got your mesage\n", 18); + 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"<< std::endl; - break; + std::cout << "ERROR Writing socket to " << c << std::endl; } } - clients.erase( - std::remove(clients.begin(), clients.end(), client), clients.end()); - close(client); + } + 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; - close(this->server_socket); + close(this->server_socket); server_running = false; } -void Server::off(){ - this-> online = false; +void Server::off() +{ + this->online = false; } -bool Server::running(){ +bool Server::running() +{ return this->server_running; } -- 2.43.0 From 7b96cd990c1e46ffe92fedef1ed63ec383e84f0b Mon Sep 17 00:00:00 2001 From: beta Date: Sun, 28 Apr 2019 18:06:05 -0500 Subject: [PATCH 4/5] working --- includes/common.hpp | 2 ++ includes/game.hpp | 4 ++-- run.sh | 2 +- source/client.cpp | 5 ++--- source/game.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/includes/common.hpp b/includes/common.hpp index 6940944..71ce1fa 100644 --- a/includes/common.hpp +++ b/includes/common.hpp @@ -34,6 +34,8 @@ class Parser return "ALICE"; case BOB: return "BOB"; + case EVE: + return "EVE"; default: return "END"; } diff --git a/includes/game.hpp b/includes/game.hpp index f9ad597..ced64fb 100644 --- a/includes/game.hpp +++ b/includes/game.hpp @@ -36,8 +36,8 @@ private: const char *TITLE ; bool KEEP_ALIVE = true; bool SHOW_GRID = false; - SHAPE USER_CURRENT = TRIANGLE; - SHAPE GUST_CURRENT = UNKOWN; + SHAPE USER_CURRENT = NONE; + SHAPE GUST_CURRENT = NONE; MODE ME; bool KEY_UP_ARROW_ACTIVE = false; bool KEY_DOWN_ARROW_ACTIVE = false; diff --git a/run.sh b/run.sh index 3c32e76..8dbdb74 100644 --- a/run.sh +++ b/run.sh @@ -1,2 +1,2 @@ export SDL_VIDEO_X11_VISUALID= -./bin/game.out \ No newline at end of file +./bin/game.out 1 4444 localhost \ No newline at end of file diff --git a/source/client.cpp b/source/client.cpp index a1174cf..bcbdf13 100644 --- a/source/client.cpp +++ b/source/client.cpp @@ -101,13 +101,12 @@ std::vector Client::explode(std::string &message){ SHAPE Client::parse(const std::string &message) { - std::string tmp = message; - SHAPE result = decode(tmp); + SHAPE result = decode(message); if (result != UNKOWN) return result; if (_mode == EVE) return UNKOWN; - return decode(crypt(tmp)); + return decode(crypt(message)); } SHAPE Client::decode(const std::string &shape) { diff --git a/source/game.cpp b/source/game.cpp index 692420d..f2cd3cd 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -17,9 +17,11 @@ Game::Game(MODE mode, int port, std::string url): ME(mode) { + this->TITLE = Parser::mode(mode).c_str(); - printf("%s\t%d\t%d\n",this->TITLE, this->MONITOR_WIDTH, this->MONITOR_HEIGHT); + printf("%s\t%s\t%d\t%d\n",this->TITLE, Parser::mode(mode).c_str(), + this->MONITOR_WIDTH, this->MONITOR_HEIGHT); if (init()) { printf("Error loading init\n"); @@ -427,6 +429,16 @@ void Game::user_screen() this->pentagon(); break; + case UNKOWN: + float gray = 105.0 / 255.0; + glColor3f(gray, gray, gray); + glPushMatrix(); + glRotatef(-45.0f, 0.0f, 0.0f, 1.0f); + glScalef(2.0f, 2.0f, 0.0f); + + this->cross(true); + glPopMatrix(); + break; } // glTranslatef(3.0f, 0.0f, 0.0f); // Move Right 3 Units @@ -697,15 +709,46 @@ void Game::on_key_enter() } void Game::update(SHAPE &s, MODE &m) { - if (m == ME) + if (m == ME || (ME==EVE && m == ALICE)) this->USER_CURRENT = s; else this->GUST_CURRENT = s; } } // namespace betacore + +void ussage(){ + std::cout + << "Ussage:\n" + << "\tMODE: 1-3\n" + << "\t\t1)Alice\n" + << "\t\t2)Bob\n" + << "\t\t2)Eve\n" + << "\tport\n" + << "\taddress" + << std::endl; +} int main(int argc, char *argv[]) { - betacore::Game game(betacore::BOB, 4444, "localhost"); + + if(argc < 4){ + ussage(); + return 1; + } + betacore::MODE mode = betacore::EVE; + int a = atoi(argv[1]); + std::cout << "Running mode" << a << std::endl; + switch( a){ + case 1: + mode = betacore::ALICE; + break; + case 2: + mode = betacore::BOB; + break; + default: + mode = betacore::EVE; + break; + } + betacore::Game game(mode, atoi(argv[2]),argv[3]); return 0; } \ No newline at end of file -- 2.43.0 From d4d23cbfe5a601031266580b00cc86f016f9b540 Mon Sep 17 00:00:00 2001 From: beta Date: Sun, 28 Apr 2019 18:52:55 -0500 Subject: [PATCH 5/5] update install --- install.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/install.sh b/install.sh index e69de29..03e3458 100644 --- a/install.sh +++ b/install.sh @@ -0,0 +1,2 @@ +sudo apt-get install libsdl2-dev +sudo apt-get install freeglut3-dev \ No newline at end of file -- 2.43.0