update networking

This commit is contained in:
2019-04-28 13:06:00 -05:00
parent 3f2d84e8d4
commit bc0297a9dc
10 changed files with 337 additions and 177 deletions
+8 -2
View File
@@ -32,10 +32,11 @@ private:
std::string _url;
MODE _mode;
int _client_socket;
std::function<void(SHAPE &S)> _update;
std::function<void(SHAPE &S, MODE &M)> _update;
struct sockaddr_in _server_address;
struct hostent *_server;
std::map<std::string, std::string> lookup;
std::vector<std::string> 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<void(SHAPE &S)> &update);
Client(
MODE mode,
int port,
std::string url,
std::function<void(SHAPE &S, MODE &M)> &update);
~Client();
void kill();
bool running();
void send(bool encrypt, SHAPE shape);
+69 -3
View File
@@ -1,10 +1,14 @@
#ifndef __BETACORE_COMMON_HPP__
#define __BETACORE_COMMON_HPP__
#define BUFFER_LENGTH 1024
#include <string>
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
+12 -4
View File
@@ -17,7 +17,9 @@
#include <string>
#include <math.h>
#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
+1
View File
@@ -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();