networking parse command
This commit is contained in:
parent
ef38da703e
commit
eaff023ed3
6 changed files with 14 additions and 56 deletions
|
@ -11,7 +11,7 @@ set(GAME_SRC
|
|||
renderer.cpp
|
||||
game_window.cpp
|
||||
triangle_window.cpp
|
||||
|
||||
session.cpp
|
||||
util.cpp
|
||||
game.cpp
|
||||
state/object.cpp
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
uint64_t optionsFlags;
|
||||
|
||||
|
||||
using asio::ip::tcp;
|
||||
|
||||
using namespace std;
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
//#include <iostream>
|
||||
//#include <memory>
|
||||
//#include <utility>
|
||||
#include <asio.hpp>
|
||||
|
||||
#include "session.hpp"
|
||||
|
|
|
@ -3,8 +3,11 @@
|
|||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <asio.hpp>
|
||||
|
||||
#include "game.hpp"
|
||||
|
||||
using asio::ip::tcp;
|
||||
|
||||
class Session
|
||||
|
@ -15,59 +18,18 @@ public:
|
|||
: m_socket(std::move(socket))
|
||||
{
|
||||
m_state = state;
|
||||
char c[]="> ";
|
||||
strcpy(m_snd_data, c);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
}
|
||||
void start();
|
||||
|
||||
private:
|
||||
void do_read()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
m_socket.async_read_some(asio::buffer(m_rcv_data, max_length),
|
||||
[this, self](std::error_code ec, std::size_t 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void do_read();
|
||||
void do_write(char m_snd_data[], std::size_t length);
|
||||
bool parse(std::string);
|
||||
tcp::socket m_socket;
|
||||
enum { max_length = 1024 };
|
||||
char m_snd_data[max_length];
|
||||
char m_rcv_data[max_length];
|
||||
game::State* m_state;
|
||||
bool m_started = false;
|
||||
int m_id;
|
||||
int m_pid;
|
||||
};
|
||||
|
|
|
@ -70,7 +70,7 @@ namespace game {
|
|||
return playerId;
|
||||
}
|
||||
|
||||
void State::playerLeft(int playerId)
|
||||
void State::quitPlayer(int playerId)
|
||||
{
|
||||
(void) playerId;
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ namespace game {
|
|||
// The upper layer (network/renderer) calling these three functions
|
||||
// should keep id's unique and give one (network) input an id.
|
||||
int addPlayer();
|
||||
void playerLeft(int playerId);
|
||||
void quitPlayer(int playerId);
|
||||
void commandForPlayer(int playerId, Command *cmd);
|
||||
|
||||
// lookup. return nullptr on invalid playerId
|
||||
|
|
Loading…
Reference in a new issue