-
Notifications
You must be signed in to change notification settings - Fork 1
/
a3s.h
84 lines (75 loc) · 1.86 KB
/
a3s.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @file a3s.h
*
*
* @date 24/11/2014
* @uthor patxi
*/
#ifndef A3S_H_
#define A3S_H_
#include <cstdlib>
#include <cstddef>
#include <cstdint>
#include <string>
#include <functional>
#include <boost/asio.hpp>
namespace patxi {
class a3s
{
public:
typedef enum {
NORMAL,
LOGIN,
BROADCAST_RESPONSE
} request_type;
using on_login_func = std::function<void (bool)>;
using on_data_func = std::function<void (char const*, std::size_t)>;
static const std::size_t MAX_COMMAND_LENGTH = 256;
static const std::size_t MSG_MAX_LENGTH = 1024;
static const std::size_t PAYLOAD_MAX_LENGTH = 256;
a3s(boost::asio::io_service& io_service, uint16_t port
,std::string const& addr, on_data_func on_data=nullptr
,on_login_func on_login=nullptr);
~a3s();
void login(std::string const& password);
void onData(on_data_func fnc) {on_data_ = fnc;}
void onLogin(on_login_func fnc) {on_login_ = fnc;}
void consoleInput();
void send(char const* data, std::size_t length=0);
bool listening() const {return listening_;}
bool logedIn() const {return loged_in_;}
private:
#pragma pack(push, 1)
struct reqheader_t {
char begin_header[2];
uint32_t checksum;
uint8_t end_header;
uint8_t begin_msg;
reqheader_t()
:begin_header{'B','E'}
,checksum{0}
,end_header{0xff}
,begin_msg{0} {}
};
struct request_t {
reqheader_t hdr;
char payload[PAYLOAD_MAX_LENGTH];
request_t(): hdr() {}
};
#pragma pack(pop)
void receive();
void send_request(char const* data, std::size_t length,
request_type rtp=NORMAL);
bool loged_in_;
bool listening_;
uint8_t sequence_;
on_data_func on_data_;
on_login_func on_login_;
boost::asio::ip::udp::socket socket_;
boost::asio::posix::stream_descriptor input_;
boost::asio::streambuf console_data_;
boost::asio::ip::udp::endpoint endpoint_;
char net_data_[MSG_MAX_LENGTH];
};
} // namespace patxi
#endif // A3S_H_