From eaff023ed3e8485a18b916c49733d96f4b28c997 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Wed, 28 Sep 2016 03:35:05 +0200 Subject: [PATCH] networking parse command --- game/CMakeLists.txt | 2 +- game/main.cpp | 1 - game/server.hpp | 3 --- game/session.hpp | 60 ++++++++------------------------------------ game/state/state.cpp | 2 +- game/state/state.hpp | 2 +- 6 files changed, 14 insertions(+), 56 deletions(-) diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index d3995c4..b807796 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -11,7 +11,7 @@ set(GAME_SRC renderer.cpp game_window.cpp triangle_window.cpp - + session.cpp util.cpp game.cpp state/object.cpp diff --git a/game/main.cpp b/game/main.cpp index f33a738..2718363 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -10,7 +10,6 @@ uint64_t optionsFlags; - using asio::ip::tcp; using namespace std; diff --git a/game/server.hpp b/game/server.hpp index 23bb4de..dc33e08 100644 --- a/game/server.hpp +++ b/game/server.hpp @@ -1,8 +1,5 @@ #pragma once -//#include -//#include -//#include #include #include "session.hpp" diff --git a/game/session.hpp b/game/session.hpp index cc7b3da..23a8a49 100644 --- a/game/session.hpp +++ b/game/session.hpp @@ -3,8 +3,11 @@ #include #include #include +#include #include +#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(); - } - }); - } - - tcp::socket m_socket; - enum { max_length = 1024 }; - char m_snd_data[max_length]; - char m_rcv_data[max_length]; + 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_rcv_data[max_length]; game::State* m_state; bool m_started = false; - int m_id; + int m_pid; }; diff --git a/game/state/state.cpp b/game/state/state.cpp index 1eb66d3..acc2502 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -70,7 +70,7 @@ namespace game { return playerId; } - void State::playerLeft(int playerId) + void State::quitPlayer(int playerId) { (void) playerId; } diff --git a/game/state/state.hpp b/game/state/state.hpp index 5058ef6..a526d06 100644 --- a/game/state/state.hpp +++ b/game/state/state.hpp @@ -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