gSoap:如何优雅地关闭Web服务应用程序?
我正在使用 gSoap 编写网络服务。它作为控制台应用程序运行。在我看到的所有 gSoap 示例中,即使在多线程版本中,请求也会像 for(;;;) 那样以无限循环的方式分派。
但是,当用户在控制台上按下空格键时,如何才能使我的 Web 服务正常终止呢?
优选地:
- 停止接受新连接;
- 为现有的服务;
- 退出应用程序
I'm using gSoap to write a webservice. It's running as a console application. In all gSoap examples I see, that requests are dispatched in infinite loop like for(;;;) even in multi-threaded version.
But how can I make my webservice to terminate gracefully when, say, user presses space on the console?
Preferably:
- stop accepting new connections;
- Serve existing ones;
- Exit from application
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
到目前为止我想到的唯一解决方案是使用超时
肥皂->recv_timeout = 20;
肥皂->send_timeout = 20;
肥皂->connect_timeout = 5;
肥皂->accept_timeout = 5;
然后所有阻塞函数都会定期返回。但这对我来说并不理想,因为即使正在进行传输,我也希望能够快速终止应用程序,但同时,不想损害缓慢/片状连接的可靠性(它是一个嵌入式应用程序)通过 GPRS 连接的设备)。
The only solution I came up so far is using timeouts
soap->recv_timeout = 20;
soap->send_timeout = 20;
soap->connect_timeout = 5;
soap->accept_timeout = 5;
Then all blocking functions return periodically. But this is not ideal for me, because I want to be able to terminate the app quickly even if there is an ongoing transmission, but at the same time, don't want to compromise reliability on a slow/flaky connection (it's an embedded device connected via GPRS).
7.2.4 如何创建多线程独立服务部分文档中的 有用于编写接受循环的示例代码。您需要编写自己的接受循环并添加信号处理,以便它响应 Ctrl-C。
停止接受新连接:
离开循环,以便停止调用accept。
服务现有的:
线程需要在完成时通知您,以便您可以在活动客户端数量为零时退出。 (boost::thead_group 有一个 join_all 正是这样做的。)
退出应用程序:
The section 7.2.4 How to Create a Multi-Threaded Stand-Alone Service in the documentation has example code for writing an accept loop. You need to write your own accept loop and add signal handling so it responds to Ctrl-C.
stop accepting new connections:
Leave the loop so you stop calling accept.
Serve existing ones:
The threads need to inform you when they are finished, so you can exit when the number of active clients is zero. (boost::thead_group has a join_all which does exactly that.)
Exit from application:
您需要做的是注册信号处理程序,以便当您使用 Ctrl + C 终止应用程序时,它会调用您注册的函数,您可以在其中优雅地终止。
例如
What you need to do is register signal handler so when you terminate your application using Ctrl + C, it calls you registered function where you can gracefully terminates.
e.g