/* Close the WebSocket server and clean up. */ void stop_ws_server (GWSWriter * gwswriter, GWSReader * gwsreader) { pthread_t writer, reader; WSServer *server = NULL; if (!gwsreader || !gwswriter) return; if (!(server = gwswriter->server)) return; pthread_mutex_lock (&gwsreader->mutex); if ((write (gwsreader->self_pipe[1], "x", 1)) == -1 && errno != EAGAIN) LOG (("Unable to write to self pipe on pipeout.\n")); pthread_mutex_unlock (&gwsreader->mutex); pthread_mutex_lock (&gwswriter->mutex); /* if it fails to write, force stop */ if ((write (server->self_pipe[1], "x", 1)) == -1 && errno != EAGAIN) ws_stop (server); pthread_mutex_unlock (&gwswriter->mutex); reader = gwsreader->thread; if (pthread_join (reader, NULL) != 0) LOG (("Unable to join thread: %d %s\n", reader, strerror (errno))); writer = gwswriter->thread; if (pthread_join (writer, NULL) != 0) LOG (("Unable to join thread: %d %s\n", writer, strerror (errno))); }
/* Start the WebSocket server and initialize default options. */ static void start_server (void *ptr_data) { GWSWriter *writer = (GWSWriter *) ptr_data; if ((writer->server = ws_init ("0.0.0.0", "7890")) == NULL) return; ws_set_config_strict (1); if (conf.addr) ws_set_config_host (conf.addr); if (conf.fifo_in) ws_set_config_pipein (conf.fifo_in); if (conf.fifo_out) ws_set_config_pipeout (conf.fifo_out); if (conf.origin) ws_set_config_origin (conf.origin); if (conf.port) ws_set_config_port (conf.port); if (conf.sslcert) ws_set_config_sslcert (conf.sslcert); if (conf.sslkey) ws_set_config_sslkey (conf.sslkey); writer->server->onopen = onopen; set_self_pipe (writer->server->self_pipe); /* select(2) will block in here */ ws_start (writer->server); fprintf (stderr, "Stopping WebSocket server...\n"); ws_stop (writer->server); }
static void handle_signal_action (int sig_number) { if (sig_number == SIGINT) { printf ("SIGINT caught!\n"); /* if it fails to write, force stop */ if ((write (server->self_pipe[1], "x", 1)) == -1 && errno != EAGAIN) ws_stop (server); } else if (sig_number == SIGPIPE) { printf ("SIGPIPE caught!\n"); } }
// Handle correct exiting static void exit_handler(int sig) { // Stop webserver if (ws != NULL) { ws_stop(ws); ws_destroy(ws); } // Exit printf("Exiting....\n"); exit(sig); }
/* Start the WebSocket server and initialize default options. */ static void start_server (void *ptr_data) { GWSWriter *writer = (GWSWriter *) ptr_data; writer->server->onopen = onopen; set_self_pipe (writer->server->self_pipe); /* select(2) will block in here */ ws_start (writer->server); fprintf (stderr, "Stopping WebSocket server...\n"); ws_stop (writer->server); }
/* Command line help. */ static void cmd_help (void) { printf ("\nGWSocket - %s\n\n", GW_VERSION); printf ( "Usage: " "gwsocket [ options ... ] -p [--addr][--origin][...]\n" "The following options can also be supplied to the command:\n\n" "" " -p --port=<port> - Specifies the port to bind.\n" " -h --help - This help.\n" " -V --version - Display version information and exit.\n" " --access-log=<path/file> - Specifies the path/file for the access log.\n" " --addr=<addr> - Specify an IP address to bind to.\n" " --echo-mode - Echo all received messages.\n" " --max-frame-size=<bytes> - Maximum size of a websocket frame. This\n" " includes received frames from the client\n" " and messages through the named pipe.\n" " --origin=<origin> - Ensure clients send the specified origin\n" " header upon the WebSocket handshake.\n" " --pipein=<path/file> - Creates a named pipe (FIFO) that reads\n" " from on the given path/file.\n" " --pipeout=<path/file> - Creates a named pipe (FIFO) that writes\n" " to on the given path/file.\n" " --std - Enable --stdin and --stdout.\n" " --stdin - Send stdin to the websocket.\n" " --stdout - Send received websocket data to stdout.\n" " --strict - Parse messages using strict mode. See\n" " man page for more details.\n" " --ssl-cert=<cert.crt> - Path to SSL certificate.\n" " --ssl-key=<priv.key> - Path to SSL private key.\n" "\n" "See the man page for more information `man gwsocket`.\n\n" "For more details visit: http://gwsocket.io\n" "gwsocket Copyright (C) 2016 by Gerardo Orellana" "\n\n" ); ws_stop (server); exit (EXIT_FAILURE); }
int main (int argc, char **argv) { if ((server = ws_init ("0.0.0.0", "7890")) == NULL) { perror ("Error during ws_init.\n"); exit (EXIT_FAILURE); } /* callbacks */ server->onclose = onclose; server->onmessage = onmessage; server->onopen = onopen; set_self_pipe (); if (setup_signals () != 0) exit (EXIT_FAILURE); if (read_option_args (argc, argv) == 0) ws_start (server); ws_stop (server); return EXIT_SUCCESS; }