forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_msg_bus_client.hpp
98 lines (78 loc) · 2.67 KB
/
sf_msg_bus_client.hpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @version 1.0.0
* @author skyfire
* @mail skyfireitdiy@hotmail.com
* @see http://github.com/skyfireitdiy/sflib
* @file sf_msg_bus_client.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
/*
* sf_msg_bus_client 消息总线客户端
*/
#pragma once
#include "sf_msg_bus_client.h"
namespace skyfire
{
inline std::shared_ptr<sf_msg_bus_client> sf_msg_bus_client::make_client() {
return std::make_shared<sf_msg_bus_client>();
}
inline sf_msg_bus_client::sf_msg_bus_client() {
sf_bind_signal(
p_client__,
data_coming,
std::bind(
&sf_msg_bus_client::on_reg_data__,
this,
std::placeholders::_1,
std::placeholders::_2
),
true
);
}
inline sf_msg_bus_client::~sf_msg_bus_client() {
close();
}
inline void sf_msg_bus_client::reg_msg_to_bus(const std::string &type) {
p_client__->send(msg_bus_reg_type_single, sf_serialize_binary(type));
}
inline void sf_msg_bus_client::reg_msg_to_bus(const std::vector<std::string> &types) {
p_client__->send(msg_bus_reg_type_multi, sf_serialize_binary(types));
}
inline void sf_msg_bus_client::unreg_msg_to_bus(const std::string &type) {
p_client__->send(msg_bus_unreg_single, sf_serialize_binary(type));
}
inline void sf_msg_bus_client::unreg_msg_to_bus(const std::vector<std::string> &types) {
p_client__->send(msg_bus_unreg_multi, sf_serialize_binary(types));
}
inline bool sf_msg_bus_client::connect_to_server(const std::string &ip, unsigned short port) {
return p_client__->connect_to_server(ip,port);
}
inline void sf_msg_bus_client::send_msg(const std::string &type, const byte_array &data) {
sf_msg_bus_t msg;
msg.type = type;
msg.data = data;
auto send_data = sf_serialize_binary(msg);
p_client__->send(msg_bus_new_msg, send_data);
}
inline void sf_msg_bus_client::close() {
p_client__->close();
}
inline void sf_msg_bus_client::on_reg_data__(const sf_pkg_header_t &header, const byte_array &data) {
if(header.type == msg_bus_new_msg)
{
sf_msg_bus_t msg_data;
sf_deserialize_binary(data, msg_data, 0);
msg_come(msg_data.type, msg_data.data);
}
}
inline bool sf_msg_bus_client::get_local_addr(sf_addr_info_t &addr)
{
return p_client__->get_local_addr(addr);
}
bool sf_msg_bus_client::get_peer_addr(sf_addr_info_t &addr)
{
return p_client__->get_peer_addr(addr);
}
}