networking parse command

This commit is contained in:
j3d1 2016-09-28 03:35:05 +02:00
parent ef38da703e
commit eaff023ed3
6 changed files with 14 additions and 56 deletions

View file

@ -11,7 +11,7 @@ set(GAME_SRC
renderer.cpp renderer.cpp
game_window.cpp game_window.cpp
triangle_window.cpp triangle_window.cpp
session.cpp
util.cpp util.cpp
game.cpp game.cpp
state/object.cpp state/object.cpp

View file

@ -10,7 +10,6 @@
uint64_t optionsFlags; uint64_t optionsFlags;
using asio::ip::tcp; using asio::ip::tcp;
using namespace std; using namespace std;

View file

@ -1,8 +1,5 @@
#pragma once #pragma once
//#include <iostream>
//#include <memory>
//#include <utility>
#include <asio.hpp> #include <asio.hpp>
#include "session.hpp" #include "session.hpp"

View file

@ -3,8 +3,11 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <string>
#include <asio.hpp> #include <asio.hpp>
#include "game.hpp"
using asio::ip::tcp; using asio::ip::tcp;
class Session class Session
@ -15,59 +18,18 @@ public:
: m_socket(std::move(socket)) : m_socket(std::move(socket))
{ {
m_state = state; m_state = state;
char c[]="> ";
strcpy(m_snd_data, c);
} }
void start() void start();
{
if(m_started) return;
m_started=true;
m_id = m_state->addPlayer();
auto self(shared_from_this());
char hello[] = "\nUse \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n> ";
asio::async_write(m_socket, asio::buffer(hello, strlen(hello)),
[this, self](std::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
do_read();
}
});
}
private: private:
void do_read() void do_read();
{ void do_write(char m_snd_data[], std::size_t length);
auto self(shared_from_this()); bool parse(std::string);
m_socket.async_read_some(asio::buffer(m_rcv_data, max_length), tcp::socket m_socket;
[this, self](std::error_code ec, std::size_t length) enum { max_length = 1024 };
{ char m_rcv_data[max_length];
if (!ec)
{
do_write(length);
}
});
}
void do_write(std::size_t length)
{
auto self(shared_from_this());
asio::async_write(m_socket, asio::buffer(m_snd_data, length),
[this, self](std::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
do_read();
}
});
}
tcp::socket m_socket;
enum { max_length = 1024 };
char m_snd_data[max_length];
char m_rcv_data[max_length];
game::State* m_state; game::State* m_state;
bool m_started = false; bool m_started = false;
int m_id; int m_pid;
}; };

View file

@ -70,7 +70,7 @@ namespace game {
return playerId; return playerId;
} }
void State::playerLeft(int playerId) void State::quitPlayer(int playerId)
{ {
(void) playerId; (void) playerId;
} }

View file

@ -37,7 +37,7 @@ namespace game {
// The upper layer (network/renderer) calling these three functions // The upper layer (network/renderer) calling these three functions
// should keep id's unique and give one (network) input an id. // should keep id's unique and give one (network) input an id.
int addPlayer(); int addPlayer();
void playerLeft(int playerId); void quitPlayer(int playerId);
void commandForPlayer(int playerId, Command *cmd); void commandForPlayer(int playerId, Command *cmd);
// lookup. return nullptr on invalid playerId // lookup. return nullptr on invalid playerId