Simple Web Game Server  0.1
A C++ library for creating authenticated scalable backends for multiplayer web games.
Classes | Public Types | Public Member Functions | List of all members
simple_web_game_server::base_server< player_traits, jwt_clock, json_traits, server_config, close_reasons > Class Template Reference

A WebSocket server that performs authentication and manages sessions. More...

#include <base_server.hpp>

Classes

class  server_error
 The class representing errors with the base_server. More...
 

Public Types

using combined_id = typename player_traits::id
 The type of a client id.
 
using player_id = typename combined_id::player_id
 The type of the player component of a client id.
 
using session_id = typename combined_id::session_id
 The type of the session component of a client id.
 
using id_hash = typename combined_id::hash
 The type of the hash struct for all id types.
 
using json = typename json_traits::json
 The type of a json object.
 
using clock = std::chrono::high_resolution_clock
 The type of clock for server time-step management.
 
using ssl_context_ptr = websocketpp::lib::shared_ptr< websocketpp::lib::asio::ssl::context >
 The type of a pointer to an asio ssl context.
 

Public Member Functions

 base_server (const jwt::verifier< jwt_clock, json_traits > &v, function< std::string(const combined_id &, const json &)> f, std::chrono::milliseconds t)
 The constructor for the base_server class. More...
 
void set_tls_init_handler (function< ssl_context_ptr(connection_hdl)> f)
 Sets a the given function f as the tls_init_handler for m_server.
 
void set_open_handler (function< void(const combined_id &, json &&)> f)
 Sets the given function to be called when a client sends a valid JWT.
 
void set_close_handler (function< void(const combined_id &)> f)
 Sets the given function to be called when a client disconnects.
 
void set_message_handler (function< void(const combined_id &, std::string &&)> f)
 Sets the given function to be called when a client sends a message.
 
void run (uint16_t port, bool unlock_address)
 Runs the underlying websocketpp server m_server. More...
 
bool is_running ()
 Returns whether the underlying WebSocket++ server is running.
 
void reset ()
 Resets the server so it may be started again.
 
void stop ()
 Stops the server, closes all connections, and clears all actions.
 
void process_messages ()
 Worker loop that processes server actions. More...
 
std::size_t get_player_count ()
 Returns the number of verified clients connected.
 
void send_message (const combined_id &id, std::string &&msg)
 Asynchronously sends a message to the given client. More...
 
void complete_session (const session_id &sid, const session_id &result_sid, const json &result_data)
 Asynchronously closes the given session and sends out result tokens. More...
 

Detailed Description

template<typename player_traits, typename jwt_clock, typename json_traits, typename server_config, typename close_reasons>
class simple_web_game_server::base_server< player_traits, jwt_clock, json_traits, server_config, close_reasons >

A WebSocket server that performs authentication and manages sessions.

This class wraps an underlying websocketpp::server m_server. The player_traits template parameter must define a struct plyaer_traits::id with members id.player, id.session of types id::player_id, id::session_id respectively. The id::hash struct must hash id, player_id, and session_id. The id type must be defualt constructable and constructable from player_id and session_id.

The jwt_clock and json_traits parameters must meet the requirements of such template types in JWT++ library.

Constructor & Destructor Documentation

◆ base_server()

template<typename player_traits , typename jwt_clock , typename json_traits , typename server_config , typename close_reasons >
simple_web_game_server::base_server< player_traits, jwt_clock, json_traits, server_config, close_reasons >::base_server ( const jwt::verifier< jwt_clock, json_traits > &  v,
function< std::string(const combined_id &, const json &)>  f,
std::chrono::milliseconds  t 
)
inline

The constructor for the base_server class.

Takes a jwt::verifier v to authenticate client tokens, a function f to construct result tokens for each complete session, and the length of time in milliseconds t that complete session data should remain in memory. It is recommend that the time t should always be at least as long as the time it takes for any issued JWT that can be verified with v to expire.

Member Function Documentation

◆ complete_session()

template<typename player_traits , typename jwt_clock , typename json_traits , typename server_config , typename close_reasons >
void simple_web_game_server::base_server< player_traits, jwt_clock, json_traits, server_config, close_reasons >::complete_session ( const session_id sid,
const session_id result_sid,
const json result_data 
)
inline

Asynchronously closes the given session and sends out result tokens.

Submits actions to close all clients associated with the given session id and send each a result string constructed with m_get_result_str, the function provided via the constructor. Stores sid and the associated data in m_locked_sessions for m_session_release_time milliseconds so clients connecting with the same session id are sent the same result string.

◆ process_messages()

template<typename player_traits , typename jwt_clock , typename json_traits , typename server_config , typename close_reasons >
void simple_web_game_server::base_server< player_traits, jwt_clock, json_traits, server_config, close_reasons >::process_messages ( )
inline

Worker loop that processes server actions.

Continually pulls work from the queue m_actions. May be run by multiple threads if desired.

◆ run()

template<typename player_traits , typename jwt_clock , typename json_traits , typename server_config , typename close_reasons >
void simple_web_game_server::base_server< player_traits, jwt_clock, json_traits, server_config, close_reasons >::run ( uint16_t  port,
bool  unlock_address 
)
inline

Runs the underlying websocketpp server m_server.

May be called by multiple threads if desired, so long as unlock_address is true.

◆ send_message()

template<typename player_traits , typename jwt_clock , typename json_traits , typename server_config , typename close_reasons >
void simple_web_game_server::base_server< player_traits, jwt_clock, json_traits, server_config, close_reasons >::send_message ( const combined_id id,
std::string &&  msg 
)
inline

Asynchronously sends a message to the given client.

Submits an action to the queue m_actions to send the text msg to the client associated with id.


The documentation for this class was generated from the following file: