// Written by retoor@molodetz.nl
// This source code sets up a simple TCP server that listens for connections and
// handles cleanup on exit. The server is intended to interact with an upstream
// server defined by its host and port.
// Imports: Custom includes 'py.h' and 'sock.h' for additional functionality.
// MIT License
#include "py.h"
#include "sock.h"
#include <Python.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>
#define PY_SSIZE_T_CLEAN 1
#define LISTEN_PORT 2222
#define UPSTREAM_HOST "127.0.0.1"
#define UPSTREAM_PORT 9999
void cleanup() {
close(epoll_fd);
close(listen_fd);
py_destruct();
printf("Graceful exit.\n");
}
void handle_sigint(int sig) {
printf("\nCtrl+C pressed.\n");
exit(0);
}
int main() {
if (signal(SIGINT, handle_sigint) == SIG_ERR) {
perror("Failed to register signal handler");
return EXIT_FAILURE;
}
atexit(cleanup);
serve(LISTEN_PORT);
return EXIT_SUCCESS;
}