working tcp comm
This commit is contained in:
parent
3ad3d73a2f
commit
3f2d84e8d4
1
.vscode/c_cpp_properties.json
vendored
1
.vscode/c_cpp_properties.json
vendored
@ -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"
|
||||
],
|
||||
|
20
.vscode/settings.json
vendored
20
.vscode/settings.json
vendored
@ -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"
|
||||
}
|
||||
}
|
@ -5,59 +5,60 @@
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include <thread>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
namespace betacore{
|
||||
namespace betacore
|
||||
{
|
||||
|
||||
class Client
|
||||
{
|
||||
private:
|
||||
bool _online = false;
|
||||
bool _running = false;
|
||||
char key = 'Z';
|
||||
int _port;
|
||||
std::string _url;
|
||||
MODE _mode;
|
||||
int _client_socket;
|
||||
std::function<void(SHAPE &S)> _update;
|
||||
struct sockaddr_in _server_address;
|
||||
struct hostent *_server;
|
||||
std::map<std::string, std::string> lookup;
|
||||
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 _address;
|
||||
MODE _mode;
|
||||
int _client_socket;
|
||||
std::function<void(SHAPE &S)> _update;
|
||||
struct sockaddr_in _server_address;
|
||||
struct hostent * _server;
|
||||
|
||||
std::string raw(const SHAPE &shape);
|
||||
SHAPE parse(const std::string &message);
|
||||
|
||||
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);
|
||||
/**
|
||||
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<void(SHAPE &S)> &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<void(SHAPE &S)> &update);
|
||||
void kill();
|
||||
bool running();
|
||||
void send(bool encrypt, SHAPE shape);
|
||||
};
|
||||
}
|
||||
} // namespace betacore
|
||||
#endif
|
@ -1,7 +1,7 @@
|
||||
#ifndef __BETACORE_COMMON_HPP__
|
||||
#define __BETACORE_COMMON_HPP__
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
namespace betacore
|
||||
{
|
||||
enum SHAPE{
|
||||
|
@ -1,14 +1,26 @@
|
||||
#ifndef __BETACORE_SERVER_HPP__
|
||||
#define __BETACORE_SERVER_HPP__
|
||||
#include <vector>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/poll.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <functional>
|
||||
#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<int> clients;
|
||||
char buffer[1024];
|
||||
struct sockaddr_in server_address;
|
||||
std::vector<sockaddr_in> client_socket_collection;
|
||||
void start();
|
||||
void shutdown();
|
||||
void listener();
|
||||
@ -32,6 +43,7 @@ public:
|
||||
Server(int port);
|
||||
void off();
|
||||
bool running();
|
||||
|
||||
};
|
||||
} // namespace betacore
|
||||
|
||||
|
6
makefile
6
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
|
||||
|
@ -2,22 +2,32 @@
|
||||
|
||||
namespace betacore
|
||||
{
|
||||
Client::Client(MODE mode, int port, std::string address, std::function<void(SHAPE &S)> &update) : _mode(mode),
|
||||
_port(port),
|
||||
_address(address),
|
||||
_update(update)
|
||||
Client::Client(MODE mode, int port, std::string url, std::function<void(SHAPE &S)> &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);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include <iostream>
|
||||
|
||||
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<void(betacore::SHAPE &S)> 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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,5 @@
|
||||
#include "../includes/server.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user