sessionhash.cpp
上传用户: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. #include <stdlib.h>
  12. #include "udpsession.h"
  13. #include "sessionhash.h"
  14. IcqList SessionHash::alive[ALIVE_HASH_SIZE];
  15. IcqList SessionHash::dead[DEAD_HASH_SIZE];
  16. UdpSession *SessionHash::getAlive(uint32 uin)
  17. {
  18. UdpSession *p;
  19. IcqListItem *pos;
  20. IcqListItem *head = &alive[hashfn(uin)].head;
  21. LIST_FOR_EACH(pos, head) {
  22. p = LIST_ENTRY(pos, UdpSession, listItem);
  23. if (p->uin == uin)
  24. return p;
  25. }
  26. return NULL;
  27. }
  28. UdpSession *SessionHash::getDead(uint32 ip, uint16 port)
  29. {
  30. UdpSession *p;
  31. IcqListItem *pos;
  32. IcqListItem *head = &dead[hashfn(ip, port)].head;
  33. LIST_FOR_EACH(pos, head) {
  34. p = LIST_ENTRY(pos, UdpSession, listItem);
  35. if (p->ip == ip && p->port == port)
  36. return p;
  37. }
  38. return NULL;
  39. }
  40. int SessionHash::random(uint32 results[], int n)
  41. {
  42. int start = rand() % ALIVE_HASH_SIZE;
  43. int num = 0;
  44. for (int i = start + 1; num < n && i != start; i++) {
  45. if (i >= ALIVE_HASH_SIZE)
  46. i = 0;
  47. IcqListItem *pos, *head = &alive[i].head;
  48. LIST_FOR_EACH(pos, head) {
  49. UdpSession *p = LIST_ENTRY(pos, UdpSession, listItem);
  50. results[num++] = p->uin;
  51. if (num >= n)
  52. break;
  53. }
  54. }
  55. return num;
  56. }