Cleaned up code in Client.h and Client.cpp.

Moved ping code into Ping() function.
This commit is contained in:
Jocke
2015-12-08 17:18:03 +01:00
parent d1880754f5
commit 24dada149f
2 changed files with 20 additions and 117 deletions
+1 -9
View File
@@ -4,8 +4,6 @@
#include <string>
#include <ctime>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <glm/common.hpp>
#include <GLFW/glfw3.h> // For input event
@@ -26,14 +24,13 @@ public:
private:
// Threaded
void DisplayLoop();
void ReadFromServer();
void InputLoop();
int Receive(char* data, size_t length);
int CreateMessage(MessageType type, std::string message, char* data);
void Connect();
void Disconnect();
void Ping();
void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
void ParseMessageType(char* data, size_t length);
void ParseEventMessage(char* data, size_t length);
@@ -41,9 +38,6 @@ private:
void ParsePing();
void ParseServerPing();
void ParseSnapshot(char* data, size_t length);
//void SendInput();
void SendDebugInput();
void DrawBoard();
// udp stuff
boost::asio::ip::udp::endpoint m_ReceiverEndpoint;
@@ -51,12 +45,10 @@ private:
boost::asio::ip::udp::socket m_Socket;
int m_PlayerID = -1;
char m_GameBoard[BOARDSIZE][BOARDSIZE];
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
std::string m_PlayerNames[MAXCONNECTIONS];
std::clock_t m_StartPingTime;
double m_DurationOfPingTime;
bool m_ShouldDrawGameBoard = true;
std::string m_PlayerName;
bool m_ThreadIsRunning = true;
+19 -108
View File
@@ -5,12 +5,8 @@ using namespace boost::asio::ip;
Client::Client() : m_Socket(m_IOService)
{
//m_EventBroker = eventBroker;
// Set up network stream
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13);
//EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Client::OnKeyDown);
}
Client::~Client()
@@ -31,29 +27,10 @@ void Client::Start(EventBroker* eventBroker)
std::cout << "Please enter you name(No longer than 7 characters): ";
std::cin >> m_PlayerName;
}
boost::thread_group threads;
for (size_t i = 0; i < BOARDSIZE; i++) {
for (size_t j = 0; j < BOARDSIZE; j++) {
m_GameBoard[j][i] = ' ';
}
}
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
m_PlayerPositions[i].x = -1;
m_PlayerPositions[i].y = -1;
m_PlayerNames[i] = "X";
}
m_Socket.connect(m_ReceiverEndpoint);
std::cout << "I am client. BIP BOP\n";
threads.create_thread(boost::bind(&Client::DisplayLoop, this));
threads.create_thread(boost::bind(&Client::ReadFromServer, this));
//threads.create_thread(boost::bind(&Client::InputLoop, this));
threads.join_all();
ReadFromServer();
}
void Client::Close()
@@ -63,47 +40,6 @@ void Client::Close()
m_Socket.close();
}
void Client::InputLoop()
{
int intervallMs = 33; // ~30 times per second
int commandInterval = 200; // for commands like ping and connect, name might be ambigiuos
std::clock_t previousInputTime = std::clock();
std::clock_t previousCommandTime = std::clock();
while (m_ThreadIsRunning) {
std::clock_t currentTime = std::clock();
int testTimeShit = (1000 * (currentTime - previousCommandTime) / (double)CLOCKS_PER_SEC);
if (commandInterval < (1000 * (currentTime - previousCommandTime) / (double)CLOCKS_PER_SEC)) {
SendDebugInput();
previousCommandTime = currentTime;
}
int testTimeShit2 = (1000 * (currentTime - previousInputTime) / (double)CLOCKS_PER_SEC);
if (intervallMs < (1000 * (currentTime - previousInputTime) / (double)CLOCKS_PER_SEC)) {
if (m_PlayerID != -1) {
//SendInput();
}
previousInputTime = currentTime;
}
}
}
void Client::DisplayLoop()
{
while (m_ThreadIsRunning) {
// Update gameboard
for (size_t i = 0; i < BOARDSIZE; i++) {
for (size_t j = 0; j < BOARDSIZE; j++) {
m_GameBoard[j][i] = ' ';
}
}
if (m_ShouldDrawGameBoard)
//DrawBoard();
boost::this_thread::sleep(boost::posix_time::millisec(100));
}
}
void Client::ReadFromServer()
{
int bytesRead = -1;
@@ -207,32 +143,6 @@ void Client::ParseSnapshot(char* data, size_t length)
}
}
void Client::DrawBoard()
{
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
if (m_PlayerPositions[i].x != -1 && m_PlayerPositions[i].y != -1) {
m_GameBoard[static_cast<int>(m_PlayerPositions[i].x)][static_cast<int>(m_PlayerPositions[i].y)] = m_PlayerNames[i][0];
}
}
system("cls");
for (size_t i = 0; i < BOARDSIZE; i++) {
std::cout << '_';
}
std::cout << std::endl;
for (size_t i = 0; i < BOARDSIZE; i++) {
for (size_t j = 0; j < BOARDSIZE; j++) {
std::cout << m_GameBoard[j][i];
}
std::cout << std::endl;
}
for (size_t i = 0; i < BOARDSIZE; i++) {
std::cout << "^";
}
}
int Client::Receive(char* data, size_t length)
{
int bytesReceived = m_Socket.receive_from(boost
@@ -282,28 +192,27 @@ void Client::Disconnect()
delete[] dataPackage;
}
void Client::Ping()
{
char* dataPackage = new char[INPUTSIZE];
if (GetAsyncKeyState('P')) { // Maybe use previous key here
int length = CreateMessage(MessageType::ClientPing, "Ping", dataPackage);
m_StartPingTime = std::clock();
m_Socket.send_to(boost::asio::buffer(
dataPackage,
length),
m_ReceiverEndpoint, 0);
}
memset(dataPackage, 0, INPUTSIZE);
delete[] dataPackage;
}
void Client::MoveMessageHead(char*& data, size_t& length, size_t stepSize)
{
data += stepSize;
length -= stepSize;
}
void Client::SendDebugInput()
{
char* dataPackage = new char[INPUTSIZE];
if (GetAsyncKeyState('P')) { // Maybe use previous key here
int length = CreateMessage(MessageType::ClientPing, "Ping", dataPackage);
m_StartPingTime = std::clock();
m_Socket.send_to(boost::asio::buffer(
dataPackage,
length),
m_ReceiverEndpoint, 0);
}
memset(dataPackage, 0, INPUTSIZE);
delete[] dataPackage;
}
bool Client::OnKeyDown(const Events::KeyDown& event)
{
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
@@ -341,7 +250,9 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
if (event.KeyCode == GLFW_KEY_C) {
Connect();
}
if (event.KeyCode == GLFW_KEY_P) {
Ping();
}
memset(dataPackage, 0, INPUTSIZE);
delete[] dataPackage;
return true;