gSoap: how to gracefully shutdown the webservice application?

拥有回忆 提交于 2019-12-04 19:57:32

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:

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!