summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--idle.c73
1 files changed, 14 insertions, 59 deletions
diff --git a/idle.c b/idle.c
index 808c25f..e1a73ea 100644
--- a/idle.c
+++ b/idle.c
@@ -15,15 +15,13 @@
#include "idle.h"
struct idle {
- uint64_t value;
struct server *server;
+
struct uring_task task;
+ uint64_t value;
struct uring_task idlecheck;
- unsigned next_remote;
- struct sockaddr_in46 remote;
- char remotestr[ADDRSTRLEN];
-
+ struct connection conn;
struct uring_task_buf tbuf;
};
@@ -282,25 +280,24 @@ idle_check_handshake_sent(struct cfg *cfg, struct uring_task *task, int res)
uring_tbuf_read_until(cfg, &idle->idlecheck, idle_check_handshake_complete, idle_check_handshake_reply);
}
-static void idle_check_connect_next_remote(struct cfg *cfg, struct idle *idle);
-
static void
-idle_check_remote_connected(struct cfg *cfg, struct uring_task *task, int res)
+idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected)
{
- struct idle *idle = container_of(task, struct idle, idlecheck);
+ struct idle *idle = container_of(conn, struct idle, conn);
char buf[1024];
char *pos;
char *cmdbuf = idle->tbuf.buf;
uint16_t port = 25565;
- /* FIXME: Write real remote addr and port */
- fprintf(stderr, "%s: connected %i\n", __func__, res);
- if (res < 0) {
- uring_task_close_fd(cfg, task);
- idle_check_connect_next_remote(cfg, idle);
+ if (!connected) {
+ fprintf(stderr, "%s: idle check connection to remote server failed\n",
+ idle->server->name);
+ idle->server->idle_count = 0;
return;
}
+ fprintf(stderr, "%s: connected to remote %s\n", __func__, idle->conn.remotestr);
+
pos = buf;
write_byte(&pos, MC_HELO);
write_varint(&pos, -1); /* Protocol version, -1 = undefined */
@@ -324,49 +321,6 @@ idle_check_remote_connected(struct cfg *cfg, struct uring_task *task, int res)
uring_tbuf_write(cfg, &idle->idlecheck, idle_check_handshake_sent);
}
-/* FIXME: Parts of this could be shared with proxy.c, probably in server.c */
-static void
-idle_check_connect_next_remote(struct cfg *cfg, struct idle *idle)
-{
- struct sockaddr_in46 *remote, *tmp;
- struct server *scfg = idle->server;
- int sfd;
- unsigned i = 0;
-
-again:
- remote = NULL;
- list_for_each_entry(tmp, &scfg->remotes, list) {
- if (i == idle->next_remote) {
- remote = tmp;
- break;
- }
- i++;
- }
-
- if (!remote) {
- fprintf(stderr, "No more remote addresses to attempt\n");
- /* FIXME: put tasks? */
- return;
- }
-
- idle->next_remote++;
- idle->remote = *remote;
- sockaddr_to_str(&idle->remote, idle->remotestr, sizeof(idle->remotestr));
- fprintf(stderr, "%s: attempting idle check on %s (len %u)\n",
- scfg->name, idle->remotestr, idle->remote.addrlen);
-
- sfd = socket(idle->remote.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (sfd < 0) {
- perror("socket");
- goto again;
- }
-
- socket_set_low_latency(cfg, sfd);
-
- uring_task_set_fd(&idle->idlecheck, sfd);
- uring_connect(cfg, &idle->idlecheck, &idle->remote, idle_check_remote_connected);
-}
-
static void
idle_cb(struct cfg *cfg, struct uring_task *task, int res)
{
@@ -384,11 +338,12 @@ idle_cb(struct cfg *cfg, struct uring_task *task, int res)
fprintf(stderr, "%s: called with value %" PRIu64 "\n", __func__, idle->value);
- idle->next_remote = 0;
if (!list_empty(&idle->server->proxys))
idle->server->idle_count = 0;
else
- idle_check_connect_next_remote(cfg, idle);
+ connect_any(cfg, &idle->idlecheck, &idle->server->remotes,
+ &idle->conn, idle_check_connected_cb);
+
uring_read(cfg, &idle->task, &idle->value, sizeof(idle->value), idle_cb);
}