From 0e8e41e4fbbd1a3a12db171743ebd9893a480c03 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Wed, 28 Sep 2016 00:26:36 +0200 Subject: [PATCH] server open and event loop --- game/CMakeLists.txt | 3 +- game/main.cpp | 23 +++++++++++++--- game/opengl.cpp | 18 ++++-------- game/opengl.hpp | 37 ++++++++++++++----------- game/server.cpp | 67 --------------------------------------------- game/server.hpp | 44 ++++++++++++++++++++--------- game/session.hpp | 63 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 114 deletions(-) delete mode 100644 game/server.cpp create mode 100644 game/session.hpp diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 8d2e44f..d3995c4 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -35,7 +35,8 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${OPENGL_INCLUDE_DIR}) include_directories(${CMAKE_SOURCE_DIR}/libs/glm/) +include_directories(${CMAKE_SOURCE_DIR}/libs/asio/asio/include/) add_executable(game ${GAME_SRC} ${GAME_HEADERS}) setup_target(game) -target_link_libraries(game X11 epoxy) +target_link_libraries(game X11 epoxy pthread) diff --git a/game/main.cpp b/game/main.cpp index bc79379..6be6ec0 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -1,17 +1,24 @@ -#include "opengl.hpp" - #include #include +#include +#include +#include "opengl.hpp" #include "game_window.hpp" +#include "server.hpp" #include "options.hpp" uint64_t optionsFlags; + +using asio::ip::tcp; + using namespace std; int main(int argc, char *argv[]) { + + char port[]="3490"; char c; static struct option long_options[] = @@ -56,8 +63,16 @@ int main(int argc, char *argv[]) { } } + asio::io_service io_service; + Server s(io_service, atoi(port) ); + GameWindow window(500, 500); window.set_maxfps(60.0); - window.loop(); - window.stop(); + + while(window.running()){ + window.poll(); + io_service.poll(); + } + + return 0; } diff --git a/game/opengl.cpp b/game/opengl.cpp index fde9da1..0ac19ad 100644 --- a/game/opengl.cpp +++ b/game/opengl.cpp @@ -101,18 +101,11 @@ void endofthejedi::GLWindow::handle(XEvent event) { void endofthejedi::GLWindow::swap() { glXSwapBuffers(m_display, m_window); } -void endofthejedi::GLWindow::loop() { - m_running = true; +void endofthejedi::GLWindow::poll() { - timespec prev; - timespec current; - clock_gettime(CLOCK_MONOTONIC_RAW, &prev); - clock_gettime(CLOCK_MONOTONIC_RAW, ¤t); + clock_gettime(CLOCK_MONOTONIC_RAW, &prev); + clock_gettime(CLOCK_MONOTONIC_RAW, ¤t); - double delta = 0.0; - double sleeptime = 0.0; - - while (m_running) { handleevents(); render(delta); swap(); @@ -132,11 +125,10 @@ void endofthejedi::GLWindow::loop() { m_fps = (1000000000.0/delta); //std::cout << m_fps << "\n"; } - } } void endofthejedi::GLWindow::stop() { m_running = false; } - -void endofthejedi::GLWindow::init() {} +bool endofthejedi::GLWindow::running() { return m_running; } +void endofthejedi::GLWindow::init() { m_running = true; } void endofthejedi::GLWindow::render(double time) { UNUSED(time) } void endofthejedi::GLWindow::resize() {} diff --git a/game/opengl.hpp b/game/opengl.hpp index 2114682..d6aed00 100644 --- a/game/opengl.hpp +++ b/game/opengl.hpp @@ -10,11 +10,11 @@ namespace endofthejedi { class GLWindow { - private: + private: //X-related stuff - Display* m_display; - Window m_rootwnd; - GLint m_attributes[23] = + Display* m_display; + Window m_rootwnd; + GLint m_attributes[23] = { GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, GLX_RENDER_TYPE, GLX_RGBA_BIT, @@ -29,17 +29,21 @@ class GLWindow { None }; - XVisualInfo* m_visualinfo; - Colormap m_colormap; - XSetWindowAttributes m_swa; - Window m_window; - GLXContext m_glcontext; - XWindowAttributes m_gwa; + XVisualInfo* m_visualinfo; + Colormap m_colormap; + XSetWindowAttributes m_swa; + Window m_window; + GLXContext m_glcontext; + XWindowAttributes m_gwa; Atom m_atomWmDeleteWindow; //End of X related stuff unsigned int m_width; unsigned int m_height; + double delta = 0.0; + double sleeptime = 0.0; + timespec prev; + timespec current; //mainloop condition bool m_running = false; @@ -48,9 +52,7 @@ class GLWindow { //if maxfps = 0 there's no fps limit double m_maxfps; - void handleevents(); - - protected: + protected: //ancestors shall override these methods virtual void init(); //called by mainloop periodically @@ -62,16 +64,19 @@ class GLWindow { //as it handles the close calls & resizing virtual void handle(XEvent event); + virtual void handleevents(); + public: //initializes the X Window & creates an OpenGL context - GLWindow(unsigned int width, unsigned int height); - ~GLWindow(); + GLWindow(unsigned int width, unsigned int height); + ~GLWindow(); //mainloop does event handling & calls render/swap - void loop(); + void poll(); void swap(); //stops the mainloop by setting m_running false void stop(); + bool running(); //getters unsigned int getheight() const { diff --git a/game/server.cpp b/game/server.cpp deleted file mode 100644 index f2cdd77..0000000 --- a/game/server.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "server.hpp" - -#include "state/commands.hpp" - -#include "util.hpp" - -#include - -Game::Game() -{ - // advance simulation with 100 Hz - m_time_step = 1.0 / 100.0; - m_time_for_next_step = 0.0; - - m_state = new game::State(); - m_state->init(); - - m_state->addPlayer(0); - m_state->addPlayer(1); - m_state->addPlayer(2); - m_state->addPlayer(3); - //m_state->addPlayer(2); -} - -bool Game::cycle(float dt) -{ - static float acc = 0.0; - acc += dt; - - float spawnInterval = 0.1; - while(acc > spawnInterval) { - acc -= spawnInterval; - - float a = 2.0 * M_PI * util::randf_0_1(); - float speed = 0.002; - m_state->players[0]->addCommand(new game::ShootCommand(a, speed)); - } - -#if 1 - if (dt >= 10.0) { - //std::cout<<"time to big: " << dt << std::endl; - dt = m_time_step; - } - - //std::cout<<"adding dt: " << dt << std::endl; - m_time_for_next_step += dt; - - int steps = 0; - while(m_time_for_next_step >= m_time_step) { - //std::cout<<"time now: " << m_time_for_next_step << std::endl; - m_time_for_next_step -= m_time_step; - - m_state->advance(m_time_step); - steps++; - } - - //std::cout << m_time_for_next_step << " s remaining time, " << steps << " steps taken." << std::endl; - - return true; -#else - (void) dt; - - m_state->advance(dt); - - return true; -#endif -} diff --git a/game/server.hpp b/game/server.hpp index bc02597..7b6e59a 100644 --- a/game/server.hpp +++ b/game/server.hpp @@ -1,21 +1,39 @@ #pragma once -#include "state/state.hpp" +//#include +//#include +//#include +#include -class Server { +#include "session.hpp" + +using asio::ip::tcp; + +class Server +{ public: - Server(); - - // main method of the game. run this regulary - // return false if want to exit. - // bool cycle(float dt); - - // for rendering -// const game::State *state() const { return m_state; } + Server(asio::io_service& io_service, short port) + : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), + socket_(io_service) + { + do_accept(); + } private: - game::State *m_state; + void do_accept() + { + acceptor_.async_accept(socket_, + [this](std::error_code ec) + { + if (!ec) + { + std::make_shared(std::move(socket_))->start(); + } - float m_time_for_next_step; - float m_time_step; + do_accept(); + }); + } + + tcp::acceptor acceptor_; + tcp::socket socket_; }; diff --git a/game/session.hpp b/game/session.hpp new file mode 100644 index 0000000..0c1a04e --- /dev/null +++ b/game/session.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include +#include +#include +#include + +using asio::ip::tcp; + +class Session + : public std::enable_shared_from_this +{ +public: + Session(tcp::socket socket) + : socket_(std::move(socket)) + { + } + + void start() + { + 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(socket_, asio::buffer(hello, strlen(hello)), + [this, self](std::error_code ec, std::size_t /*length*/) + { + if (!ec) + { + do_read(); + } + }); + } + +private: + void do_read() + { + auto self(shared_from_this()); + socket_.async_read_some(asio::buffer(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(socket_, asio::buffer(data_, length), + [this, self](std::error_code ec, std::size_t /*length*/) + { + if (!ec) + { + do_read(); + } + }); + } + + tcp::socket socket_; + enum { max_length = 1024 }; + char data_[max_length]; +};