summaryrefslogtreecommitdiff
path: root/idle.c
diff options
context:
space:
mode:
Diffstat (limited to 'idle.c')
-rw-r--r--idle.c112
1 files changed, 27 insertions, 85 deletions
diff --git a/idle.c b/idle.c
index bfb8fc8..0ced98d 100644
--- a/idle.c
+++ b/idle.c
@@ -15,25 +15,10 @@
#include "idle.h"
struct idle {
- struct server *server;
-
struct uring_task task;
uint64_t value;
-
- struct uring_task idlecheck;
- struct connection conn;
- struct uring_task_buf tbuf;
};
-static void
-idle_check_free(struct uring_task *task)
-{
- struct idle *idle = container_of(task, struct idle, idlecheck);
-
- assert_return(task && idle);
- debug(DBG_IDLE, "task %p, idle %p", task, idle);
-}
-
static inline void
write_byte(char **pos, char byte)
{
@@ -209,12 +194,12 @@ get_player_count(struct cfg *cfg, const char *pos, size_t remain)
static void
idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res)
{
- struct idle *idle = container_of(task, struct idle, idlecheck);
+ struct server *server = container_of(task, struct server, idle_task);
int32_t mclen;
int32_t jsonlen;
char *pos;
size_t remain;
- int player_count;
+ int player_count = -1;
int r;
assert_return(cfg && task);
@@ -231,8 +216,8 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res)
fprintf(stderr, "n");
*/
- remain = idle->tbuf.len;
- pos = idle->tbuf.buf;
+ remain = server->idle_buf.len;
+ pos = server->idle_buf.buf;
r = read_varint(&pos, &remain, &mclen);
if (r <= 0 || mclen < 2 || mclen < remain) {
@@ -273,34 +258,16 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res)
*/
player_count = get_player_count(cfg, pos, remain);
- if (player_count < 0)
- goto out;
-
- idle->server->state = SERVER_STATE_RUNNING;
-
- debug(DBG_IDLE, "%s: currently %i active players",
- idle->server->name, player_count);
-
- if (player_count > 0)
- idle->server->idle_count = 0;
- else if (player_count == 0)
- idle->server->idle_count++;
-
- if (idle->server->idle_count > idle->server->idle_timeout) {
- verbose("stopping idle server %s", idle->server->name);
- server_stop(cfg, idle->server);
- }
out:
uring_task_close_fd(cfg, task);
+ server_set_active_players(cfg, server, player_count);
return;
}
static void
idle_check_handshake_sent(struct cfg *cfg, struct uring_task *task, int res)
{
- struct idle *idle = container_of(task, struct idle, idlecheck);
-
assert_return(cfg && task);
assert_task_alive(DBG_IDLE, task);
@@ -310,34 +277,22 @@ idle_check_handshake_sent(struct cfg *cfg, struct uring_task *task, int res)
return;
}
- uring_tbuf_read_until(cfg, &idle->idlecheck,
+ uring_tbuf_read_until(cfg, task,
idle_check_handshake_complete,
idle_check_handshake_reply);
}
-static void
-idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected)
+void
+idle_check_get_player_count(struct cfg *cfg, struct server *server,
+ struct connection *conn)
{
- struct idle *idle = container_of(conn, struct idle, conn);
char buf[1024];
char *pos;
- char *cmdbuf = idle->tbuf.buf;
+ char *cmdbuf = server->idle_buf.buf;
uint16_t port;
char hostname[INET6_ADDRSTRLEN];
- assert_return(cfg && conn);
- assert_task_alive(DBG_IDLE, &idle->idlecheck);
-
- if (!connected) {
- debug(DBG_IDLE,
- "idle check connection to remote server (%s) failed",
- idle->server->name);
- idle->server->idle_count = 0;
- idle->server->state = SERVER_STATE_STOPPED;
- return;
- }
-
- debug(DBG_IDLE, "connected to remote %s", idle->conn.remote.addrstr);
+ assert_return(cfg && server && conn && server->idle_task.priv);
port = saddr_port(&conn->remote);
saddr_addr(&conn->remote, hostname, sizeof(hostname));
@@ -355,16 +310,17 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected
write_byte(&pos, MC_GET_STATUS);
write_cmd(&cmdbuf, buf, pos);
- idle->tbuf.len = (cmdbuf - idle->tbuf.buf);
- debug(DBG_IDLE, "sending MC message (%zu bytes)", idle->tbuf.len);
+ server->idle_buf.len = (cmdbuf - server->idle_buf.buf);
+ debug(DBG_IDLE, "sending MC message (%zu bytes)", server->idle_buf.len);
- uring_tbuf_write(cfg, &idle->idlecheck, idle_check_handshake_sent);
+ uring_tbuf_write(cfg, &server->idle_task, idle_check_handshake_sent);
}
static void
idle_cb(struct cfg *cfg, struct uring_task *task, int res)
{
struct idle *idle = container_of(task, struct idle, task);
+ struct server *server;
assert_return(cfg && task);
assert_task_alive(DBG_IDLE, task);
@@ -376,11 +332,8 @@ idle_cb(struct cfg *cfg, struct uring_task *task, int res)
debug(DBG_IDLE, "timer fired (value: %" PRIu64 ")", idle->value);
- if (!list_empty(&idle->server->proxys))
- idle->server->idle_count = 0;
- else
- connect_any(cfg, &idle->idlecheck, &idle->server->remotes,
- &idle->conn, idle_check_connected_cb);
+ list_for_each_entry(server, &cfg->servers, list)
+ server_idle_check(cfg, server);
uring_read(cfg, &idle->task, &idle->value, sizeof(idle->value), idle_cb);
}
@@ -401,26 +354,21 @@ idle_refdump(struct idle *idle)
assert_return_silent(idle);
uring_task_refdump(&idle->task);
- uring_task_refdump(&idle->idlecheck);
}
void
-idle_delete(struct cfg *cfg, struct server *server)
+idle_delete(struct cfg *cfg)
{
- struct idle *idle;
+ assert_return(cfg);
+ assert_return_silent(cfg->idle);
- assert_return(cfg && server);
- assert_return_silent(server->idle);
-
- idle = server->idle;
- debug(DBG_IDLE, "closing fd %i", idle->task.fd);
- uring_task_destroy(cfg, &idle->idlecheck);
- uring_task_destroy(cfg, &idle->task);
- server->idle = NULL;
+ debug(DBG_IDLE, "closing fd %i", cfg->idle->task.fd);
+ uring_task_destroy(cfg, &cfg->idle->task);
+ cfg->idle = NULL;
}
void
-idle_init(struct cfg *cfg, struct server *server)
+idle_init(struct cfg *cfg)
{
struct idle *idle;
int ifd;
@@ -436,10 +384,7 @@ idle_init(struct cfg *cfg, struct server *server)
}
};
- assert_return(cfg && server);
-
- if (server->idle_timeout < 1)
- return;
+ assert_return(cfg);
idle = zmalloc(sizeof(*idle));
if (!idle)
@@ -452,12 +397,9 @@ idle_init(struct cfg *cfg, struct server *server)
if (timerfd_settime(ifd, 0, &tspec, NULL) != 0)
die("timerfd_settime: %m");
- uring_task_init(cfg, &idle->task, "idle", &server->task, idle_free);
+ uring_task_init(cfg, &idle->task, "idle", uring_parent(cfg), idle_free);
uring_task_set_fd(&idle->task, ifd);
- uring_task_init(cfg, &idle->idlecheck, "idlecheck", &idle->task, idle_check_free);
- uring_task_set_buf(&idle->idlecheck, &idle->tbuf);
- idle->server = server;
- server->idle = idle;
+ cfg->idle = idle;
uring_read(cfg, &idle->task, &idle->value, sizeof(idle->value), idle_cb);
}