fix timeout

This commit is contained in:
Andrew Pamment 2021-10-27 10:47:53 +10:00
parent 410cc2b6c6
commit ddeede0ebe
3 changed files with 37 additions and 30 deletions

View File

@ -2,9 +2,9 @@
#include <stdio.h>
#if defined(_MSC_VER) || defined(WIN32)
#include <winsock2.h>
#define STDIN_FILENO 1
#else
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#endif
#include <time.h>
@ -19,23 +19,31 @@ extern time_t mdtimeremaining;
char md_getc() {
char c = 'x';
int ret;
int rs;
int stage = 0;
struct timeval tv;
while (1) {
fd_set rfd;
FD_ZERO(&rfd);
if (mdcontrol.socket == -1) {
ret = read(STDIN_FILENO, &c, 1);
FD_SET(STDIN_FILENO, &rfd);
} else {
ret = recv(mdcontrol.socket, &c, 1, 0);
FD_SET(mdcontrol.socket, &rfd);
}
if (ret == 0) {
md_exit(0);
tv.tv_sec = 60;
tv.tv_usec = 0;
if (mdcontrol.socket == -1) {
rs = select(STDIN_FILENO + 1, &rfd, NULL, NULL, &tv);
} else {
rs = select(mdcontrol.socket + 1, &rfd, NULL, NULL, &tv);
}
#if defined(_MSC_VER) || defined(WIN32)
if (ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) {
#else
if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
#endif
if (rs == 0) {
if (mdtimeout <= time(NULL)) {
md_printf("\r\nIdle timeout!\r\n");
md_exit(0);
@ -44,18 +52,13 @@ char md_getc() {
md_printf("\r\nOut of time!\r\n");
md_exit(0);
}
#if defined(_MSC_VER)
Sleep(1);
#else
usleep(100);
#endif
continue;
}
else if (ret < 0) {
} else if (rs == -1 && errno != EINTR) {
md_exit(0);
}
if (mdcontrol.socket != -1) {
} else if (mdcontrol.socket != -1 && FD_ISSET(mdcontrol.socket, &rfd)) {
ret = recv(mdcontrol.socket, &c, 1, 0);
if (ret <= 0) {
md_exit(0);
}
if ((unsigned char)c == IAC && stage == 0) {
stage = 1;
continue;
@ -81,14 +84,21 @@ char md_getc() {
}
continue;
}
}
mdtimeout = time(NULL) + 900;
return c;
mdtimeout = time(NULL) + 900;
return c;
} else if (mdcontrol.socket == -1 && FD_ISSET(STDIN_FILENO, &rfd)) {
ret = read(STDIN_FILENO, &c, 1);
if (ret <= 0) {
md_exit(0);
}
mdtimeout = time(NULL) + 900;
return c;
}
}
}
char md_get_answer(char *options) {
char md_get_answer(const char *options) {
char c;
c = md_getc();
while (strchr(options, c) == NULL) {

View File

@ -4,9 +4,6 @@
#if defined(_MSC_VER) || defined(WIN32)
#include <winsock2.h>
#include "MD_AnsiCons.h"
#define STDOUT_FILENO 0
#else
#include <sys/socket.h>
#include <unistd.h>

View File

@ -4,7 +4,7 @@
#include <stdint.h>
#define MAGIDOOR_VERSION_MAJOR 1
#define MAGIDOOR_VERSION_MINOR 2
#define MAGIDOOR_VERSION_MINOR 3
#define TRUE 1
#define FALSE 0
@ -34,5 +34,5 @@ extern int md_getstring(char* ptr, int maxlen, char minchar, char maxchar);
extern void md_sendfile(const char* filename, int pause);
extern void md_clr_scr();
extern void md_set_cursor(int y, int x);
extern char md_get_answer(char* options);
extern char md_get_answer(const char* options);
#endif