Alex Perry : Don't call stream functions in signal handlers
Me : close socket to force exit when termination signal is received while idle.
This commit is contained in:
parent
d1d7e6ad38
commit
3a6587ae16
1 changed files with 20 additions and 14 deletions
|
@ -32,6 +32,8 @@
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#include <io.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdlib.h> // atoi() atof() abs() system()
|
#include <stdlib.h> // atoi() atof() abs() system()
|
||||||
|
@ -110,9 +112,10 @@ static void usage( const string& prog ) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::deque<std::string> waitingTiles;
|
deque<string> waitingTiles;
|
||||||
typedef std::map<std::string,time_t> CompletedTiles;
|
typedef map<string,time_t> CompletedTiles;
|
||||||
CompletedTiles completedTiles;
|
CompletedTiles completedTiles;
|
||||||
|
netSocket socket;
|
||||||
|
|
||||||
#ifdef HAVE_SVN_CLIENT_H
|
#ifdef HAVE_SVN_CLIENT_H
|
||||||
|
|
||||||
|
@ -276,19 +279,23 @@ typedef void (__cdecl * sighandler_t)(int);
|
||||||
|
|
||||||
bool terminating = false;
|
bool terminating = false;
|
||||||
sighandler_t prior_signal_handlers[32];
|
sighandler_t prior_signal_handlers[32];
|
||||||
int termination_triggering_signals[] =
|
int termination_triggering_signals[] = {
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
{SIGHUP, SIGINT, SIGQUIT, SIGKILL, 0}; // zero terminated
|
SIGHUP, SIGINT, SIGQUIT, SIGKILL,
|
||||||
#else
|
#else
|
||||||
{SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT, 0}; // zero terminated
|
SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT,
|
||||||
#endif
|
#endif
|
||||||
|
0}; // zero terminated
|
||||||
|
|
||||||
void terminate_request_handler(int param) {
|
void terminate_request_handler(int param) {
|
||||||
cout << "\nReceived signal " << param << ", "
|
char msg[] = "\nReceived signal XX, intend to exit soon.\n"
|
||||||
<< "intend to terminate soon, "
|
"repeat the signal to force immediate termination.\n";
|
||||||
<< "repeat to force an immediate effect.\n";
|
msg[17] = '0' + param / 10;
|
||||||
|
msg[18] = '0' + param % 10;
|
||||||
|
write(1, msg, sizeof(msg) - 1);
|
||||||
terminating = true;
|
terminating = true;
|
||||||
signal(param, prior_signal_handlers[param]);
|
signal(param, prior_signal_handlers[param]);
|
||||||
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -509,14 +516,13 @@ int main( int argc, char **argv ) {
|
||||||
|
|
||||||
// Must call this before any other net stuff
|
// Must call this before any other net stuff
|
||||||
netInit( &argc,argv );
|
netInit( &argc,argv );
|
||||||
netSocket s;
|
|
||||||
|
|
||||||
if ( ! s.open( false ) ) { // open a UDP socket
|
if ( ! socket.open( false ) ) { // open a UDP socket
|
||||||
printf("error opening socket\n");
|
printf("error opening socket\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( s.bind( host, port ) == -1 ) {
|
if ( socket.bind( host, port ) == -1 ) {
|
||||||
printf("error binding to port %d\n", port);
|
printf("error binding to port %d\n", port);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -565,8 +571,8 @@ int main( int argc, char **argv ) {
|
||||||
if (verbose && waitingTiles.empty()) {
|
if (verbose && waitingTiles.empty()) {
|
||||||
cout << "Idle; waiting for FlightGear position\n";
|
cout << "Idle; waiting for FlightGear position\n";
|
||||||
}
|
}
|
||||||
s.setBlocking(waitingTiles.empty());
|
socket.setBlocking(waitingTiles.empty());
|
||||||
len = s.recv(msg, maxlen, 0);
|
len = socket.recv(msg, maxlen, 0);
|
||||||
if (len >= 0) {
|
if (len >= 0) {
|
||||||
msg[len] = '\0';
|
msg[len] = '\0';
|
||||||
recv_msg = true;
|
recv_msg = true;
|
||||||
|
@ -579,7 +585,7 @@ int main( int argc, char **argv ) {
|
||||||
// Ignore messages where the location does not change
|
// Ignore messages where the location does not change
|
||||||
if ( lat != last_lat || lon != last_lon ) {
|
if ( lat != last_lat || lon != last_lon ) {
|
||||||
cout << "pos in msg = " << lat << "," << lon << endl;
|
cout << "pos in msg = " << lat << "," << lon << endl;
|
||||||
std::deque<std::string> oldRequests;
|
deque<string> oldRequests;
|
||||||
oldRequests.swap( waitingTiles );
|
oldRequests.swap( waitingTiles );
|
||||||
int lat_dir, lon_dir, dist;
|
int lat_dir, lon_dir, dist;
|
||||||
if ( last_lat == nowhere || last_lon == nowhere ) {
|
if ( last_lat == nowhere || last_lon == nowhere ) {
|
||||||
|
|
Loading…
Reference in a new issue