gSoap:如何优雅地关闭Web服务应用程序?

发布于 2024-10-21 02:08:48 字数 218 浏览 1 评论 0原文

我正在使用 gSoap 编写网络服务。它作为控制台应用程序运行。在我看到的所有 gSoap 示例中,即使在多线程版本中,请求也会像 for(;;;) 那样以无限循环的方式分派。

但是,当用户在控制台上按下空格键时,如何才能使我的 Web 服务正常终止呢?

优选地:

  1. 停止接受新连接;
  2. 为现有的服务;
  3. 退出应用程序

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:

  1. stop accepting new connections;
  2. Serve existing ones;
  3. Exit from application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

固执像三岁 2024-10-28 02:08:48

到目前为止我想到的唯一解决方案是使用超时
肥皂->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).

落叶缤纷 2024-10-28 02:08:48

7.2.4 如何创建多线程独立服务部分文档中的 有用于编写接受循环的示例代码。您需要编写自己的接受循环并添加信号处理,以便它响应 Ctrl-C。

  1. 停止接受新连接:

    离开循环,以便停止调用accept。

  2. 服务现有的:

    线程需要在完成时通知您,以便您可以在活动客户端数量为零时退出。 (boost::thead_group 有一个 join_all 正是这样做的。)

  3. 退出应用程序:

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.

  1. stop accepting new connections:

    Leave the loop so you stop calling accept.

  2. 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.)

  3. Exit from application:

肤浅与狂妄 2024-10-28 02:08:48

您需要做的是注册信号处理程序,以便当您使用 Ctrl + C 终止应用程序时,它会调用您注册的函数,您可以在其中优雅地终止。

例如

class gsoap_test {

public:
   void start() {
     running_ = true;
     while(running_) {
       //gsoap threads
     }
     //stop and cleanup
   }


   void stop() {
        running_ = false;
   }
 private:
      bool running_;
};

 //global variable 
 gsoap_test gsoap;


void sighandler(int sig)
{
    std::cout<< "Signal caught..." << std::endl;

    //Stop gracefully here
    gsoap.stop();
    exit(0);

}

int main(int argc, char** argv) {

  //register signal
   signal(SIGABRT, &sighandler);
   signal(SIGTERM, &sighandler);
   signal(SIGINT, &sighandler);

   gsoap.start();

   return EXIT_SUCCESS;
}

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

class gsoap_test {

public:
   void start() {
     running_ = true;
     while(running_) {
       //gsoap threads
     }
     //stop and cleanup
   }


   void stop() {
        running_ = false;
   }
 private:
      bool running_;
};

 //global variable 
 gsoap_test gsoap;


void sighandler(int sig)
{
    std::cout<< "Signal caught..." << std::endl;

    //Stop gracefully here
    gsoap.stop();
    exit(0);

}

int main(int argc, char** argv) {

  //register signal
   signal(SIGABRT, &sighandler);
   signal(SIGTERM, &sighandler);
   signal(SIGINT, &sighandler);

   gsoap.start();

   return EXIT_SUCCESS;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文