sscall

UDP based voice chat
git clone git://git.2f30.org/sscall
Log | Files | Refs | README | LICENSE

commit 720e8b8110723ecf35a3b5c2acf4072273926072
parent c475325159d22a14df121efa2a8a80c0926262de
Author: sin <sin@2f30.org>
Date:   Mon,  4 Jun 2012 15:10:47 +0100

sscall: Handle SIGINT gracefully


Diffstat:
Msscall.c | 25+++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)

diff --git a/sscall.c b/sscall.c @@ -4,6 +4,7 @@ #include <stdlib.h> #include <string.h> #include <err.h> +#include <signal.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> @@ -69,6 +70,8 @@ static pthread_mutex_t pcm_buf_lock; /* Condition variable on which ao_play() blocks */ static pthread_cond_t tx_pcm_cond; +static volatile int handle_sigint; + /* Play back audio from the client */ static void * output_pcm(void *data __attribute__ ((unused))) @@ -156,6 +159,18 @@ usage(const char *s) exit(EXIT_SUCCESS); } +static void +sig_handler(int signum) +{ + switch (signum) { + case SIGINT: + handle_sigint = 1; + break; + default: + break; + } +} + int main(int argc, char *argv[]) { @@ -173,6 +188,9 @@ main(int argc, char *argv[]) int c; char host[NI_MAXHOST]; + if (signal(SIGINT, sig_handler) == SIG_ERR) + err(1, "signal"); + prog = *argv; while ((c = getopt(argc, argv, "hb:c:r:d:v")) != -1) { switch (c) { @@ -313,6 +331,13 @@ main(int argc, char *argv[]) /* Receive audio data from other end and prepare * for playback */ do { + /* Handle SIGINT gracefully */ + if (handle_sigint) { + if (fverbose) + printf("Interrupted, exiting...\n"); + break; + } + addr_len = sizeof(their_addr); bytes = recvfrom(srv_sockfd, buf, sizeof(buf), MSG_DONTWAIT,