From 1403daed9fea438a0a5d03c3efeeb414854c66d4 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Tue, 16 Jun 2020 21:42:45 +0200 Subject: Convert idle to use debugging --- idle.c | 91 ++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 47 insertions(+), 44 deletions(-) (limited to 'idle.c') diff --git a/idle.c b/idle.c index 006c8d2..6dda006 100644 --- a/idle.c +++ b/idle.c @@ -30,7 +30,7 @@ idle_check_free(struct uring_task *task) { struct idle *idle = container_of(task, struct idle, idlecheck); - fprintf(stderr, "%s: called with task 0x%p and idle 0x%p\n", __func__, task, idle); + debug(DBG_IDLE, "task %p, idle %p\n", task, idle); } static inline void @@ -126,22 +126,22 @@ idle_check_handshake_complete(struct cfg *cfg, struct uring_task *task, int res) r = read_varint(&pos, &remain, &mclen); if (r < 0) { - fprintf(stderr, "Failed to parse message length\n"); + error("failed to parse message length\n"); return -EINVAL; } else if (r == 0) { return 0; } else if (mclen < 2) { - fprintf(stderr, "Short MC message\n"); + error("short MC message\n"); return -EINVAL; } - fprintf(stderr, "MC message len: %" PRIi32 "\n", mclen); - fprintf(stderr, "Remain: %zu\n", remain); - - if (mclen < remain) + if (mclen < remain) { + debug(DBG_IDLE, "short MC message - len: %" PRIi32 ", remain: %zu\n", + mclen, remain); return 0; + } - fprintf(stderr, "Complete message\n"); + debug(DBG_IDLE, "Complete message\n"); return 1; } @@ -163,7 +163,7 @@ get_player_count(struct cfg *cfg, const char *pos, size_t remain) online = memmem(pos, remain, ONLINE_NEEDLE, strlen(ONLINE_NEEDLE)); if (!online) { - fprintf(stderr, "Could not find online count in JSON\n"); + error("could not find online count in JSON\n"); return -1; } @@ -171,13 +171,13 @@ get_player_count(struct cfg *cfg, const char *pos, size_t remain) end = memchr(online, '}', remain); if (!end) { - fprintf(stderr, "Could not parse JSON (no end)\n"); + error("could not parse JSON (no end)\n"); return -1; } *end = '\0'; if (sscanf(online, ONLINE_NEEDLE " : %u", &count) != 1) { - fprintf(stderr, "Could not parse JSON (online count)\n"); + error("could not parse JSON (online count)\n"); return -1; } @@ -195,7 +195,7 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) int player_count; int r; - fprintf(stderr, "%s: received %i bytes\n", __func__, res); + debug(DBG_IDLE, "res: %i\n", res); if (res < 0) goto out; @@ -212,15 +212,15 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) r = read_varint(&pos, &remain, &mclen); if (r <= 0 || mclen < 2 || mclen < remain) { /* Should not happen since the msg has been checked already */ - fprintf(stderr, "Invalid message\n"); + error("invalid message\n"); goto out; } - fprintf(stderr, "%s: MC message len: %" PRIi32 "\n", __func__, mclen); - fprintf(stderr, "%s: Remain: %zu\n", __func__, remain); + debug(DBG_IDLE, "MC message - len: %" PRIi32 ", remain: %zu\n", + mclen, remain); if (*pos != MC_STATUS_REPLY) { - fprintf(stderr, "Unknown server reply\n"); + error("unknown server reply (0x%02hhx)\n", *pos); goto out; } @@ -229,36 +229,40 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) r = read_varint(&pos, &remain, &jsonlen); if (r <= 0) { - fprintf(stderr, "Could not read JSON length\n"); + error("could not read JSON length\n"); goto out; } - fprintf(stderr, "MC json len: %" PRIi32 "\n", jsonlen); - fprintf(stderr, "Remain: %zu\n", remain); + debug(DBG_IDLE, "MC - json len: %" PRIi32 ", remain: %zu\n", + jsonlen, remain); if (jsonlen < remain) { - fprintf(stderr, "Invalid JSON length\n"); + error("invalid JSON length\n"); goto out; } + /* fprintf(stderr, "JSON: "); for (int i = 0; i < jsonlen; i++) fprintf(stderr, "%c", pos[i]); fprintf(stderr, "\n"); + */ player_count = get_player_count(cfg, pos, remain); - fprintf(stderr, "We have %i players\n", player_count); - if (player_count < 0) goto out; - else if (player_count > 0) + + debug(DBG_IDLE, "%s: currently %i active players\n", + idle->server->name, player_count); + + if (player_count > 0) idle->server->idle_count = 0; - else { + else if (player_count == 0) idle->server->idle_count++; - if (idle->server->idle_count > idle->server->idle_timeout) { - fprintf(stderr, "Stopping idle server %s\n", idle->server->name); - server_stop(cfg, idle->server); - } + + if (idle->server->idle_count > idle->server->idle_timeout) { + verbose("stopping idle server %s\n", idle->server->name); + server_stop(cfg, idle->server); } out: @@ -271,13 +275,15 @@ idle_check_handshake_sent(struct cfg *cfg, struct uring_task *task, int res) { struct idle *idle = container_of(task, struct idle, idlecheck); - fprintf(stderr, "%s: sent %i bytes\n", __func__, res); + debug(DBG_IDLE, "sent %i bytes\n", res); if (res < 0) { uring_task_close_fd(cfg, task); return; } - uring_tbuf_read_until(cfg, &idle->idlecheck, idle_check_handshake_complete, idle_check_handshake_reply); + uring_tbuf_read_until(cfg, &idle->idlecheck, + idle_check_handshake_complete, + idle_check_handshake_reply); } static void @@ -291,13 +297,13 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected char hostname[INET6_ADDRSTRLEN]; if (!connected) { - fprintf(stderr, "%s: idle check connection to remote server failed\n", - idle->server->name); + error("idle check connection to remote server (%s) failed\n", + idle->server->name); idle->server->idle_count = 0; return; } - fprintf(stderr, "%s: connected to remote %s\n", __func__, idle->conn.remotestr); + debug(DBG_IDLE, "connected to remote %s\n", idle->conn.remotestr); port = sockaddr_port(&conn->remote); sockaddr_addr(&conn->remote, hostname, sizeof(hostname)); @@ -317,10 +323,7 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected write_cmd(&cmdbuf, buf, pos); idle->tbuf.len = (cmdbuf - idle->tbuf.buf); - fprintf(stderr, "Sending MC message (%zu bytes):\n", idle->tbuf.len); - for (pos = idle->tbuf.buf; pos < cmdbuf; pos++) - fprintf(stderr, "0x%02hhx ", *pos); - fprintf(stderr, "\n"); + debug(DBG_IDLE, "sending MC message (%zu bytes)\n", idle->tbuf.len); uring_tbuf_write(cfg, &idle->idlecheck, idle_check_handshake_sent); } @@ -330,17 +333,17 @@ idle_cb(struct cfg *cfg, struct uring_task *task, int res) { struct idle *idle = container_of(task, struct idle, task); - fprintf(stderr, "%s: ret is %i (ref %u)\n", __func__, res, task->refcount); - if (task->dead) { - fprintf(stderr, "%s: task is dead\n", __func__); + debug(DBG_IDLE, "task is dead\n"); return; } - if (res != sizeof(idle->value)) + if (res != sizeof(idle->value)) { + error("timerfd_read returned %i\n", res); perrordie("timerfd_read"); + } - fprintf(stderr, "%s: called with value %" PRIu64 "\n", __func__, idle->value); + debug(DBG_IDLE, "timer fired (value: %" PRIu64 ")\n", idle->value); if (!list_empty(&idle->server->proxys)) idle->server->idle_count = 0; @@ -356,7 +359,7 @@ idle_free(struct uring_task *task) { struct idle *idle = container_of(task, struct idle, task); - fprintf(stderr, "%s: called with task %p and idle %p\n", __func__, task, idle); + debug(DBG_IDLE, "task %p, idle %p\n", task, idle); xfree(idle); } @@ -378,7 +381,7 @@ idle_delete(struct cfg *cfg, struct server *server) if (!idle) return; - fprintf(stderr, "%s called, closing fd %i\n", __func__, idle->task.fd); + debug(DBG_IDLE, "closing fd %i\n", idle->task.fd); uring_task_destroy(cfg, &idle->idlecheck); uring_task_destroy(cfg, &idle->task); server->idle = NULL; -- cgit v1.2.3