Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ASIO.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_DIVIDE_BOOST_ASIO_TPL_H_
34#define DVD_DIVIDE_BOOST_ASIO_TPL_H_
35
36#include "WorldPacket.h"
37#include "Client.h"
38
39namespace Divide
40{
41
42#ifndef OPCODE_ENUM
43#error \
44 "Please include 'OPCodesTpl' and define custom OPcodes before using the networking library!"
45#endif
46
47 class OPCodes;
49 class ASIO
50 {
51 public:
53 virtual bool sendPacket( WorldPacket& p ) const;
55 virtual bool init( const string& address, U16 port );
58 virtual bool connect( const string& address, U16 port );
60 virtual void disconnect();
62 virtual bool isConnected() const noexcept;
64 virtual void toggleDebugOutput( bool debugOutput ) noexcept;
65
66
67 using LOG_CBK = DELEGATE<void, std::string_view /*msg*/, bool /*is_error*/>;
68 static void SET_LOG_FUNCTION( const LOG_CBK& cbk );
69 static void LOG_PRINT( const char* msg, bool error = false );
70
71 virtual ~ASIO();
72
73 protected:
74 ASIO() noexcept = default;
75
76 friend class Client;
77 void close();
78
79 // Define this functions to implement various packet handling (a switch
80 // statement for example)
81 // switch(p.getOpcode()) { case SMSG_XXXXX: bla bla bla break; case
82 // MSG_HEARTBEAT: break;}
83 virtual void handlePacket( WorldPacket& p ) = 0;
84
85 protected:
86 boost::asio::io_context _ioService;
87 std::unique_ptr<boost::asio::io_context::work> _work;
88 std::unique_ptr<std::thread> _thread;
89 Client_uptr _localClient;
90 bool _connected{false};
91 bool _debugOutput{true};
92 string _address, _port;
93
95 };
96
97}; // namespace Divide
98
99#endif //DVD_DIVIDE_BOOST_ASIO_TPL_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
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
string _port
Definition: ASIO.h:92
bool _connected
Definition: ASIO.h:90
string _address
Definition: ASIO.h:92
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 void handlePacket(WorldPacket &p)=0
ASIO() noexcept=default
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
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
DELEGATE_STD< Ret, Args... > DELEGATE
uint16_t U16