sessionhash.h
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:2k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   This program is free software; you can redistribute it and/or modify  *
  4.  *   it under the terms of the GNU General Public License as published by  *
  5.  *   the Free Software Foundation; either version 2 of the License, or     *
  6.  *   (at your option) any later version.                                   *
  7.  *                                                                         *
  8.  *   copyright            : (C) 2002 by Zhang Yong                         *
  9.  *   email                : z-yong163@163.com                              *
  10.  ***************************************************************************/
  11. #ifndef _SESSION_HASH_H_
  12. #define _SESSION_HASH_H_
  13. #include "udpsession.h"
  14. #define ALIVE_HASH_SIZE (1024 * 1024)
  15. #define DEAD_HASH_SIZE 128
  16. class SessionHash {
  17. public:
  18. static UdpSession *getAlive(uint32 uin);
  19. static UdpSession *getDead(uint32 ip, uint16 port);
  20. static void addAlive(UdpSession *p) {
  21. alive[hashfn(p->uin)].addHead(&p->listItem);
  22. }
  23. static void addDead(UdpSession *p) {
  24. dead[hashfn(p->ip, p->port)].add(&p->listItem);
  25. }
  26. static int random(uint32 results[], int n);
  27. private:
  28. static int hashfn(uint32 uin) {
  29. uin ^= (uin >> 16);
  30. uin ^= (uin >> 8);
  31. return (uin & (ALIVE_HASH_SIZE - 1));
  32. }
  33. static int hashfn(uint32 ip, uint16 port) {
  34. int h = (ip ^ port);
  35. h ^= (h >> 16);
  36. h ^= (h >> 8);
  37. return (h & (DEAD_HASH_SIZE - 1));
  38. }
  39. static IcqList alive[ALIVE_HASH_SIZE];
  40. static IcqList dead[DEAD_HASH_SIZE];
  41. };
  42. #endif