如何正确关闭 IOCP 服务器?
我可以找到大量关于启动 IOCP 服务器的文章,但没有一篇关于正确关闭它的文章 =/
完成后关闭服务器的正确方法是什么?更具体地说,我已经使用 PostQueuedCompletionStatus() 来告诉工作线程退出,但是我是否还需要取消所有待处理的 IO,并在退出之前关闭所有套接字?
我在 MSDN 上找到了 CancelSynchronousIo() ,似乎我可以让每个工作线程在收到退出的完成通知时调用此函数...这是正确的方法吗?
感谢您对此的任何帮助。
I can find tonnes of article's about starting up an IOCP server, but none about properly shutting it down =/
What is the proper way to shut the server down when you are done? more specifically, I already use PostQueuedCompletionStatus() to tell the worker threads to quit, but don't I also need to cancel all the pending IO, and close all the sockets before I quit?
I found CancelSynchronousIo() on MSDN, and it seems like I could have each worker thread call this function when it receives the completion notification to quit...is this the proper way to do it?
thanks for any help on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果循环遍历所有打开的连接并关闭所有套接字,则所有待处理的 I/O 将自行完成,服务器将关闭。您可以使用延迟关闭来关闭以允许任何挂起的写入数据完成,也可以关闭套接字上的延迟并导致挂起的写入数据被丢弃,并使用 RST 重置连接而不是彻底关闭。
我有一些 IOCP 服务器示例这里,您可能已经有了已经见过,但它展示了如何实现干净的关闭。
If you loop through all of the open connections and shut down all of the sockets then all of the pending I/O will complete on its own and the server will shut down. You can either close with a lingering close to allow any pending write data to complete or you can turn linger off on the sockets and cause pending write data to be discarded and the connections to be reset with an RST rather than shutdown cleanly.
I have some IOCP server examples here, which you may already have seen, but which show how you could achieve a clean shutdown.