📄 hash.c
字号:
/*********************************************************************
* 模块名称:hash
* 说明:hash表处理模块
* 其它说明:
* 作者: 刘青山
* 时间 : 2004-09-26 7:13:30
*********************************************************************/
#include "natsvr.h"
#include "hash.h"
#include "list.h"
#include "thread_rwlock.h"
#define HASH_SIZE (1 << 16)
#define HASH_MASK (HASH_SIZE - 1)
static HASH m_hashUser[HASH_SIZE];
pthread_mutex_t g_mtGlobalUser ;
void LockGlobalUser()
{
pthread_mutex_lock(&g_mtGlobalUser);
}
void UnlockGlobalUser()
{
pthread_mutex_unlock(&g_mtGlobalUser);
}
static int hashfn_uin(DWORD uin)
{
uin ^= (uin >> 16);
uin ^= (uin >> 8);
return (uin & HASH_MASK);
}
static int hashfn_ip(DWORD ip, WORD port)
{
int h = (ip ^ port);
h ^= (h >> 16);
h ^= (h >> 8);
return (h & HASH_MASK);
}
static uint32 rand32()
{
return ((rand() << 15) | (rand() & 0x7fff));
}
void InitHash()
{
int i ;
pthread_mutex_init(&g_mtGlobalUser, NULL);
for(i = 0 ; i< HASH_SIZE; i++)
{
INIT_LIST_HEAD(&(m_hashUser[i].ipport)) ;
INIT_LIST_HEAD(&(m_hashUser[i].uin)) ;
}
}
LPP2P_USER GetHashUser(DWORD ip, WORD port)
{
struct list_head *pos;
struct list_head *head = &m_hashUser[hashfn_ip(ip, port)].ipport;
LPP2P_USER pUser = NULL;
LockGlobalUser();
LIST_FOR_EACH(pos, head)
{
pUser = LIST_ENTRY(pos,P2P_USER, ipport);
if ( pUser->dwNatIP == ip && pUser->wNatPort == port )
break;
pUser = NULL;
}
UnlockGlobalUser();
return pUser;
}
LPP2P_USER GetHashUserByID(DWORD uin)
{
struct list_head *pos;
struct list_head *head = &m_hashUser[hashfn_uin(uin)].uin ;
LPP2P_USER pUser = NULL;
LockGlobalUser();
LIST_FOR_EACH(pos, head)
{
pUser = LIST_ENTRY(pos,P2P_USER, uin);
if ( pUser->dwID == uin )
break;
pUser = NULL;
}
UnlockGlobalUser();
return pUser;
}
void PutHashUser(LPP2P_USER pUser,DWORD ip, WORD port)
{
int i = hashfn_ip(ip, port);
LockGlobalUser();
list_add(&pUser->ipport, &m_hashUser[i].ipport);
UnlockGlobalUser();
}
void PutHashUserByID(LPP2P_USER pUser,DWORD uin)
{
int i = hashfn_uin(uin);
LockGlobalUser();
list_add( &pUser->uin,&m_hashUser[i].uin );
UnlockGlobalUser();
}
void ClearHashUser(DWORD uin)
{
struct list_head *pos;
struct list_head *head = &m_hashUser[hashfn_uin(uin)].uin ;
LPP2P_USER pUser = NULL;
LockGlobalUser();
LIST_FOR_EACH(pos, head)
{
pUser = LIST_ENTRY(pos,P2P_USER, uin);
if ( pUser->dwID == uin )
{
list_del(&pUser->uin);
list_del(&pUser->ipport);
break;
}
}
UnlockGlobalUser();
return ;
}
void ClearHashByUser(LPP2P_USER pUser)
{
LockGlobalUser();
list_del(&pUser->ipport);
list_del(&pUser->uin);
UnlockGlobalUser();
/*
if (pUser->ipport.next && pUser->ipport.prev)
{
LockGlobalUser();
if (pUser->ipport.next && pUser->ipport.prev)
{
list_del(&pUser->ipport);
}
UnlockGlobalUser();
}
if (pUser->uin.next && pUser->uin.prev)
{
LockGlobalUser();
if (pUser->uin.next && pUser->uin.prev)
{
list_del(&pUser->uin);
}
UnlockGlobalUser();
}
*/
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -