Add support for clock ticks

This commit is contained in:
2021-10-12 13:54:08 -04:00
parent 995219d965
commit bf3dd49cc8
4 changed files with 140 additions and 43 deletions

View File

@@ -8,6 +8,7 @@
#include <ws2tcpip.h>
#include <winsock2.h>
#include <synchapi.h>
#include <sysinfoapi.h>
@@ -19,9 +20,16 @@ public:
DrivenEngine *driven_;
SOCKET socket_[MAX_CHAN];
bool connected_[MAX_CHAN];
bool engine_wakeup_;
bool short_sleep_;
std::map<int, SOCKET> listen_sockets_;
std::unique_ptr<char> chbuf;
int64_t basetime_;
int64_t get_now() {
FILETIME ft_now;
GetSystemTimeAsFileTime(&ft_now);
return (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);
}
static PADDRINFOA find_good_addr(PADDRINFOA addrinfo) {
for (PADDRINFOA addr = addrinfo; addr != nullptr; addr = addr->ai_next) {
@@ -56,7 +64,7 @@ public:
goto error;
}
if (status != 0) {
err = "DNS resolution hard failure";
err = "DNS resolution malfunction";
goto error;
}
goodaddr = find_good_addr(addrs);
@@ -72,7 +80,7 @@ public:
int errcode = WSAGetLastError();
if (errcode != WSAEWOULDBLOCK) {
std::ostringstream oss;
oss << "Error " << errcode;
oss << "connect error " << errcode;
err = oss.str();
goto error;
}
@@ -111,7 +119,7 @@ public:
socket_[i] = INVALID_SOCKET;
connected_[i] = false;
}
engine_wakeup_ = false;
short_sleep_ = false;
chbuf.reset(new char[65536]);
}
@@ -132,7 +140,7 @@ public:
void handle_lua_source() {
if (driven_->drv_get_rescan_lua_source()) {
driven_->drv_set_lua_source(util::read_lua_source("lua"));
engine_wakeup_ = true;
short_sleep_ = true;
}
}
@@ -156,8 +164,8 @@ public:
std::string err;
SOCKET sock = open_connection(driven_->drv_get_target(chid), err);
if (sock == INVALID_SOCKET) {
driven_->drv_notify_close(chid);
engine_wakeup_ = true;
driven_->drv_notify_close(chid, err);
short_sleep_ = true;
} else {
socket_[chid] = sock;
connected_[chid] = false;
@@ -200,21 +208,25 @@ public:
char c = key.uChar.AsciiChar;
ascii[nascii++] = c;
}
driven_->drv_recv_incoming(0, nascii, ascii);
if (nascii > 0) {
driven_->drv_recv_incoming(0, nascii, ascii);
short_sleep_ = true;
}
}
engine_wakeup_ = true;
}
void handle_clock() {
int now = get_now() - basetime_;
driven_->drv_set_clock(double(now) / 10000000.0);
}
void close_socket(int chid) {
void close_socket(int chid, const std::string err) {
assert(socket_[chid] != INVALID_SOCKET);
assert(closesocket(socket_[chid]) == 0);
driven_->drv_notify_close(chid);
driven_->drv_notify_close(chid, err);
socket_[chid] = INVALID_SOCKET;
connected_[chid] = false;
engine_wakeup_ = true;
short_sleep_ = true;
}
bool calc_select_sets(fd_set &rfds, fd_set &wfds, fd_set &efds) const {
@@ -245,7 +257,7 @@ public:
int chid = driven_->drv_notify_accept(port);
socket_[chid] = chsock;
connected_[chid] = true;
engine_wakeup_ = true;
short_sleep_ = true;
continue;
}
int errcode = WSAGetLastError();
@@ -293,7 +305,7 @@ public:
if (nbytes > 0) {
int wbytes = send(sock, bytes, nbytes, 0);
if (wbytes == SOCKET_ERROR) {
close_socket(chid);
close_socket(chid, "send failure");
continue;
} else {
driven_->drv_sent_outgoing(chid, wbytes);
@@ -303,11 +315,11 @@ public:
if (FD_ISSET(sock, &rfds)) {
int nrecv = recv(sock, chbuf.get(), 65536, 0);
if ((nrecv == SOCKET_ERROR) || (nrecv == 0)) {
close_socket(chid);
close_socket(chid, "recv failure");
continue;
} else {
driven_->drv_recv_incoming(chid, nrecv, chbuf.get());
engine_wakeup_ = true;
short_sleep_ = true;
}
}
}
@@ -320,22 +332,21 @@ public:
assert(hconsole != INVALID_HANDLE_VALUE);
init(de);
DrivenEngine::set(de);
basetime_ = get_now();
driven_->drv_set_lua_source(util::read_lua_source("lua"));
driven_->drv_invoke_event_init();
handle_listen_ports();
while (!de->drv_get_stop_driver()) {
engine_wakeup_ = false;
short_sleep_ = false;
handle_lua_source();
handle_new_closed_sockets();
handle_new_outgoing_sockets();
handle_console_output();
handle_console_input();
int mstimeout = engine_wakeup_ ? 0 : 100;
handle_console_output();
int mstimeout = short_sleep_ ? 0 : 100;
handle_socket_input_output(mstimeout);
handle_clock();
if (engine_wakeup_) {
de->drv_invoke_event_update();
}
de->drv_invoke_event_update();
}
DrivenEngine::set(nullptr);
}