From 6ed78636a5fc40eafbaf31960460b5df0b14fc64 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Mon, 7 Oct 2024 21:20:01 -0400 Subject: [PATCH] Pretend to run the emulator --- include/request.h | 2 ++ main.c | 14 +++++++++++--- request.c | 11 +++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/request.h b/include/request.h index 399d58f..c5f2603 100644 --- a/include/request.h +++ b/include/request.h @@ -22,6 +22,7 @@ typedef enum rdb_read_state_t { typedef struct RdbRequest { int connfd; + bool blocking; struct Buffer { char buf[INBUF_LEN]; size_t len; @@ -36,6 +37,7 @@ typedef struct RdbRequest { void rdb_request_init(RdbRequest *req, int connfd, char *cmd, size_t cmdlen); void rdb_request_reset(RdbRequest *req); +void rdb_request_set_blocking(RdbRequest *req, bool blocking); rdb_read_result_t rdb_request_read(RdbRequest *req, size_t *len); #endif \ No newline at end of file diff --git a/main.c b/main.c index 201c28d..ce21ea6 100644 --- a/main.c +++ b/main.c @@ -78,10 +78,11 @@ const uint32_t SYSTEM_REGISTERS[] = { const uint32_t PC_INDEX = 32 + 13; -int handle_command(RdbClient *client, CommandBuf *cmd, VB *sim) { +int handle_command(RdbClient *client, CommandBuf *cmd, VB *sim, bool *running) { rdb_client_begin_packet(client); if (cmd_match_only_str(cmd, "\x03")) { + *running = false; rdb_client_write_str(client, "T05thread:p1.t1;threads:p1.t1"); return rdb_client_send_packet(client); } @@ -183,6 +184,7 @@ int handle_command(RdbClient *client, CommandBuf *cmd, VB *sim) { } if (cmd_match_only_str(cmd, "c")) { printf("running until we hit a breakpoint or the server stops us\n"); + *running = true; return 0; } if (cmd_match_only_str(cmd, "?")) { @@ -224,6 +226,8 @@ int server(int connfd, VB *sim) { rdb_request_init(&req, connfd, buf, BUFLEN); rdb_client_init(&client, connfd); + bool running = false; + while (1) { rdb_read_result_t result = rdb_request_read(&req, &len); if (result == read_result_error) { @@ -232,7 +236,10 @@ int server(int connfd, VB *sim) { printf("client has disconnected\n"); return 0; } else if (result == read_result_pending) { - // TODO: should run the emulator while we wait + if (running) { + printf("pretend the emulator is running now\n"); + sleep(1); + } continue; } else { printf("received command \"%.*s\"\n", (int) len, buf); @@ -240,10 +247,11 @@ int server(int connfd, VB *sim) { CommandBuf cmd; cmd.buf = buf; cmd.len = len; - int res = handle_command(&client, &cmd, sim); + int res = handle_command(&client, &cmd, sim, &running); if (res != 0) { return res; } + rdb_request_set_blocking(&req, !running); rdb_request_reset(&req); } diff --git a/request.c b/request.c index ca66cf8..f17d625 100644 --- a/request.c +++ b/request.c @@ -2,12 +2,14 @@ #include #include #include +#include #include void rdb_request_init(RdbRequest *req, int connfd, char *cmd, size_t cmdlen) { req->connfd = connfd; req->cmd = cmd; req->cmdlen = cmdlen; + req->blocking = true; rdb_request_reset(req); } @@ -19,14 +21,19 @@ void rdb_request_reset(RdbRequest *req) { req->chk = 0; } +void rdb_request_set_blocking(RdbRequest *req, bool blocking) { + req->blocking = blocking; +} + static rdb_read_result_t read_char(RdbRequest *req, char *in) { if (req->inbuf.index >= req->inbuf.len) { - ssize_t inlen = read(req->connfd, req->inbuf.buf, INBUF_LEN); + int flags = req->blocking ? 0 : MSG_DONTWAIT; + ssize_t inlen = recv(req->connfd, req->inbuf.buf, INBUF_LEN, flags); if (inlen == 0) { return read_result_disconnected; } if (inlen < 0) { - if (errno == EAGAIN) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { return read_result_pending; } else { perror("could not read incoming packet");