diff --git a/MD_Getc.c b/MD_Getc.c index dc18fbb..fe91d22 100644 --- a/MD_Getc.c +++ b/MD_Getc.c @@ -6,8 +6,12 @@ #include #endif #include +#include #include "MagiDoor.h" +extern time_t mdtimeout; +extern time_t mdtimeremaining; + char md_getc() { char c; ssize_t ret; @@ -25,9 +29,18 @@ char md_getc() { #else if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { #endif - usleep(1); + if (mdtimeout <= time(NULL)) { + md_printf("\r\nIdle timeout!\r\n"); + md_exit(0); + } + if (mdtimeremaining <= time(NULL)) { + md_printf("\r\nOut of time!\r\n"); + md_exit(0); + } + usleep(100); continue; } + mdtimeout = time(NULL) + 900; return c; } } diff --git a/MD_Init.c b/MD_Init.c index 3529f68..d0dada4 100644 --- a/MD_Init.c +++ b/MD_Init.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "MagiDoor.h" @@ -24,6 +25,9 @@ MDDoorControl_t mdcontrol; struct termios ttysave; #endif +time_t mdtimeout; +time_t mdtimeremaining; + static void md_cfg_read_line(char *buffer, int len, FILE *fptr) { fgets(buffer, len, fptr); if (buffer[strlen(buffer) - 1] == '\n') { @@ -74,13 +78,7 @@ int read_door32(const char *dropfile) { md_cfg_read_line(buffer, 256, fptr); // node no mdcontrol.node = strtol(buffer, NULL, 10); -#if defined(WIN32) || defined(_MSC_VER) - WSADATA wsa; - WSAStartup(WINSOCK_VERSION, &wsa); -#else - mdcontrol.socket = -1; -#endif return 0; } @@ -171,8 +169,6 @@ int read_doorsys(const char *dropfile) { fclose(fptr); - mdcontrol.socket = -1; - return 0; } @@ -187,13 +183,15 @@ void md_exit(int exitcode) { exit(exitcode); } -int md_init(const char *dropfile) { +int md_init(const char *dropfile, int socket) { char *filename = strrchr(dropfile, PATH_SEP); #if !defined(WIN32) && !defined(_MSC_VER) struct termios ttystate; #endif int ret; + mdcontrol.socket = socket; + if (filename == NULL) { filename = (char *)dropfile; } else { @@ -207,8 +205,7 @@ int md_init(const char *dropfile) { } if (mdcontrol.socket == -1) { -#if defined(WIN32) || defined(_MSC_VER) -#else +#if !defined(WIN32) && !defined(_MSC_VER) tcgetattr(STDIN_FILENO, &ttystate); ttysave = ttystate; @@ -220,7 +217,14 @@ int md_init(const char *dropfile) { tcsetattr(STDIN_FILENO, TCSANOW, &ttystate); #endif - } + } else { +#if defined(WIN32) || defined(_MSC_VER) + WSADATA wsa; + WSAStartup(WINSOCK_VERSION, &wsa); +#endif + } + mdtimeremaining = time(NULL) + mdcontrol.user_timeleft; + mdtimeout = time(NULL) + 900; return ret; } diff --git a/MagiDoor.h b/MagiDoor.h index dd12ddd..f521f87 100644 --- a/MagiDoor.h +++ b/MagiDoor.h @@ -18,7 +18,7 @@ typedef struct MDDoorControl { extern MDDoorControl_t mdcontrol; -extern int md_init(const char *dropfile); +extern int md_init(const char *dropfile, int socket); extern void md_exit(int exitcode); extern void md_putchar(char c); extern void md_printf(const char *fmt, ...);