Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
Server.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/asio/ip/tcp.hpp>
4 #include <thread>
5 #include <vector>
6 
8 #include "world/GameWorld.hpp"
9 #include <network.h>
10 
14 class Server
15 {
16 public:
17  Server();
18 
22  void acceptConnections();
23 
27  void processMessages();
28 
33  void handleMessage(const Message& msg);
34 
35 private:
39  void ticker();
40 
44  void tick();
45 
49  void sendGameState();
50 
54  void initGameState();
55 
63  bool loadPlayer(std::string username, std::string password, const std::shared_ptr<Connection>& connection);
64 
69  void savePlayer(int playerId);
70 
71  boost::asio::io_context ioContextM;
72 
73  boost::asio::ip::tcp::acceptor acceptorM;
74 
75  std::vector<std::shared_ptr<Connection>> connectionsM;
76 
77  MessageQueue messagesM;
78 
79  std::thread ioThreadM;
80 
81  std::thread acceptThreadM;
82 
83  std::thread tickThreadM;
84 
85  boost::asio::executor_work_guard<boost::asio::io_context::executor_type> workGuardM;
86 
88 
90 };
Class for handling database queries.
Definition: DatabaseManager.hpp:23
GameWorld class. It contains all the player, npc, object and item instances.
Definition: GameWorld.hpp:16
Main class for the server.
Definition: Server.hpp:15
void handleMessage(const Message &msg)
Function to parse and act upon messages received from clients.
Definition: Server.cpp:85
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > workGuardM
Definition: Server.hpp:85
void sendGameState()
Sends the current gamestate to connected clients.
Definition: Server.cpp:251
void tick()
Function that updates the gamestate on each server tick, called by ticker()
Definition: Server.cpp:229
void savePlayer(int playerId)
Saves a player's data to the database.
Definition: Server.cpp:449
DatabaseManager dbManagerM
Definition: Server.hpp:89
void initGameState()
Initializes the gamestate and gameworld.
Definition: Server.cpp:403
std::vector< std::shared_ptr< Connection > > connectionsM
Definition: Server.hpp:75
std::thread ioThreadM
Definition: Server.hpp:79
void ticker()
Timer function that is used to start server ticks, runs on a separate thread.
Definition: Server.cpp:220
void acceptConnections()
Function to accept new connections from clients, runs on a seperate thread.
Definition: Server.cpp:46
std::thread acceptThreadM
Definition: Server.hpp:81
MessageQueue messagesM
Definition: Server.hpp:77
boost::asio::io_context ioContextM
Definition: Server.hpp:71
GameWorld gameWorldM
Definition: Server.hpp:87
Server()
Definition: Server.cpp:21
boost::asio::ip::tcp::acceptor acceptorM
Definition: Server.hpp:73
bool loadPlayer(std::string username, std::string password, const std::shared_ptr< Connection > &connection)
Loads a player's data from the database and adds the player to the game.
Definition: Server.cpp:408
void processMessages()
Server's main loop for processing messages.
Definition: Server.cpp:70
std::thread tickThreadM
Definition: Server.hpp:83