-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusermgr.h
More file actions
157 lines (134 loc) · 3.25 KB
/
usermgr.h
File metadata and controls
157 lines (134 loc) · 3.25 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
#ifndef __GAME_USER_MGR_H__
#define __GAME_USER_MGR_H__
/*
统一的gameuser 管理入口,线程安全
*/
#include "user.h"
#include "base/lock_common.h"
#include "nskernel/connection.h"
#include "async_log.h"
struct GameUserInfo
{
int userid; //用户的唯一ID
GameUser* pGameuser;
Connection* pCon; //当前的连接
int fd;
time_t timestamp; //建立的时间戳,用于超时检测
bool bVerify;//玩家对象是否有效
std::string session;
int lineNum; //分线号
int curCjId; //当前场景ID
int m_nIndex;//-1:玩家对象未使用,[0,m_nMaxUserNum-1]:玩家对象正在使用。该值为自己在玩家对象池的位置,用于回收该玩家对象。
GameUserInfo()
{
userid = -1;
pCon = NULL;
timestamp = time(NULL);
pGameuser = NULL;
bVerify = false;
lineNum = -1;
curCjId = -1;
m_nIndex = -1;
fd = -1;
}
GameUserInfo& copy(const GameUserInfo& in)
{
if (&in == this)
{
return *this;
}
userid = in.userid;
pCon = in.pCon;
timestamp = in.timestamp;
bVerify = in.bVerify;
lineNum = in.lineNum;
curCjId = in.curCjId;
fd = in.fd;
return *this;
}
};
typedef std::map<int,GameUserInfo*> Uid2InfoMap;
typedef Uid2InfoMap GameUserMap ;
class UserData
{
public :
GameUserInfo* getUnUsedUser() //throw (std::string&)
{
DORW_WRLOCK(m_lockInfo);
if (m_lstUnUsedIndex.size() == 0)
{
DORW_UNLOCK(m_lockInfo);
return NULL;
}
int nIndex = m_lstUnUsedIndex.front();
m_lstUnUsedIndex.pop_front();
(*(m_pUserContainer+nIndex))->m_nIndex = nIndex;
DORW_UNLOCK(m_lockInfo);
return *(m_pUserContainer+nIndex);
}
void setUserUsed(int index)//throw (std::string& )
{
assert(index < m_nMaxUserNum);
if (index >= m_nMaxUserNum)
{
return ;
}
DORW_WRLOCK(m_lockInfo);
(*(m_pUserContainer+index))->m_nIndex = index;
DORW_UNLOCK(m_lockInfo);
}
void setUserUnUsed(int index)//throw (std::string& )
{
assert(index < m_nMaxUserNum);
if (index >= m_nMaxUserNum)
{
return ;
}
DORW_WRLOCK(m_lockInfo);
(*(m_pUserContainer+index))->m_nIndex = -1;
m_lstUnUsedIndex.push_back(index);
DORW_UNLOCK(m_lockInfo);
}
UserData()
{
DORW_LOCK_INIT(m_lockInfo);
}
public :
GameUserInfo** m_pUserContainer; //user info
std::list<int> m_lstUnUsedIndex; //unuser index
RWLOCK m_lockInfo;
public :
int m_nMaxUserNum; //max user info
};
class SProxy;
class UserMgr
{
public:
UserMgr();
~UserMgr();
GameUserInfo* newGameUser(int iUID);
void delGameUser(GameUserInfo* del_user);
GameUserInfo* linkGameUser(GameUserInfo* pTmp);
void init(SProxy* proxy,int max);
void deleteGameUser_NoLock(int iUID, bool real_del);
//判断game user 是否存在
bool isGameUserExist(int iUID);
//根据uid判断用户是否在线
bool isOnlineByUid(int iUID);
GameUserInfo* getUserInfo(int iUID);
GameUserInfo* getUserInfoByUid(int uid);
Connection* getValidConn(void);
void delUserInfoByUid(int uid);
int size(){return m_mapUid2Info.size();};
Uid2InfoMap getUidMap(){return m_mapUid2Info;};
private:
LOCK m_lockGameuser;
Uid2InfoMap m_mapUid2Info;
AsyncSysLogger *m_pLogger;
//base::Logger* m_pLogger;
SProxy* m_pProxy;
private:
//玩家对象池
UserData m_userData;
};
#endif