diff options
Diffstat (limited to 'minecctl/mc-commands.c')
-rw-r--r-- | minecctl/mc-commands.c | 31 |
1 files changed, 11 insertions, 20 deletions
diff --git a/minecctl/mc-commands.c b/minecctl/mc-commands.c index 7d44451..c65f166 100644 --- a/minecctl/mc-commands.c +++ b/minecctl/mc-commands.c @@ -8,32 +8,23 @@ #include "misc.h" #include "shared/mc-protocol.h" -bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) +bool do_mc_pcount(struct cfg *cfg, struct server *server, unsigned *online, + unsigned *max, const char **error) { - struct server *server; struct saddr *saddr; - const char *error; char buf[4096]; size_t plen, off; ssize_t r; bool rv = false; int fd; - server = server_get_default(cfg); - if (!server) { - error("failed to get default server"); - return false; - } - fd = connect_any(&server->scfg.remotes, &saddr, &error); - if (fd < 0) { - error("%s: unable to connect - %s", server->name, error); + if (fd < 0) return false; - } if (!mc_protocol_create_status_request(buf, sizeof(buf), &plen, saddr)) { - error("Failed to create req"); + *error = "failed to create request"; goto out; } @@ -42,7 +33,7 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) while (off < plen) { r = write(fd, buf + off, plen - off); if (r <= 0) { - error("write failed: %zi (%m)", r); + *error = "write failed"; goto out; } off += r; @@ -52,23 +43,23 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) while (off < sizeof(buf)) { r = read(fd, buf + off, sizeof(buf) - off); if (r <= 0) { - error("Read failed %zi: %m", r); + *error = "read failed"; goto out; } off += r; - if (mc_is_handshake_complete(buf, off)) { - rv = true; + if (mc_is_handshake_complete(buf, off)) break; - } } if (!mc_protocol_parse_status_reply(buf, off, online, max)) { - error("Failed to get player count"); - return false; + *error = "failed to get player count"; + goto out; } + rv = true; + out: close(fd); return rv; |