Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Server.cpp
Go to the documentation of this file.
1
2
3#include "Headers/Server.h"
4#include "Headers/Session.h"
5
8
9using namespace boost::asio;
10
11namespace Divide
12{
14 : _channel( std::make_shared<channel>() )
15 {
16 }
17
19 {
20 close();
21 }
22
24 {
25 if ( _thread == nullptr )
26 {
27 return; // stopped
28 }
29
30 _acceptor->close();
31 _ioService.stop();
32 _thread->join();
33 _thread.reset();
34 }
35
36 void Server::init( const U16 port, const string& broadcast_endpoint_address, const bool debugOutput )
37 {
38 if ( _thread == nullptr )
39 {
40 return;
41 }
42
43 _debugOutput = debugOutput;
44 try
45 {
46 const boost::asio::ip::tcp::endpoint listen_endpoint( boost::asio::ip::tcp::v4(), port );
47 const boost::asio::ip::udp::endpoint broadcast_endpoint( boost::asio::ip::address::from_string( broadcast_endpoint_address.c_str() ), port );
48
49 _acceptor = std::make_unique<tcp_acceptor>( _ioService.get_executor(), listen_endpoint );
50 const subscriber_ptr bc( new udp_broadcaster( _ioService, broadcast_endpoint ) );
51 _channel->join( bc );
52 tcp_session_ptr new_session( new Session( _ioService, *_channel ) );
53
54 _acceptor->async_accept(
55 new_session->getSocket(),
56 [&]( const boost::system::error_code code )
57 {
58 handle_accept( new_session, code );
59 }
60 );
61
62 std::unique_ptr<io_context::work> work( new io_context::work( _ioService ) );
63 _thread = std::make_unique<std::thread>( [this]()
64 {
65 _ioService.run();
66 } );
67
68 }
69 catch ( std::exception& e )
70 {
71 ASIO::LOG_PRINT( Util::StringFormat(LOCALE_STR("SERVER_EXCEPTION"), e.what()).c_str(), true );
72 }
73 }
74
75 void Server::handle_accept( const tcp_session_ptr& session, const boost::system::error_code& ec )
76 {
77 if ( !ec )
78 {
79 if ( _debugOutput )
80 {
81 ASIO::LOG_PRINT( LOCALE_STR("SERVER_ACCEPT_TCP") );
82 }
83 session->start();
84
85 tcp_session_ptr new_session( new Session( _ioService, *_channel ) );
86
87 _acceptor->async_accept(
88 new_session->getSocket(),
89 [&]( const boost::system::error_code code )
90 {
91 handle_accept( new_session, code );
92 } );
93 }
94 else
95 {
96 ASIO::LOG_PRINT( Util::StringFormat(LOCALE_STR("SERVER_ACCEPT_ERROR"), ec.what()).c_str(), true );
97 }
98 }
99
100}; // namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
static void LOG_PRINT(const char *msg, bool error=false)
Definition: ASIO.cpp:123
std::unique_ptr< std::thread > _thread
Definition: Server.h:60
void close()
Definition: Server.cpp:23
void handle_accept(const tcp_session_tpl_ptr &session, const boost::system::error_code &ec)
Definition: Server.cpp:75
void init(U16 port, const string &broadcast_endpoint_address, bool debugOutput)
Definition: Server.cpp:36
std::unique_ptr< tcp_acceptor > _acceptor
Definition: Server.h:61
channel_ptr _channel
Definition: Server.h:62
bool _debugOutput
Definition: Server.h:63
boost::asio::io_context _ioService
Definition: Server.h:59
Str StringFormat(const char *fmt, Args &&...args)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::shared_ptr< tcp_session_tpl > tcp_session_ptr
uint16_t U16
std::shared_ptr< subscriber > subscriber_ptr