common.h
6.71 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
#pragma once
#include <lib_include.h>
#include <lib_xmlparser.h>
#include <lib_util.h>
#include <lib_timer.h>
#include <lib_time.h>
#include <lib_random.h>
#include <lib_platform.h>
#include <lib_lock.h>
#include <lib_log.h>
#include <lib_file.h>
#include <lib_net/lib_tcp_client.h>
#include <lib_err_code.h>
#include <lib_proto/lib_proto.h>
#include <lib_proto/lib_msgbuf.h>
#include <bench_conf.h>
#include <service_if.h>
#include <service_mgr_if.h>
#include <google/protobuf/message.h>
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
// 检查配置文件
//#define __CHECK_CFG__
typedef uint64_t USER_ID;
typedef uint32_t USER_HEAD_ID;
typedef uint32_t PET_ID;
typedef uint32_t PET_ATTR;
typedef uint32_t PET_KEY;
typedef uint32_t EFF_ID;
typedef uint32_t SKILL_ID;
typedef uint32_t SKILL_TYPE;
typedef uint32_t BUF_ID;
typedef uint32_t PET_STAR;
typedef uint32_t PET_CNT;
typedef uint32_t PET_LV;
typedef uint32_t USER_HEAD_FRAME_ID;
typedef uint32_t CMD_ID;
typedef uint32_t PROTO_LEN;
typedef uint64_t MAIL_IDX;
typedef uint32_t MAIL_ID;
typedef uint32_t GAME_ID;
typedef uint32_t GAME_LEVEL;
typedef uint32_t GAME_SCORE;
typedef uint32_t TASK_ID;
typedef uint32_t STEP_ID;
typedef uint32_t EVENT_TYPE;
typedef uint64_t EVENT_ID;
typedef uint64_t EVENT_DATA;
typedef uint32_t EVENT_TIME;
typedef std::string EVENT_STR_DATA;
typedef std::string EVENT_BIN_DATA;
typedef uint32_t ITEM_ID;
typedef uint32_t ITEM_CNT;
typedef uint32_t ROOM_ID;
static const ITEM_CNT ITEM_CNT_MAX = UINT32_MAX;
//client
static const uint32_t MAXMESSAGE_LEN = 1048576;//1024*1024 = 1M
#ifdef WIN32
static const uint32_t SERVER_PACK_DEFAULT_SIZE = 8388608;//1024*1024*8 = 8Mdb
#else
static const uint32_t SERVER_PACK_DEFAULT_SIZE = 83886080;//1024*1024*80 = 80Mdb
#endif
static const uint32_t USER_EVENT_STR_DATA_LEN_MAX = 20480;
static const uint32_t USER_EVENT_BIN_DATA_LEN_MAX = 65535;
static const uint32_t NICK_LEN = 32;
static const uint32_t ACCOUNT_MAX_LEN = 64;
static const uint32_t EXCHANGE_CODE_MAX_LEN = 32;
static const uint32_t ORDER_ID_MAX_LEN = 32;
static const uint32_t AMOUNT_MAX_LEN = 32;
//密码最大长度
static const uint32_t PS_MAX_LEN = 64;
static const uint32_t CHAT_MSG_LEN_MAX = 256;
enum E_ITEM_ID
{
E_ITEM_ID_SILVER = 20002,//银币
E_ITEM_ID_GOLD = 20018,//金币
E_ITEM_ID_USER_EXP = 20116,//城主经验Id 20116
};
//自然时间获得的粮草总数
static const uint32_t FORAGE_TIME_MAX = 120;
//总的粮草总数
static const uint32_t FORAGE_ALL_MAX = 600;
//每多少秒增加一次体力
static const uint32_t FORAGE_ADD_SEC = 360;
static const uint32_t BTL_CMD_BEGIN = 0x00010801;
static const uint32_t BTL_CMD_END = 0x00010FFF;
enum E_USER_LOGIN_STATUS{
E_USER_LOGIN_STATUS_CONNECTED = 1,//已连接成功
E_USER_LOGIN_STATUS_LOGINING = 2,//验证登录中
// E_USER_LOGIN_STATUS_APPROVED = 3,//通过身份校验
E_USER_LOGIN_STATUS_CREATE_ROLE = 4,//创建角色
E_USER_LOGIN_STATUS_COMPLETE = 5,//登录完成
};
//玩家UID 最小起点,小于 MIN_UID 的都是AI
static const USER_ID MIN_UID = 10142857;
static const USER_ID MAX_UID = 73709551615;
//OLD
// 1 0000 00000000000
// 1 00000000000
//1 8446 7440 73709551615
//static const USER_ID OLD_USER_ID_PLATFORM = 1000000000000000;
//NEW
// 1 00 00000000000
// 1 00000000000
//1 844674 40 73709551615
static const USER_ID NEW_USER_ID_PLATFORM = 10000000000000;
inline uint32_t g_gen_platform(USER_ID uid){
// if (0 != (uint32_t)(uid/OLD_USER_ID_PLATFORM)){
// return (uint32_t)(uid/OLD_USER_ID_PLATFORM);
// }
return (uint32_t)(uid/NEW_USER_ID_PLATFORM);
}
#define GEN_UID(__platform__, __server_id__, __uid__) \
((USER_ID)(__platform__)*NEW_USER_ID_PLATFORM \
+ (USER_ID)(__server_id__) * 100000000000 \
+ (USER_ID)(__uid__))
//2000100010142859,10142862 =>2000100010142862
#define GEN_MAIL_RECV_UID(__send_uid__, __recv_uid__) \
((USER_ID)(__send_uid__)/100000000*100000000 \
+ (USER_ID)(__recv_uid__) % 100000000)
inline bool g_is_robot(USER_ID uid){
return uid < MIN_UID;
}
enum E_MAIL
{
MAIL_BODY_MAX_LEN = 1024,
MAIL_ATTACHMENT_MAX_LEN = 20480,
MAIL_ARG_MAX_LEN = 1024,
MAIL_BIN_DATA_MAX_LEN = 65535,
};
inline uint32_t get_valid_len(const char * str, uint32_t max_len){
if( strlen(str) > max_len ){
return max_len;
} else {
return (uint32_t)strlen(str);
}
}
struct uint_pair {
uint_pair() : key(0), val(0) {}
uint_pair(uint32_t p_key, uint32_t p_val) : key(p_key), val(p_val) {}
uint32_t key;
uint32_t val;
void init(){
this->key = 0;
this->val = 0;
}
};
//字符串分割成uint32,只能是uint类型字符串 格式必须是 1,2;3,4;5,6 可以接收空字符串
inline void str_split_uint(const std::string& source, std::vector<uint_pair>& vec_dest)
{
uint_pair u_pa;
char ctr_key[11] = {0};
int i = 0;
for(std::string::const_iterator it = source.begin(); it != source.end(); ++it) {
if (*it != ';') {
if (*it != ',') {
ctr_key[i++] = *it;
} else {
u_pa.key = atoi(ctr_key);
::memset(ctr_key, 0, 11);
i = 0;
}
} else {
u_pa.val = atoi(ctr_key);
vec_dest.push_back(u_pa);
::memset(ctr_key, 0, 11);
i = 0;
}
}
if (ctr_key[0] != 0) {
u_pa.val = atoi(ctr_key);
vec_dest.push_back(u_pa);
}
}
//每个vector中间用';'隔开,结尾不含; 如: 1,2;3,4;5,6
inline void uint_concat_str(const std::vector<uint_pair>& vec_source, std::string& str_dest)
{
if (vec_source.empty()) {
return;
}
for (auto it = vec_source.begin(); it != vec_source.end(); ++it) {
char ctr_dest[24] = {0};
::sprintf(ctr_dest, "%u,%u;", it->key, it->val);
str_dest += ctr_dest;
}
str_dest.pop_back();
}
// @brief: 计算今天是周几(周一:1 ~ 周日:7)
inline uint32_t time_week(time_t now_time)
{
#ifdef WIN32
struct tm* ptm = ::localtime(&now_time);
return 0 == ptm->tm_wday ? 7 : ptm->tm_wday;
#else
struct tm t_m;
::localtime_r(&now_time, &t_m);
return 0 == t_m.tm_wday ? 7 : t_m.tm_wday;
#endif
}
#define DEF_MSG_HANDLE_FUN(__HANDLE_CLASS__, __NAME__) \
class __HANDLE_CLASS__;\
typedef int (__HANDLE_CLASS__::* __NAME__##_MSG_HANDLE_FUN)( __NAME__##_MSG_HANDLE_FUN_PAR);\
struct MSG_HANDLE_##__NAME__##_T{\
google::protobuf::Message* prototype;\
__NAME__##_MSG_HANDLE_FUN func;\
bool is_callback;\
MSG_HANDLE_##__NAME__##_T(){\
this->prototype = NULL;\
this->func = NULL;\
this->is_callback = true;\
}\
MSG_HANDLE_##__NAME__##_T( google::protobuf::Message* proto, __NAME__##_MSG_HANDLE_FUN fun, bool is_callback = true ){\
this->prototype = proto;\
this->func = fun;\
this->is_callback = is_callback;\
}\
};\
typedef std::map<CMD_ID, MSG_HANDLE_##__NAME__##_T> __NAME__##_MSG_MAP_T;
//#define NEW_RECONNECT
//#define TEST_FIRST_BLOOD //todo