Repository navigation
Expand file tree
/
Copy pathmulti_svr.cpp
More file actions
339 lines (289 loc) · 11.2 KB
/
Copy pathmulti_svr.cpp
File metadata and controls
339 lines (289 loc) · 11.2 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* =====================================================================================
*
* Filename: multi_svr.cpp
*
* Description:
*
* Version: 1.0
* Created: 2011年12月16日 11时10分48秒
* Revision: none
* Compiler: gcc
*
* Author: henry (韩林), henry@taomee.com
* Company: TaoMee.Inc, ShangHai
*
* =====================================================================================
*/
#include <assert.h>
#include "data.h"
#include "message.h"
#include "multi_svr.h"
#include "function.h"
#include "cli_proto.h"
void handle_multi_svr_return(svr_msg_header_t* p_pkg, int len)
{
assert(p_pkg != NULL);
uint16_t multi_svr_msg = p_pkg->msg_type;
#ifdef DEBUG
KDEBUG_LOG(p_pkg->user_id, "recv msg %u from multi_svr.", multi_svr_msg);
#endif
fdsession_t *fdsess = g_user_mng.get_fd(p_pkg->user_id);;
if(fdsess == NULL)
{
KCRIT_LOG(p_pkg->user_id, "User is not online.");
return;
}
usr_info_t *p_user = g_user_mng.get_user(fdsess->fd);
if(p_user == NULL)
{
KCRIT_LOG(p_pkg->user_id, "User is not in Cache.");
return;
}
if((int)p_pkg->len != len)
{
KCRIT_LOG(p_pkg->user_id, "msg len from multi_svr is not correct, p_pkg->len:%u, len:%d", p_pkg->len, len);
return;
}
if(p_pkg->seq_id != 0)
{//seq_id == 0的消息为服务器主动推送过来的消息
uint32_t counter = p_pkg->seq_id & 0xFFFF;
if(counter != p_user->counter)
{
KCRIT_LOG(p_pkg->user_id, "It should not happen this case.");
return;
}
REMOVE_TIMERS(p_user);
}
g_errno = ERR_NO_ERR;
bool clear_waitcmd = true;
switch(multi_svr_msg)
{
case svr_msg_online_multi_enter:
process_enter_two_puzzle_return(p_user, p_pkg, len);
break;
case svr_msg_multi_online_match:
//服务器主动推送的协议
process_two_puzzle_match_notice(p_user, p_pkg, len);
clear_waitcmd = false;
return;
case svr_msg_online_multi_start:
process_start_two_puzzle_return(p_user, p_pkg, len);
break;
case svr_msg_multi_online_start:
//服务器主动推送的协议
process_two_puzzle_start_notice(p_user, p_pkg, len);
clear_waitcmd = false;
return;
case svr_msg_online_multi_answer:
process_answer_two_puzzle_return(p_user, p_pkg, len);
break;
case svr_msg_multi_online_answer:
//服务器主动推送的协议
process_two_puzzle_answer_notice(p_user, p_pkg, len);
clear_waitcmd = false;
return;
case svr_msg_multi_online_result:
//服务器主动推送的协议
process_two_puzzle_result_notice(p_user, p_pkg, len);
clear_waitcmd = false;
return;
default:
KCRIT_LOG(p_pkg->user_id, "recv unkonwn [msg:%u] from multi_svr.", p_pkg->msg_type);
break;
}
if(g_errno != ERR_NO_ERR)
{
send_header_to_user(p_user, SYSTEM_ERR_NUM, g_errno);
}
return;
}
int process_enter_two_puzzle_return(usr_info_t *p_user, svr_msg_header_t* p_pkg, int len)
{
assert(p_user != NULL && p_pkg != NULL);
if(p_pkg->result != 0)
{
KCRIT_LOG(p_user->uid, "enter_two_puzzle failed(errno:%u).", p_pkg->result);
g_errno = p_pkg->result;
return 0;
}
uint8_t is_found = *(uint8_t*)p_pkg->body;
pack_as_pkg_header(p_user->uid, p_user->waitcmd, 0, ERR_NO_ERR);
KINFO_LOG(p_user->uid, "Get matching info :isfound:%u", is_found);
g_send_msg.pack(is_found);
if(is_found != 0)
{
const matching_opponent_t *p_match_opp = (const matching_opponent_t*)(p_pkg->body + sizeof(uint8_t));
g_send_msg.pack(p_match_opp->opp_info.uid);
g_send_msg.pack(p_match_opp->opp_info.uname, 16);
g_send_msg.pack(p_match_opp->opp_info.is_vip);
g_send_msg.pack(p_match_opp->opp_info.mon_id);
g_send_msg.pack(p_match_opp->opp_info.mon_main_color);
g_send_msg.pack(p_match_opp->opp_info.mon_exp_color);
g_send_msg.pack(p_match_opp->opp_info.mon_eye_color);
g_send_msg.pack(p_match_opp->puz_com.compete_init_time);
g_send_msg.pack(p_match_opp->puz_com.question_num);
KINFO_LOG(p_user->uid, "1 question_num:%u", p_match_opp->puz_com.question_num);
for(int i = 0; i < p_match_opp->puz_com.question_num; i++)
{
// KINFO_LOG(p_user->uid, "type:%u, id:%u", p_match_opp->puz_com.quests[i].quest_type, p_match_opp->puz_com.quests[i].quest_id);
g_send_msg.pack(p_match_opp->puz_com.quests[i].quest_type);
g_send_msg.pack(p_match_opp->puz_com.quests[i].quest_id);
}
}
g_send_msg.end();
send_data_to_user(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len());
return 0;
}
int process_two_puzzle_match_notice(usr_info_t *p_user, svr_msg_header_t* p_pkg, int len)
{
assert(p_user != NULL && p_pkg != NULL);
if(p_pkg->result != 0)
{
KCRIT_LOG(p_user->uid, "notice match puzzle info failed(errno:%u).", p_pkg->result);
g_errno = p_pkg->result;
return 0;
}
uint8_t is_found = *(uint8_t*)p_pkg->body;
pack_as_pkg_header(p_user->uid, as_msg_match_two_puzzle, 0, ERR_NO_ERR);
KINFO_LOG(p_user->uid, "notice matching info :is_found:%u", is_found);
g_send_msg.pack(is_found);
if(is_found != 0)
{
const matching_opponent_t *p_match_opp = (const matching_opponent_t*)(p_pkg->body + sizeof(uint8_t));
g_send_msg.pack(p_match_opp->opp_info.uid);
g_send_msg.pack(p_match_opp->opp_info.uname, 16);
g_send_msg.pack(p_match_opp->opp_info.is_vip);
g_send_msg.pack(p_match_opp->opp_info.mon_id);
g_send_msg.pack(p_match_opp->opp_info.mon_main_color);
g_send_msg.pack(p_match_opp->opp_info.mon_exp_color);
g_send_msg.pack(p_match_opp->opp_info.mon_eye_color);
g_send_msg.pack(p_match_opp->puz_com.compete_init_time);
g_send_msg.pack(p_match_opp->puz_com.question_num);
KINFO_LOG(p_user->uid, "2 question_num:%u", p_match_opp->puz_com.question_num);
for(int i = 0; i < p_match_opp->puz_com.question_num; i++)
{
//KINFO_LOG(p_user->uid, "type:%u, id:%u", p_match_opp->puz_com.quests[i].quest_type, p_match_opp->puz_com.quests[i].quest_id);
g_send_msg.pack(p_match_opp->puz_com.quests[i].quest_type);
g_send_msg.pack(p_match_opp->puz_com.quests[i].quest_id);
}
}
g_send_msg.end();
send_data_to_user(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len(), false);
return 0;
}
int process_start_two_puzzle_return(usr_info_t *p_user, svr_msg_header_t* p_pkg, int len)
{
assert(p_user != NULL && p_pkg != NULL);
if(p_pkg->result != 0)
{
KCRIT_LOG(p_user->uid, "start_two_puzzle info failed(errno:%u).", p_pkg->result);
g_errno = p_pkg->result;
return 0;
}
pack_as_pkg_header(p_user->uid, p_user->waitcmd, 0, ERR_NO_ERR);
uint8_t status = *(uint8_t*)p_pkg->body;
g_send_msg.pack(status);
if(status == 0)
{//对方已离开
KINFO_LOG(p_user->uid, "Opponent user has been out.");
}
else
{
KINFO_LOG(p_user->uid, "Opponent user has not been out.status = %u", status);
}
g_send_msg.end();
send_data_to_user(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len());
return 0;
}
int process_two_puzzle_start_notice(usr_info_t *p_user, svr_msg_header_t *p_pkg, int len)
{
assert(p_user != NULL && p_pkg != NULL);
if(p_pkg->result != 0)
{
KCRIT_LOG(p_user->uid, "notice start_two_puzzle info failed(errno:%u).", p_pkg->result);
g_errno = p_pkg->result;
return 0;
}
pack_as_pkg_header(p_user->uid, as_msg_begin_two_puzzle, 0, ERR_NO_ERR);
uint8_t status = *(uint8_t*)p_pkg->body;
g_send_msg.pack(status);
g_send_msg.end();
send_data_to_user(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len(), false);
return 0;
}
int process_answer_two_puzzle_return(usr_info_t *p_user, svr_msg_header_t *p_pkg, int len)
{
assert(p_user != NULL && p_pkg != NULL);
if(p_pkg->result != 0)
{
KCRIT_LOG(p_user->uid, "answer two puzzle return(errno:%u).", p_pkg->result);
g_errno = p_pkg->result;
return 0;
}
uint8_t status = *(uint8_t*)p_pkg->body;
if(status != both_finished)
{//比赛未结束
KINFO_LOG(p_user->uid, "two_puzzle not end, status=%u", status);
pack_as_pkg_header(p_user->uid, p_user->waitcmd, 0, ERR_NO_ERR);
g_send_msg.pack(status);
g_send_msg.end();
send_data_to_user(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len());
}
else
{//比赛结束
two_puzzle_result_t *p_result = (two_puzzle_result_t*)(p_pkg->body + sizeof(uint8_t));
pack_as_pkg_header(p_user->uid, multi_puzzle_add_coin, 0, ERR_NO_ERR);
g_send_msg.pack(p_result->self_score);
g_send_msg.pack(p_result->opp_score);
g_send_msg.pack(p_result->result);
g_send_msg.pack(p_result->rewards_coins);
g_send_msg.pack(p_result->is_restrict);
g_send_msg.pack((uint16_t)p_user->waitcmd);
g_send_msg.end();
KINFO_LOG(p_user->uid, "cache a pkg, waitcmd:%u", p_user->waitcmd);
cache_a_pkg(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len(), true);
}
return 0;
}
int process_two_puzzle_answer_notice(usr_info_t *p_user, svr_msg_header_t *p_pkg, int len)
{
assert(p_user != NULL && p_pkg != NULL);
if(p_pkg->result != 0)
{
KCRIT_LOG(p_user->uid, "two puzzle answer notice(errno:%u).", p_pkg->result);
g_errno = p_pkg->result;
return 0;
}
pack_as_pkg_header(p_user->uid, as_msg_opp_answer_two_puzzle, 0, ERR_NO_ERR);
opp_answer_info_t *p_answ = (opp_answer_info_t*)p_pkg->body;
g_send_msg.pack(p_answ->opp_cur_score);
g_send_msg.pack(p_answ->opp_remain_time);
g_send_msg.pack(p_answ->opp_is_end);
g_send_msg.end();
KINFO_LOG(p_user->uid, "opp_score:%u remain_time:%u is_end:%u", p_answ->opp_cur_score, p_answ->opp_remain_time, p_answ->opp_is_end);
send_data_to_user(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len(), false);
return 0;
}
int process_two_puzzle_result_notice(usr_info_t *p_user, svr_msg_header_t *p_pkg, int len)
{
assert(p_user != NULL && p_pkg != NULL);
if(p_pkg->result != 0)
{
KCRIT_LOG(p_user->uid, "notice two_puzzle result info failed(errno:%u).", p_pkg->result);
g_errno = p_pkg->result;
return 0;
}
two_puzzle_result_t *p_result = (two_puzzle_result_t*)(p_pkg->body);
pack_as_pkg_header(p_user->uid, multi_puzzle_add_coin, 0, ERR_NO_ERR);
g_send_msg.pack(p_result->self_score);
g_send_msg.pack(p_result->opp_score);
g_send_msg.pack(p_result->result);
g_send_msg.pack(p_result->rewards_coins);
g_send_msg.pack(p_result->is_restrict);
g_send_msg.pack((uint16_t)as_msg_two_puzzle_result);
g_send_msg.end();
KINFO_LOG(p_user->uid, "opp end cache a pkg, waitcmd:%u", p_user->waitcmd);
cache_a_pkg(p_user, g_send_msg.get_msg(), g_send_msg.get_msg_len());
return 0;
}