networking stuff

This commit is contained in:
2019-04-27 16:05:49 -05:00
parent ac294f64c3
commit 3ad3d73a2f
13 changed files with 478 additions and 14 deletions
+63
View File
@@ -0,0 +1,63 @@
#ifndef __BETACORE_CLIENT_HPP__
#define __BETACORE_CLIENT_HPP__
#include <string>
#include <iostream>
#include <functional>
#include <sstream>
#include <vector>
#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{
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 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<void(SHAPE &S)> &update);
void kill();
bool running();
void send(bool encrypt, SHAPE shape);
};
}
#endif
+23
View File
@@ -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
+2 -8
View File
@@ -17,16 +17,10 @@
#include <string>
#include <math.h>
#define PI 3.14159265359
#include "../includes/common.hpp"
namespace betacore
{
enum SHAPE{
NONE,
TRIANGLE,
CIRCLE,
SQUARE,
PENTAGON,
UNKOWN
};
class Game
{
private:
+38
View File
@@ -0,0 +1,38 @@
#ifndef __BETACORE_SERVER_HPP__
#define __BETACORE_SERVER_HPP__
#include <vector>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <thread>
#include <utility>
#include <functional>
namespace betacore
{
class Server
{
private:
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();
void read_socket(int client);
public:
Server(int port);
void off();
bool running();
};
} // namespace betacore
#endif