网络编程之你真了解select模型??

本文探讨了Select模型在网络编程中的局限性及其解决方案。通过分析libevent源码,介绍了一种利用动态数组来突破传统fd_set限制的方法,实现了高效处理大量套接字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只要接触过c/c++网路编程人都可能会知道select io 模式,网络书籍都说 fd_set {int num; SOCKET arr[64]} 有所限制,因为数组的长度只有64,那么超过64你就不能放,要么你就是用多线程分别实用select.。
一些书籍通过改定义宏 使数组的长度变长,但也不实用,不能动态的变化,我总不能定一个非常的长的长度,毕竟是在栈上。
   我就在想那么select完全只能在客户端使用,而且套接字还不能超过64。那这不就是一个软肋吗??一直对这个有一个迷惑。。。。。

   后来,自己看到了libevent的源代码发现他也用的是select。。看别人说这个库,轻轻松松处理上万个套接字,我就在想select不是有限制吗??他是怎么做到。。。。。。
看了源代码,我明白了。他只是用对上存放SOCKET的句柄。。我们看一下他的新定义结构体。

struct win_fd_set {
        u_int fd_count;
        SOCKET fd_array[1];
};

这个就是新定义结构体,跟原来稍微有点变化只是把64改为1,有些同学可能见多很多这样的写法,这种写法我也在一些项目使用了。这种写法可以fd_array动态变化。
win_fd_set * Set = (win_fd_set*)malloc(sizeof(win_fd_set) + sizoef(SCOEKT) * 10);
Set->fd_array 可以放11 个 SOCKET,因为我的内存大小足够放11个SOCKET。
请记住内存是没有数据格式,只要足够大小,随便你怎么放。数据格式只是方便我们管理和处理数据而已。
这样就解决64个大小限制。。我其实一直很好奇为什么中国书籍都是一样的,libevent已经出来好久了,但也没有看到有人说这一点,可能是高手们都不屑。
现在渐渐喜欢看开源的代码,不喜欢看书籍了,喜欢在代码中学习他们是怎么组织一个好项目。
有时候感慨:高手用c 写着漂亮的c++代码,而我等菜鸟却用c++ 写丑陋的c代码。。

我顺便把他整个定义结构和函数给大家贴出来,免大家还要自己去下libevent,不过推荐没有看过libevent同学,可以稍微看一下。
  1. volatile double SIGFPE_REQ = 0.0f;

  2. struct idx_info {
  3.         int read_pos_plus1;
  4.         int write_pos_plus1;
  5. };

  6. struct win32op {
  7.         unsigned num_fds_in_fd_sets;
  8.         int resize_out_sets;
  9.         struct win_fd_set *readset_in;
  10.         struct win_fd_set *writeset_in;
  11.         struct win_fd_set *readset_out;
  12.         struct win_fd_set *writeset_out;
  13.         struct win_fd_set *exset_out;
  14.         unsigned signals_are_broken : 1;
  15. };

  16. static void *win32_init(struct event_base *);
  17. static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *_idx);
  18. static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *_idx);
  19. static int win32_dispatch(struct event_base *base, struct timeval *);
  20. static void win32_dealloc(struct event_base *);

  21. struct eventop win32ops = {
  22.         "win32",
  23.         win32_init,
  24.         win32_add,
  25.         win32_del,
  26.         win32_dispatch,
  27.         win32_dealloc,
  28.         0, /* doesn't need reinit */
  29.         0, /* No features supported. */
  30.         sizeof(struct idx_info),
  31. };

  32. #define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET)))

  33. static int
  34. grow_fd_sets(struct win32op *op, unsigned new_num_fds)
  35. {
  36.         size_t size;

  37.         EVUTIL_ASSERT(new_num_fds >= op->readset_in->fd_count &&
  38.                new_num_fds >= op->writeset_in->fd_count);
  39.         EVUTIL_ASSERT(new_num_fds >= 1);

  40.         size = FD_SET_ALLOC_SIZE(new_num_fds);
  41.         if (!(op->readset_in = mm_realloc(op->readset_in, size)))
  42.                 return (-1);
  43.         if (!(op->writeset_in = mm_realloc(op->writeset_in, size)))
  44.                 return (-1);
  45.         op->resize_out_sets = 1;
  46.         op->num_fds_in_fd_sets = new_num_fds;
  47.         return (0);
  48. }

  49. static int
  50. do_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read)
  51. {
  52.         struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
  53.         if (read) {
  54.                 if (ent->read_pos_plus1 > 0)
  55.                         return (0);
  56.         } else {
  57.                 if (ent->write_pos_plus1 > 0)
  58.                         return (0);
  59.         }
  60.         if (set->fd_count == op->num_fds_in_fd_sets) {
  61.                 if (grow_fd_sets(op, op->num_fds_in_fd_sets*2))
  62.                         return (-1);
  63.                 /* set pointer will have changed and needs reiniting! */
  64.                 set = read ? op->readset_in : op->writeset_in;
  65.         }
  66.         set->fd_array[set->fd_count] = s;
  67.         if (read)
  68.                 ent->read_pos_plus1 = set->fd_count+1;
  69.         else
  70.                 ent->write_pos_plus1 = set->fd_count+1;
  71.         return (set->fd_count++);
  72. }

  73. static int
  74. do_fd_clear(struct event_base *base,
  75.                         struct win32op *op, struct idx_info *ent, int read)
  76. {
  77.         int i;
  78.         struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
  79.         if (read) {
  80.                 i = ent->read_pos_plus1 - 1;
  81.                 ent->read_pos_plus1 = 0;
  82.         } else {
  83.                 i = ent->write_pos_plus1 - 1;
  84.                 ent->write_pos_plus1 = 0;
  85.         }
  86.         if (i < 0)
  87.                 return (0);
  88.         if (--set->fd_count != (unsigned)i) {
  89.                 struct idx_info *ent2;
  90.                 SOCKET s2;
  91.                 s2 = set->fd_array[i] = set->fd_array[set->fd_count];

  92.                 ent2 = evmap_io_get_fdinfo(&base->io, s2);

  93.                 if (!ent2) /* This indicates a bug. */
  94.                         return (0);
  95.                 if (read)
  96.                         ent2->read_pos_plus1 = i+1;
  97.                 else
  98.                         ent2->write_pos_plus1 = i+1;
  99.         }
  100.         return (0);
  101. }

  102. #define NEVENT 32
  103. void *
  104. win32_init(struct event_base *_base)
  105. {
  106.         struct win32op *winop;
  107.         size_t size;
  108.         if (!(winop = mm_calloc(1, sizeof(struct win32op))))
  109.                 return NULL;
  110.         winop->num_fds_in_fd_sets = NEVENT;
  111.         size = FD_SET_ALLOC_SIZE(NEVENT);
  112.         if (!(winop->readset_in = mm_malloc(size)))
  113.                 goto err;
  114.         if (!(winop->writeset_in = mm_malloc(size)))
  115.                 goto err;
  116.         if (!(winop->readset_out = mm_malloc(size)))
  117.                 goto err;
  118.         if (!(winop->writeset_out = mm_malloc(size)))
  119.                 goto err;
  120.         if (!(winop->exset_out = mm_malloc(size)))
  121.                 goto err;
  122.         winop->readset_in->fd_count = winop->writeset_in->fd_count = 0;
  123.         winop->readset_out->fd_count = winop->writeset_out->fd_count
  124.                 = winop->exset_out->fd_count = 0;

  125.         if (evsig_init(_base) < 0)
  126.                 winop->signals_are_broken = 1;

  127.         return (winop);
  128. err:
  129.         XFREE(winop->readset_in);
  130.         XFREE(winop->writeset_in);
  131.         XFREE(winop->readset_out);
  132.         XFREE(winop->writeset_out);
  133.         XFREE(winop->exset_out);
  134.         XFREE(winop);
  135.         return (NULL);
  136. }

  137. int
  138. win32_add(struct event_base *base, evutil_socket_t fd,
  139.                          short old, short events, void *_idx)
  140. {
  141.         struct win32op *win32op = base->evbase;
  142.         struct idx_info *idx = _idx;

  143.         if ((events & EV_SIGNAL) && win32op->signals_are_broken)
  144.                 return (-1);

  145.         if (!(events & (EV_READ|EV_WRITE)))
  146.                 return (0);

  147.         event_debug(("%s: adding event for %d", __func__, (int)fd));
  148.         if (events & EV_READ) {
  149.                 if (do_fd_set(win32op, idx, fd, 1)<0)
  150.                         return (-1);
  151.         }
  152.         if (events & EV_WRITE) {
  153.                 if (do_fd_set(win32op, idx, fd, 0)<0)
  154.                         return (-1);
  155.         }
  156.         return (0);
  157. }

  158. int
  159. win32_del(struct event_base *base, evutil_socket_t fd, short old, short events,
  160.                   void *_idx)
  161. {
  162.         struct win32op *win32op = base->evbase;
  163.         struct idx_info *idx = _idx;

  164.         event_debug(("%s: Removing event for "EV_SOCK_FMT,
  165.                 __func__, EV_SOCK_ARG(fd)));
  166.         if (events & EV_READ)
  167.                 do_fd_clear(base, win32op, idx, 1);
  168.         if (events & EV_WRITE)
  169.                 do_fd_clear(base, win32op, idx, 0);

  170.         return 0;
  171. }

  172. static void
  173. fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in)
  174. {
  175.         out->fd_count = in->fd_count;
  176.         memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET)));
  177. }

  178. /*
  179.   static void dump_fd_set(struct win_fd_set *s)
  180.   {
  181.   unsigned int i;
  182.   printf("[ ");
  183.   for(i=0;i<s->fd_count;++i)
  184.   printf("%d ",(int)s->fd_array[i]);
  185.   printf("]\n");
  186.   }
  187. */

  188. int
  189. win32_dispatch(struct event_base *base, struct timeval *tv)
  190. {
  191.         struct win32op *win32op = base->evbase;
  192.         int res = 0;
  193.         unsigned j, i;
  194.         int fd_count;
  195.         SOCKET s;

  196.         if (win32op->resize_out_sets) {
  197.                 size_t size = FD_SET_ALLOC_SIZE(win32op->num_fds_in_fd_sets);
  198.                 if (!(win32op->readset_out = mm_realloc(win32op->readset_out, size)))
  199.                         return (-1);
  200.                 if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size)))
  201.                         return (-1);
  202.                 if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size)))
  203.                         return (-1);
  204.                 win32op->resize_out_sets = 0;
  205.         }

  206.         fd_set_copy(win32op->readset_out, win32op->readset_in);
  207.         fd_set_copy(win32op->exset_out, win32op->writeset_in);
  208.         fd_set_copy(win32op->writeset_out, win32op->writeset_in);

  209.         fd_count =
  210.             (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ?
  211.             win32op->readset_out->fd_count : win32op->writeset_out->fd_count;

  212.         if (!fd_count) {
  213.                 long msec = tv ? evutil_tv_to_msec(tv) : LONG_MAX;
  214.                 /* Sleep's DWORD argument is unsigned long */
  215.                 if (msec < 0)
  216.                         msec = LONG_MAX;
  217.                 /* Windows doesn't like you to call select() with no sockets */
  218.                 Sleep(msec);
  219.                 return (0);
  220.         }

  221.         EVBASE_RELEASE_LOCK(base, th_base_lock);

  222.         res = select(fd_count,
  223.                      (struct fd_set*)win32op->readset_out,
  224.                      (struct fd_set*)win32op->writeset_out,
  225.                      (struct fd_set*)win32op->exset_out, tv);

  226.         EVBASE_ACQUIRE_LOCK(base, th_base_lock);

  227.         event_debug(("%s: select returned %d", __func__, res));

  228.         if (res <= 0) {
  229.                 return res;
  230.         }

  231.         if (win32op->readset_out->fd_count) {
  232.                 i = rand() % win32op->readset_out->fd_count;
  233.                 for (j=0; j<win32op->readset_out->fd_count; ++j) {
  234.                         if (++i >= win32op->readset_out->fd_count)
  235.                                 i = 0;
  236.                         s = win32op->readset_out->fd_array[i];
  237.                         evmap_io_active(base, s, EV_READ);
  238.                 }
  239.         }
  240.         if (win32op->exset_out->fd_count) {
  241.                 i = rand() % win32op->exset_out->fd_count;
  242.                 for (j=0; j<win32op->exset_out->fd_count; ++j) {
  243.                         if (++i >= win32op->exset_out->fd_count)
  244.                                 i = 0;
  245.                         s = win32op->exset_out->fd_array[i];
  246.                         evmap_io_active(base, s, EV_WRITE);
  247.                 }
  248.         }
  249.         if (win32op->writeset_out->fd_count) {
  250.                 SOCKET s;
  251.                 i = rand() % win32op->writeset_out->fd_count;
  252.                 for (j=0; j<win32op->writeset_out->fd_count; ++j) {
  253.                         if (++i >= win32op->writeset_out->fd_count)
  254.                                 i = 0;
  255.                         s = win32op->writeset_out->fd_array[i];
  256.                         evmap_io_active(base, s, EV_WRITE);
  257.                 }
  258.         }
  259.         return (0);
  260. }

  261. void
  262. win32_dealloc(struct event_base *_base)
  263. {
  264.         struct win32op *win32op = _base->evbase;

  265.         evsig_dealloc(_base);
  266.         if (win32op->readset_in)
  267.                 mm_free(win32op->readset_in);
  268.         if (win32op->writeset_in)
  269.                 mm_free(win32op->writeset_in);
  270.         if (win32op->readset_out)
  271.                 mm_free(win32op->readset_out);
  272.         if (win32op->writeset_out)
  273.                 mm_free(win32op->writeset_out);
  274.         if (win32op->exset_out)
  275.                 mm_free(win32op->exset_out);
  276.         /* XXXXX free the tree. */

  277.         memset(win32op, 0, sizeof(win32op));
  278.         mm_free(win32op);
  279. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值