forked from dearlulu/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_router.cpp
189 lines (164 loc) · 5.39 KB
/
socket_router.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
** repository: https://github.com/trumanzhao/luna
** trumanzhao, 2017-02-11, [email protected]
*/
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <algorithm>
#include "tools.h"
#include "var_int.h"
#include "socket_router.h"
uint32_t get_group_idx(uint32_t service_id) { return (service_id >> 16) & 0xff; }
void socket_router::update(uint32_t service_id, uint32_t token)
{
uint32_t group_idx = get_group_idx(service_id);
auto& group = m_groups[group_idx];
auto& nodes = group.nodes;
auto it = std::lower_bound(nodes.begin(), nodes.end(), service_id, [](auto& node, uint32_t id) { return node.id < id; });
if (it != nodes.end() && it->id == service_id)
{
it->token = token;
}
else
{
service_node node;
node.id = service_id;
node.token = token;
nodes.insert(it, node);
}
}
void socket_router::set_master(uint8_t group_idx, uint32_t token)
{
auto& group = m_groups[group_idx];
group.master = token;
}
void socket_router::erase(uint32_t service_id)
{
uint32_t group_idx = get_group_idx(service_id);
auto& group = m_groups[group_idx];
auto& nodes = group.nodes;
auto it = std::lower_bound(nodes.begin(), nodes.end(), service_id, [](auto& node, uint32_t id) { return node.id < id; });
if (it != nodes.end() && it->id == service_id)
{
nodes.erase(it);
}
}
void socket_router::forward_target(char* data, size_t data_len)
{
uint64_t target_id64 = 0;
size_t len = decode_u64(&target_id64, (BYTE*)data, data_len);
if (len == 0)
return;
data += len;
data_len -= len;
uint32_t target_id = (uint32_t)target_id64;
uint32_t group_idx = get_group_idx(target_id);
auto& group = m_groups[group_idx];
auto& nodes = group.nodes;
auto it = std::lower_bound(nodes.begin(), nodes.end(), target_id, [](auto& node, uint32_t id) { return node.id < id; });
if (it == nodes.end() || it->id != target_id)
return;
BYTE msg_id_data[MAX_VARINT_SIZE];
size_t msg_id_len = encode_u64(msg_id_data, sizeof(msg_id_data), (char)msg_id::remote_call);
sendv_item items[] = {{msg_id_data, msg_id_len}, {data, data_len}};
m_mgr->sendv(it->token, items, _countof(items));
}
void socket_router::forward_master(char* data, size_t data_len)
{
uint64_t group_idx = 0;
size_t len = decode_u64(&group_idx, (BYTE*)data, data_len);
if (len == 0 || group_idx >= m_groups.size())
return;
data += len;
data_len -= len;
auto token = m_groups[group_idx].master;
if (token == 0)
return;
BYTE msg_id_data[MAX_VARINT_SIZE];
size_t msg_id_len = encode_u64(msg_id_data, sizeof(msg_id_data), (char)msg_id::remote_call);
sendv_item items[] = {{msg_id_data, msg_id_len}, {data, data_len}};
m_mgr->sendv(token, items, _countof(items));
}
void socket_router::forward_random(char* data, size_t data_len)
{
uint64_t group_idx = 0;
size_t len = decode_u64(&group_idx, (BYTE*)data, data_len);
if (len == 0 || group_idx >= m_groups.size())
return;
data += len;
data_len -= len;
auto& group = m_groups[group_idx];
auto& nodes = group.nodes;
int count = (int)nodes.size();
if (count == 0)
return;
BYTE msg_id_data[MAX_VARINT_SIZE];
size_t msg_id_len = encode_u64(msg_id_data, sizeof(msg_id_data), (char)msg_id::remote_call);
sendv_item items[] = {{msg_id_data, msg_id_len}, {data, data_len}};
int idx = rand() % count;
for (int i = 0; i < count; i++)
{
auto& target = nodes[(idx + i) % count];
if (target.token != 0)
{
m_mgr->sendv(target.token, items, _countof(items));
break;
}
}
}
void socket_router::forward_broadcast(char* data, size_t data_len)
{
uint64_t group_idx = 0;
size_t len = decode_u64(&group_idx, (BYTE*)data, data_len);
if (len == 0 || group_idx >= m_groups.size())
return;
data += len;
data_len -= len;
BYTE msg_id_data[MAX_VARINT_SIZE];
size_t msg_id_len = encode_u64(msg_id_data, sizeof(msg_id_data), (char)msg_id::remote_call);
sendv_item items[] = {{msg_id_data, msg_id_len}, {data, data_len}};
auto& group = m_groups[group_idx];
auto& nodes = group.nodes;
int count = (int)nodes.size();
for (auto& target : nodes)
{
if (target.token != 0)
{
m_mgr->sendv(target.token, items, _countof(items));
}
}
}
void socket_router::forward_hash(char* data, size_t data_len)
{
uint64_t group_idx = 0;
size_t len = decode_u64(&group_idx, (BYTE*)data, data_len);
if (len == 0 || group_idx >= m_groups.size())
return;
data += len;
data_len -= len;
uint64_t hash = 0;
len = decode_u64(&hash, (BYTE*)data, data_len);
if (len == 0)
return;
data += len;
data_len -= len;
auto& group = m_groups[group_idx];
auto& nodes = group.nodes;
int count = (int)nodes.size();
if (count == 0)
return;
BYTE msg_id_data[MAX_VARINT_SIZE];
size_t msg_id_len = encode_u64(msg_id_data, sizeof(msg_id_data), (char)msg_id::remote_call);
sendv_item items[] = {{msg_id_data, msg_id_len}, {data, data_len}};
int idx = hash % count;
for (int i = 0; i < count; i++)
{
auto& target = nodes[(idx + i) % count];
if (target.token != 0)
{
m_mgr->sendv(target.token, items, _countof(items));
break;
}
}
}