WSAEventSelect,一个事件,多个套接字 [英] WSAEventSelect, one event, multiple sockets

查看:34
本文介绍了WSAEventSelect,一个事件,多个套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的软件中重写代码以支持多个连接,直到现在,我使用select.为了让我的软件更便携,我改用 WSAPoll.在 WSAPoll 中发现 Microsoft 无法解决的错误后,我想更改为建议的 WSAEventSelect.由于 WSAWaitForMultipleEvents 最多只支持 64 个事件,出于节省资源的原因,我想将一个 hEvent 连接到多个套接字.我现在的问题是,是

I am rewriting code in my software to support multiple connections, until now, I use select. To get my software more portable I changed to WSAPoll. After finding a bug in WSAPoll which Microsoft will not solve, I want to change to the suggested WSAEventSelect. As WSAWaitForMultipleEvents only support up to 64 Events and for resource saving reasons, I want to connect a hEvent to multiple sockets. My question now is, is

rc = WSAEventSelect(s1, hEventObject1, FD_READ);
rc = WSAEventSelect(s2, hEventObject1, FD_READ);

合法操作?请仅在您有确凿的事实而非意见时才回答.或者你自己以前用过这种方式.另外,由于某些原因,我不想使用 boost(例如,1.52 在套接字部分有一个错误(有趣的是,它几乎与 WSAPoll 相同)).也请不要你为什么不......"

a legit operation? Please answer only if you have hard facts, not opinions. Or you have used that way yourself before. Also, I do not want to use boost for some reasons (for e.g. 1.52 has a bug in the socket part (funny that is nearly the same bug as WSAPoll)). Also please no "Why don't you..."

推荐答案

单个等待事件不应与多个套接字关联.每个套接字都应该使用它自己的单独事件.否则,如果多个套接字使用同一个事件,当该事件被发出信号时,您将不知道哪个套接字满足了等待.即使可以,当调用 WSAEnumNetworkEvents() 以获取事件数据时,也会出现竞争条件,因为它会重置可能已由另一个套接字设置的事件.所以你可能会丢失事件.

A single wait event should not be associated with multiple sockets. Each socket should use it own individual event. Otherwise, if multiple sockets were to use the same event, you would not know which socket satisfied the wait when that event is signaled. Even if you could, there would also be a race condition when WSAEnumNetworkEvents() is called to get the event data, as it resets the event, which might have already been set by another socket. So you could lose events.

要绕过 64 个句柄的限制,请按照 WaitForMultipleObjects() 文档中的说明进行操作:

To get around the 64-handle limit, do what the WaitForMultipleObjects() documentation says to do:

要等待超过 MAXIMUM_WAIT_OBJECTS 个句柄,请使用以下方法之一:

To wait on more than MAXIMUM_WAIT_OBJECTS handles, use one of the following methods:

• 创建一个线程以等待 MAXIMUM_WAIT_OBJECTS 句柄,然后等待该线程和其他句柄.使用此技术将句柄分成 MAXIMUM_WAIT_OBJECTS 组.

• Create a thread to wait on MAXIMUM_WAIT_OBJECTS handles, then wait on that thread plus the other handles. Use this technique to break the handles into groups of MAXIMUM_WAIT_OBJECTS.

• 调用RegisterWaitForSingleObject 以等待每个句柄.线程池中的一个等待线程等待 MAXIMUM_WAIT_OBJECTS 个已注册对象,并在该对象收到信号或超时间隔到期后分配一个工作线程.

• Call RegisterWaitForSingleObject to wait on each handle. A wait thread from the thread pool waits on MAXIMUM_WAIT_OBJECTS registered objects and assigns a worker thread after the object is signaled or the time-out interval expires.

否则,请改用 WSAAsyncSelect(),并让它在任何给定套接字满足请求条件时通过窗口消息通知您.

Otherwise, use WSAAsyncSelect() instead, and let it notify you via a window message whenever any given socket satisfies the requested conditions.

这篇关于WSAEventSelect,一个事件,多个套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆