Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ASIO.cpp
Go to the documentation of this file.
1
2
3#ifndef OPCODE_ENUM
4#define OPCODE_ENUM OPcodes
5#endif
6
8#include "Headers/ASIO.h"
9#include "Headers/Client.h"
10
13
14namespace Divide
15{
16
18
20 {
21 _ioService.stop();
22 _work.reset();
23 if ( _thread != nullptr )
24 {
25 _thread->join();
26 }
27 if ( _localClient != nullptr )
28 {
29 _localClient->stop();
30 }
31 }
32
34 {
35 if ( !_connected )
36 {
37 return;
38 }
40 p << _localClient->getSocket().local_endpoint().address().to_string();
41 sendPacket( p );
42 }
43
44 bool ASIO::init( const string& address, const U16 port )
45 {
46 try
47 {
49 _localClient = std::make_unique<Client>( this, _ioService, _debugOutput );
50 _work.reset( new boost::asio::io_context::work( _ioService ) );
51 _localClient->start( res.resolve( address, Util::to_string( port ) ) );
52 _thread = std::make_unique<std::thread>( [&]
53 {
54 SetThreadName("ASIO_THREAD");
55 _ioService.run();
56 } );
57 _ioService.poll();
58 _connected = true;
59 }
60 catch ( const std::exception& e )
61 {
62 if ( _debugOutput )
63 {
64 LOG_PRINT(Util::StringFormat( LOCALE_STR("ASIO_EXCEPTION"), e.what()).c_str(), true );
65 }
66 _connected = false;
67 }
68
69 return _connected;
70 }
71
72 bool ASIO::connect( const string& address, const U16 port )
73 {
74 if ( _connected )
75 {
76 close();
77 }
78
79 return init( address, port );
80 }
81
82 bool ASIO::isConnected() const noexcept
83 {
84 return _connected;
85 }
86
88 {
89 _localClient->stop();
90 _connected = false;
91 }
92
94 {
95 if ( !_connected )
96 {
97 return false;
98 }
99 if ( _localClient->sendPacket( p ) )
100 {
101
102 if ( _debugOutput )
103 {
104 LOG_PRINT( Util::StringFormat(LOCALE_STR("ASIO_OPCODE"), p.opcode() ).c_str() );
105 }
106 return true;
107 }
108
109 return false;
110 }
111
112 void ASIO::toggleDebugOutput( const bool debugOutput ) noexcept
113 {
114 _debugOutput = debugOutput;
115 _localClient->toggleDebugOutput( _debugOutput );
116 }
117
119 {
120 s_logCBK = cbk;
121 }
122
123 void ASIO::LOG_PRINT( const char* msg, const bool error )
124 {
125 if ( s_logCBK )
126 {
127 s_logCBK( msg, error );
128 }
129 else
130 {
131 if ( error )
132 {
133 Console::errorfn( msg );
134 }
135 else
136 {
137 Console::printfn( msg );
138 }
139 }
140 }
141
142}; // namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
#define NO_DESTROY
static LOG_CBK s_logCBK
Definition: ASIO.h:94
static void SET_LOG_FUNCTION(const LOG_CBK &cbk)
Definition: ASIO.cpp:118
virtual bool sendPacket(WorldPacket &p) const
Send a packet to the target server.
Definition: ASIO.cpp:93
bool _connected
Definition: ASIO.h:90
virtual bool init(const string &address, U16 port)
Init a connection to the target address:port.
Definition: ASIO.cpp:44
virtual ~ASIO()
Definition: ASIO.cpp:19
virtual void toggleDebugOutput(bool debugOutput) noexcept
Toggle the printing of debug information.
Definition: ASIO.cpp:112
void close()
Definition: ASIO.cpp:87
virtual bool connect(const string &address, U16 port)
Definition: ASIO.cpp:72
static void LOG_PRINT(const char *msg, bool error=false)
Definition: ASIO.cpp:123
Client_uptr _localClient
Definition: ASIO.h:89
std::unique_ptr< boost::asio::io_context::work > _work
Definition: ASIO.h:87
virtual bool isConnected() const noexcept
Check connection state;.
Definition: ASIO.cpp:82
DELEGATE< void, std::string_view, bool > LOG_CBK
Definition: ASIO.h:67
boost::asio::io_context _ioService
Definition: ASIO.h:86
virtual void disconnect()
Disconnect from the server.
Definition: ASIO.cpp:33
bool _debugOutput
Definition: ASIO.h:91
std::unique_ptr< std::thread > _thread
Definition: ASIO.h:88
static const ValueType CMSG_REQUEST_DISCONNECT
Definition: OPCodesTpl.h:21
Str StringFormat(const char *fmt, Args &&...args)
string to_string(GET_PASS_TYPE< T > value)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
void SetThreadName(std::string_view threadName) noexcept
boost::asio::ip::basic_resolver< boost::asio::ip::tcp, boost::asio::io_context::executor_type > tcp_resolver
Definition: Utils.h:47
uint16_t U16
static NO_INLINE void errorfn(const char *format, T &&... args)
static NO_INLINE void printfn(const char *format, T &&... args)