Mod aaa-12 修改web server用户态hash表

RCA:
SOL:
修改人:chenling
检视人:
This commit is contained in:
ChenLing 2019-09-26 16:07:47 +08:00
parent a5ed8b0ad0
commit 5859a3d252
2 changed files with 46 additions and 38 deletions

View File

@ -12,11 +12,11 @@
static server* g_svr = NULL; static server* g_svr = NULL;
extern USER_AUTH_LIST g_user_auth_ret_table[]; extern USER_AUTH_LIST g_user_auth_ret_table[];
#if 0
/*链表全局变量 */ /*链表全局变量 */
struct hlist_head *hash; struct hlist_head *hash;
USER_INFO *pNode ; USER_INFO *pNode ;
/*计算hash值 */ /*计算hash值 */
struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip) struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip)
{ {
@ -25,21 +25,10 @@ struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip)
return &hash[val]; return &hash[val];
} }
/*初始化函数 */ /*初始化函数 */
int Init_hash() int Init_hash()
{ {
int i = 0; int i = 0;
//struct hlist_node *p = NULL, *n = NULL;
#if 0
USER_INFO uInfo[100];
for(i = 0; i < 100; i++) {
memset(&uInfo[i], 0, sizeof(USER_INFO));
uInfo[i].val = i;
}
#endif
/*创建hash头 */ /*创建hash头 */
hash = (struct hlist_head*)malloc(sizeof(*hash)*100); hash = (struct hlist_head*)malloc(sizeof(*hash)*100);
@ -53,17 +42,6 @@ int Init_hash()
for(i = 0; i < USERNUM_MAX; i++) for(i = 0; i < USERNUM_MAX; i++)
INIT_HLIST_HEAD(&hash[i]); INIT_HLIST_HEAD(&hash[i]);
#if 0
for(i = 0; i < 100; i++) {
hlist_add_head(&uInfo[i].hnode, &hash[0]);
}
hlist_for_each_safe(p, n, &hash[0]) {
USER_INFO* pV = hlist_entry(p, struct user_info, hnode);
log_error_write(g_svr, __FILE__, __LINE__, "dsd", i, ":", pV->val);
}
#endif
/*hsah桶普通节点分配内存 */ /*hsah桶普通节点分配内存 */
pNode = (struct user_info *)malloc(sizeof(struct user_info)); pNode = (struct user_info *)malloc(sizeof(struct user_info));
if (NULL == pNode) if (NULL == pNode)
@ -78,19 +56,44 @@ int Init_hash()
return 0; return 0;
} }
#endif
/*定义大小为HASH_SIZE的hashtable */
struct hlist_head hash[USERNUM_MAX];
/*获取hashtable位置索引 */
int get_uhash_index(uint32_t ip)
{
int val = ip % USERNUM_MAX;
return val;
}
/*初始化函数 */
void Init_hash()
{
int i;
for(i = 0; i < USERNUM_MAX; i++)
{
INIT_HLIST_HEAD(&hash[i]);
}
return;
}
/*通过IP查找在线用户信息*/ /*通过IP查找在线用户信息*/
USER_INFO *ufind_user(uint32_t user_ip) USER_INFO *ufind_user(uint32_t user_ip)
{ {
struct hlist_node *p = NULL, *n = NULL; struct hlist_node *p = NULL, *n = NULL;
USER_INFO *pNode;
log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); log_error_write(g_svr, __FILE__, __LINE__, "s", "test");
/* 这个实际上就是一个for循环从头到尾遍历链表。 /* 这个实际上就是一个for循环从头到尾遍历链表。
* posstruct hlist_node类型的一个指针 * posstruct hlist_node类型的一个指针
* nstruct hlist_node类型的一个指针 * nstruct hlist_node类型的一个指针
* headstruct hlist_head类型的一个指针hlist链表的头结点 * headstruct hlist_head类型的一个指针hlist链表的头结点
*/ */
hlist_for_each_safe(p, n, call_hash(hash, user_ip)) int index = get_uhash_index(user_ip);
hlist_for_each_safe(p, n, &hash[index])
{ {
log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); log_error_write(g_svr, __FILE__, __LINE__, "s", "test");
@ -115,16 +118,19 @@ USER_INFO *ufind_user(uint32_t user_ip)
USER_INFO *ufind_user_by_ippage(int page_num, int count, uint32_t user_ip) USER_INFO *ufind_user_by_ippage(int page_num, int count, uint32_t user_ip)
{ {
struct hlist_node *p = NULL, *n = NULL; struct hlist_node *p = NULL, *n = NULL;
USER_INFO *pNode;
int usernum = 0; int usernum = 0;
int num_begin; int num_begin;
int num_end; int num_end;
int index;
log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); log_error_write(g_svr, __FILE__, __LINE__, "s", "test");
/* 这个实际上就是一个for循环从头到尾遍历链表。 /* 这个实际上就是一个for循环从头到尾遍历链表。
* posstruct hlist_node类型的一个指针 * posstruct hlist_node类型的一个指针
* nstruct hlist_node类型的一个指针 * nstruct hlist_node类型的一个指针
* headstruct hlist_head类型的一个指针hlist链表的头结点 * headstruct hlist_head类型的一个指针hlist链表的头结点
*/ */
hlist_for_each_safe(p, n, call_hash(hash, user_ip)) index = get_uhash_index(user_ip);
hlist_for_each_safe(p, n, &hash[index])
{ {
log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); log_error_write(g_svr, __FILE__, __LINE__, "s", "test");
/* p表示struct hlist_node类型的一个地址。 /* p表示struct hlist_node类型的一个地址。
@ -158,11 +164,10 @@ USER_INFO *ufind_user_by_ippage(int page_num, int count, uint32_t user_ip)
int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t message_num, uint64_t byte_num, time_t online_time) int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t message_num, uint64_t byte_num, time_t online_time)
{ {
struct hlist_node *pos = NULL, *n = NULL; struct hlist_node *pos = NULL, *n = NULL;
pNode = NULL; USER_INFO *pNode = NULL;
struct hlist_head* pVal = call_hash(hash, user_ip); int index = get_uhash_index(user_ip);
//hlist_for_each_safe(p,n,call_hash(hash, user_ip)) /*查找ip是否存在hash表中 */ hlist_for_each_safe(pos, n, &hash[index]) /*查找ip是否存在hash表中 */
for(pos = pVal->first; pos && ({ n = pos->next; 1; }); pos = n)
{ {
pNode = hlist_entry(pos, struct user_info ,hnode); pNode = hlist_entry(pos, struct user_info ,hnode);
if((pNode != NULL) && (user_ip == pNode->auth_user.user_ip)) if((pNode != NULL) && (user_ip == pNode->auth_user.user_ip))
@ -189,7 +194,7 @@ int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t
memset(pNode,0,sizeof(struct user_info)); memset(pNode,0,sizeof(struct user_info));
INIT_HLIST_NODE(&pNode->hnode); INIT_HLIST_NODE(&pNode->hnode);
pNode->auth_user.user_ip = user_ip; pNode->auth_user.user_ip = user_ip;
hlist_add_head(&pNode->hnode, call_hash(hash, user_ip)); hlist_add_head(&pNode->hnode, &hash[index]);
log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); log_error_write(g_svr, __FILE__, __LINE__, "s", "test");
} }
@ -208,12 +213,12 @@ int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t
void udelete_user(int user_ip) void udelete_user(int user_ip)
{ {
unsigned short check_id; unsigned short check_id;
USER_INFO *result; USER_INFO *pNode;
/*查找要删除的用户IP和端口号*/ /*查找要删除的用户IP和端口号*/
result = ufind_user(user_ip); pNode = ufind_user(user_ip);
if(NULL != result) if(NULL != pNode)
{ {
/*查找用户ID确认ID是否存在 */ /*查找用户ID确认ID是否存在 */
check_id = g_user_auth_ret_table[pNode->auth_user.user_id].group_id; check_id = g_user_auth_ret_table[pNode->auth_user.user_id].group_id;
@ -232,6 +237,7 @@ void udelete_all()
{ {
struct hlist_node *p = NULL, *n = NULL ; struct hlist_node *p = NULL, *n = NULL ;
int i = 0; int i = 0;
USER_INFO *pNode;
for(i = 0; i < USERNUM_MAX; i++) for(i = 0; i < USERNUM_MAX; i++)
{ {
@ -250,6 +256,7 @@ int uprintf_users()
struct hlist_node *p = NULL, *n = NULL ; struct hlist_node *p = NULL, *n = NULL ;
int i = 0; int i = 0;
int online_num = 0; int online_num = 0;
USER_INFO *pNode;
log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); log_error_write(g_svr, __FILE__, __LINE__, "s", "test");
for(i = 0; i < USERNUM_MAX; i++) for(i = 0; i < USERNUM_MAX; i++)
@ -286,6 +293,7 @@ void ufind_user_by_name(int page_num, int count, char *user_name, cJSON **pJson)
struct hlist_node *p = NULL, *n = NULL ; struct hlist_node *p = NULL, *n = NULL ;
cJSON *pJsonsub; cJSON *pJsonsub;
cJSON *pJsonArry = *pJson; cJSON *pJsonArry = *pJson;
USER_INFO *pNode;
int usernum = 0; int usernum = 0;
int num_begin; int num_begin;

View File

@ -1,5 +1,5 @@
#ifndef K_HASHTABLE_H #ifndef U_HASHTABLE_H
#define K_HASHTABLE_H #define U_HASHTABLE_H
#include <stdint.h> #include <stdint.h>
#include "hlist.h" #include "hlist.h"
#include <cjson/cJSON.h> #include <cjson/cJSON.h>
@ -30,11 +30,11 @@ typedef struct user_info{
}USER_INFO; }USER_INFO;
/*计算hash值 */ /*获取hashtable位置索引 */
struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip); int get_uhash_index(uint32_t ip);
/*初始化函数 */ /*初始化函数 */
int Init_hash(); void Init_hash();
/*通过IP查找在线用户信息*/ /*通过IP查找在线用户信息*/
USER_INFO *ufind_user(uint32_t user_ip); USER_INFO *ufind_user(uint32_t user_ip);