The Battle for Wesnoth  1.17.0-dev
Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
wesnothd_connection Class Reference

A class that represents a TCP/IP connection to the wesnothd server. More...

#include <wesnothd_connection.hpp>

Public Types

using error = wesnothd_connection_error
 

Public Member Functions

 wesnothd_connection (const wesnothd_connection &)=delete
 
wesnothd_connectionoperator= (const wesnothd_connection &)=delete
 
 ~wesnothd_connection ()
 
 wesnothd_connection (const std::string &host, const std::string &service)
 Constructor. More...
 
void send_data (const configr_of &request)
 Queues the given data to be sent to the server. More...
 
bool receive_data (config &result)
 Receives the next pending data pack from the server, if available. More...
 
bool wait_and_receive_data (config &data)
 Unlike receive_data, waits until data is available instead of returning immediately. More...
 
void wait_for_handshake ()
 Waits until the server handshake is complete. More...
 
bool using_tls () const
 True if connection is currently using TLS and thus is allowed to send cleartext passwords or auth tokens. More...
 
void cancel ()
 
void stop ()
 
std::size_t bytes_to_write () const
 
std::size_t bytes_written () const
 
std::size_t bytes_to_read () const
 
std::size_t bytes_read () const
 
bool has_data_received () const
 
bool is_sending_data () const
 

Private Types

typedef boost::asio::ip::tcp::resolver resolver
 
typedef std::unique_ptr< boost::asio::ip::tcp::socket > raw_socket
 
typedef std::unique_ptr< boost::asio::ssl::stream< raw_socket::element_type > > tls_socket
 
typedef utils::variant< raw_socket, tls_socketany_socket
 
using results_type = resolver::results_type
 
using endpoint = const boost::asio::ip::tcp::endpoint &
 
template<typename T >
using data_queue = std::queue< T, std::list< T > >
 

Private Member Functions

void handle_resolve (const boost::system::error_code &ec, results_type results)
 
void handle_connect (const boost::system::error_code &ec, endpoint endpoint)
 
void handshake ()
 
void handle_handshake (const boost::system::error_code &ec)
 
void fallback_to_unencrypted ()
 
std::size_t is_write_complete (const boost::system::error_code &error, std::size_t bytes_transferred)
 
void handle_write (const boost::system::error_code &ec, std::size_t bytes_transferred)
 
std::size_t is_read_complete (const boost::system::error_code &error, std::size_t bytes_transferred)
 
void handle_read (const boost::system::error_code &ec, std::size_t bytes_transferred)
 
void send ()
 
void recv ()
 

Private Attributes

std::thread worker_thread_
 
boost::asio::io_context io_context_
 
resolver resolver_
 
boost::asio::ssl::context tls_context_
 
std::string host_
 
std::string service_
 
bool use_tls_
 
any_socket socket_
 
boost::system::error_code last_error_
 
std::mutex last_error_mutex_
 
std::promise< void > handshake_finished_
 
boost::asio::streambuf read_buf_
 
uint32_t handshake_response_
 
data_queue< std::unique_ptr< boost::asio::streambuf > > send_queue_
 
data_queue< configrecv_queue_
 
std::mutex recv_queue_mutex_
 
std::condition_variable recv_queue_lock_
 
uint32_t payload_size_
 
std::size_t bytes_to_write_
 
std::size_t bytes_written_
 
std::size_t bytes_to_read_
 
std::size_t bytes_read_
 

Detailed Description

A class that represents a TCP/IP connection to the wesnothd server.

Definition at line 53 of file wesnothd_connection.hpp.

Member Typedef Documentation

◆ any_socket

typedef utils::variant<raw_socket, tls_socket> wesnothd_connection::any_socket
private

Definition at line 151 of file wesnothd_connection.hpp.

◆ data_queue

template<typename T >
using wesnothd_connection::data_queue = std::queue<T, std::list<T> >
private

Definition at line 186 of file wesnothd_connection.hpp.

◆ endpoint

using wesnothd_connection::endpoint = const boost::asio::ip::tcp::endpoint&
private

Definition at line 164 of file wesnothd_connection.hpp.

◆ error

Definition at line 56 of file wesnothd_connection.hpp.

◆ raw_socket

typedef std::unique_ptr<boost::asio::ip::tcp::socket> wesnothd_connection::raw_socket
private

Definition at line 149 of file wesnothd_connection.hpp.

◆ resolver

typedef boost::asio::ip::tcp::resolver wesnothd_connection::resolver
private

Definition at line 142 of file wesnothd_connection.hpp.

◆ results_type

using wesnothd_connection::results_type = resolver::results_type
private

Definition at line 163 of file wesnothd_connection.hpp.

◆ tls_socket

typedef std::unique_ptr<boost::asio::ssl::stream<raw_socket::element_type> > wesnothd_connection::tls_socket
private

Definition at line 150 of file wesnothd_connection.hpp.

Constructor & Destructor Documentation

◆ wesnothd_connection() [1/2]

wesnothd_connection::wesnothd_connection ( const wesnothd_connection )
delete

◆ ~wesnothd_connection()

wesnothd_connection::~wesnothd_connection ( )

Definition at line 116 of file wesnothd_connection.cpp.

References MPTEST_LOG, socket_, stop(), worker_thread_, and write().

◆ wesnothd_connection() [2/2]

wesnothd_connection::wesnothd_connection ( const std::string &  host,
const std::string &  service 
)

Constructor.

Parameters
hostName of the host to connect to
serviceService identifier such as "80" or "http"

Definition at line 61 of file wesnothd_connection.cpp.

References bytes_read_, bytes_to_read_, bytes_to_write_, bytes_written_, handle_resolve(), handshake_finished_, handshake_response_, io_context_, last_error_, last_error_mutex_, LOG_NW, MPTEST_LOG, payload_size_, read_buf_, recv_queue_, recv_queue_lock_, recv_queue_mutex_, resolver_, use_tls_, and worker_thread_.

Member Function Documentation

◆ bytes_read()

std::size_t wesnothd_connection::bytes_read ( ) const
inline

Definition at line 122 of file wesnothd_connection.hpp.

References bytes_read_.

◆ bytes_to_read()

std::size_t wesnothd_connection::bytes_to_read ( ) const
inline

Definition at line 117 of file wesnothd_connection.hpp.

References bytes_to_read_.

◆ bytes_to_write()

std::size_t wesnothd_connection::bytes_to_write ( ) const
inline

Definition at line 107 of file wesnothd_connection.hpp.

References bytes_to_write_.

◆ bytes_written()

std::size_t wesnothd_connection::bytes_written ( ) const
inline

Definition at line 112 of file wesnothd_connection.hpp.

References bytes_written_.

◆ cancel()

void wesnothd_connection::cancel ( )

Definition at line 305 of file wesnothd_connection.cpp.

References MPTEST_LOG, socket_, and WRN_NW.

Referenced by using_tls().

◆ fallback_to_unencrypted()

void wesnothd_connection::fallback_to_unencrypted ( )
private

Definition at line 236 of file wesnothd_connection.cpp.

References handle_connect(), socket_, and use_tls_.

Referenced by handle_handshake().

◆ handle_connect()

void wesnothd_connection::handle_connect ( const boost::system::error_code &  ec,
endpoint  endpoint 
)
private

Definition at line 147 of file wesnothd_connection.cpp.

References ERR_NW, handshake(), LOG_NW, MPTEST_LOG, and use_tls_.

Referenced by fallback_to_unencrypted(), and handle_resolve().

◆ handle_handshake()

void wesnothd_connection::handle_handshake ( const boost::system::error_code &  ec)
private

◆ handle_read()

void wesnothd_connection::handle_read ( const boost::system::error_code &  ec,
std::size_t  bytes_transferred 
)
private

◆ handle_resolve()

void wesnothd_connection::handle_resolve ( const boost::system::error_code &  ec,
results_type  results 
)
private

Definition at line 134 of file wesnothd_connection.cpp.

References handle_connect(), LOG_NW, MPTEST_LOG, and socket_.

Referenced by wesnothd_connection().

◆ handle_write()

void wesnothd_connection::handle_write ( const boost::system::error_code &  ec,
std::size_t  bytes_transferred 
)
private

Definition at line 359 of file wesnothd_connection.cpp.

References DBG_NW, io_context_, last_error_, last_error_mutex_, LOG_NW, MPTEST_LOG, send(), and send_queue_.

Referenced by send().

◆ handshake()

void wesnothd_connection::handshake ( )
private

Definition at line 164 of file wesnothd_connection.cpp.

References handle_handshake(), handshake_response_, MPTEST_LOG, socket_, and use_tls_.

Referenced by handle_connect().

◆ has_data_received()

bool wesnothd_connection::has_data_received ( ) const
inline

Definition at line 127 of file wesnothd_connection.hpp.

References recv_queue_.

Referenced by wait_and_receive_data().

◆ is_read_complete()

std::size_t wesnothd_connection::is_read_complete ( const boost::system::error_code &  error,
std::size_t  bytes_transferred 
)
private

◆ is_sending_data()

bool wesnothd_connection::is_sending_data ( ) const
inline

Definition at line 132 of file wesnothd_connection.hpp.

References send_queue_.

◆ is_write_complete()

std::size_t wesnothd_connection::is_write_complete ( const boost::system::error_code &  error,
std::size_t  bytes_transferred 
)
private

◆ operator=()

wesnothd_connection& wesnothd_connection::operator= ( const wesnothd_connection )
delete

◆ receive_data()

bool wesnothd_connection::receive_data ( config result)

Receives the next pending data pack from the server, if available.

Parameters
resultThe object to which the received data will be written.
Returns
True if any data was available, false otherwise.

Definition at line 491 of file wesnothd_connection.cpp.

References _(), last_error_, last_error_mutex_, MPTEST_LOG, recv_queue_, recv_queue_mutex_, and config::swap().

Referenced by gui2::dialogs::mp_join_game::network_handler(), gui2::dialogs::mp_lobby::network_handler(), gui2::dialogs::mp_staging::network_handler(), playmp_controller::receive_from_wesnothd(), and wait_and_receive_data().

◆ recv()

void wesnothd_connection::recv ( )
private

Definition at line 479 of file wesnothd_connection.cpp.

References handle_read(), is_read_complete(), MPTEST_LOG, read_buf_, and socket_.

Referenced by handle_handshake(), and handle_read().

◆ send()

void wesnothd_connection::send ( )
private

◆ send_data()

void wesnothd_connection::send_data ( const configr_of request)

◆ stop()

void wesnothd_connection::stop ( )

Definition at line 331 of file wesnothd_connection.cpp.

References io_context_, and MPTEST_LOG.

Referenced by using_tls(), and ~wesnothd_connection().

◆ using_tls()

bool wesnothd_connection::using_tls ( ) const
inline

True if connection is currently using TLS and thus is allowed to send cleartext passwords or auth tokens.

Definition at line 98 of file wesnothd_connection.hpp.

References cancel(), socket_, and stop().

◆ wait_and_receive_data()

bool wesnothd_connection::wait_and_receive_data ( config data)

Unlike receive_data, waits until data is available instead of returning immediately.

Parameters
dataConfig object passed to receive_data
Returns
True, since data will always be available.

Definition at line 520 of file wesnothd_connection.cpp.

References has_data_received(), receive_data(), recv_queue_lock_, and recv_queue_mutex_.

Referenced by gui2::dialogs::mp_join_game::fetch_game_config(), and playmp_controller::wait_for_upload().

◆ wait_for_handshake()

void wesnothd_connection::wait_for_handshake ( )

Waits until the server handshake is complete.

Definition at line 249 of file wesnothd_connection.cpp.

References e, lg::err(), handshake_finished_, LOG_NW, MPTEST_LOG, and WRN_NW.

Member Data Documentation

◆ bytes_read_

std::size_t wesnothd_connection::bytes_read_
private

Definition at line 201 of file wesnothd_connection.hpp.

Referenced by bytes_read(), is_read_complete(), and wesnothd_connection().

◆ bytes_to_read_

std::size_t wesnothd_connection::bytes_to_read_
private

◆ bytes_to_write_

std::size_t wesnothd_connection::bytes_to_write_
private

◆ bytes_written_

std::size_t wesnothd_connection::bytes_written_
private

◆ handshake_finished_

std::promise<void> wesnothd_connection::handshake_finished_
private

◆ handshake_response_

uint32_t wesnothd_connection::handshake_response_
private

Definition at line 172 of file wesnothd_connection.hpp.

Referenced by handle_handshake(), handshake(), and wesnothd_connection().

◆ host_

std::string wesnothd_connection::host_
private

Definition at line 147 of file wesnothd_connection.hpp.

Referenced by handle_handshake().

◆ io_context_

boost::asio::io_context wesnothd_connection::io_context_
private

◆ last_error_

boost::system::error_code wesnothd_connection::last_error_
private

◆ last_error_mutex_

std::mutex wesnothd_connection::last_error_mutex_
private

◆ payload_size_

uint32_t wesnothd_connection::payload_size_
private

Definition at line 195 of file wesnothd_connection.hpp.

Referenced by send(), and wesnothd_connection().

◆ read_buf_

boost::asio::streambuf wesnothd_connection::read_buf_
private

Definition at line 161 of file wesnothd_connection.hpp.

Referenced by handle_read(), is_read_complete(), recv(), and wesnothd_connection().

◆ recv_queue_

data_queue<config> wesnothd_connection::recv_queue_
private

◆ recv_queue_lock_

std::condition_variable wesnothd_connection::recv_queue_lock_
private

◆ recv_queue_mutex_

std::mutex wesnothd_connection::recv_queue_mutex_
private

◆ resolver_

resolver wesnothd_connection::resolver_
private

Definition at line 143 of file wesnothd_connection.hpp.

Referenced by wesnothd_connection().

◆ send_queue_

data_queue<std::unique_ptr<boost::asio::streambuf> > wesnothd_connection::send_queue_
private

Definition at line 188 of file wesnothd_connection.hpp.

Referenced by handle_write(), is_sending_data(), send(), and send_data().

◆ service_

std::string wesnothd_connection::service_
private

Definition at line 148 of file wesnothd_connection.hpp.

◆ socket_

any_socket wesnothd_connection::socket_
private

◆ tls_context_

boost::asio::ssl::context wesnothd_connection::tls_context_
private

Definition at line 145 of file wesnothd_connection.hpp.

Referenced by handle_handshake().

◆ use_tls_

bool wesnothd_connection::use_tls_
private

◆ worker_thread_

std::thread wesnothd_connection::worker_thread_
private

Definition at line 138 of file wesnothd_connection.hpp.

Referenced by wesnothd_connection(), and ~wesnothd_connection().


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