view mgw/main.c @ 137:f05183b18d29

top Makefile: add liboutrt
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Oct 2022 23:59:17 -0800
parents f062c32a5116
children a6eb2de277f6
line wrap: on
line source

/*
 * Main module for themwi-mgw.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>

extern int dtmf_timer_running;
extern struct timeval dtmf_next_time;

fd_set select_for_read;
void (*select_handlers[FD_SETSIZE])();
void *select_data[FD_SETSIZE];
struct timeval cur_event_time;

static int max_fd;

update_max_fd(newfd)
{
	if (newfd >= FD_SETSIZE) {
		syslog(LOG_CRIT, "FATAL: file descriptor %d >= FD_SETSIZE",
			newfd);
		exit(1);
	}
	if (newfd > max_fd)
		max_fd = newfd;
}

main(argc, argv)
	char **argv;
{
	fd_set fds;
	struct timeval timeout;
	int cc, i;

	openlog("themwi-mgw", 0, LOG_LOCAL5);
	read_config_file();
	dtmf_init_sample_arrays();
	if (create_ctrl_socket() < 0) {
		fprintf(stderr, "error creating TMGW control socket\n");
		exit(1);
	}
	signal(SIGPIPE, SIG_IGN);
	/* main select loop */
	for (;;) {
		bcopy(&select_for_read, &fds, sizeof(fd_set));
		if (dtmf_timer_running) {
			if (timercmp(&dtmf_next_time, &cur_event_time, >))
				timersub(&dtmf_next_time, &cur_event_time,
					 &timeout);
			else
				timerclear(&timeout);
			cc = select(max_fd+1, &fds, 0, 0, &timeout);
		} else
			cc = select(max_fd+1, &fds, 0, 0, 0);
		if (cc < 0) {
			if (errno == EINTR)
				continue;
			syslog(LOG_CRIT, "select: %m");
			exit(1);
		}
		gettimeofday(&cur_event_time, 0);
		for (i = 0; cc && i <= max_fd; i++) {
			if (FD_ISSET(i, &fds)) {
				select_handlers[i](i, select_data[i]);
				cc--;
			}
		}
		if (dtmf_timer_running &&
		    !timercmp(&cur_event_time, &dtmf_next_time, <))
			dtmf_timer_process();
		free_deleted_endpoints();
	}
}