summaryrefslogtreecommitdiff
path: root/minecctl/rcon-commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'minecctl/rcon-commands.c')
-rw-r--r--minecctl/rcon-commands.c36
1 files changed, 13 insertions, 23 deletions
diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c
index 44bb286..cb6ea45 100644
--- a/minecctl/rcon-commands.c
+++ b/minecctl/rcon-commands.c
@@ -21,18 +21,13 @@ static void send_packet(int sfd, const char *buf, size_t len)
size_t off = 0;
ssize_t r;
- while (true) {
+ for (off = 0; off < len; off += r) {
r = write(sfd, buf + off, len - off);
if (r < 0) {
- if (errno == EINTR)
+ if (errno == EAGAIN || errno == EINTR)
continue;
- else
- die("Failed to write packet: %m");
+ die("Failed to write packet: %m");
}
-
- off += r;
- if (off == len)
- break;
}
}
@@ -40,31 +35,26 @@ static void send_packet(int sfd, const char *buf, size_t len)
static void read_packet(int sfd, char *buf, size_t len, int32_t *id,
int32_t *type, const char **msg)
{
- size_t off = 0;
+ size_t off;
ssize_t r;
const char *error;
- while (true) {
+ for (off = 0; off < len; off += r) {
+ if (rcon_protocol_packet_complete(buf, off))
+ break;
+
r = read(sfd, buf + off, len - off);
if (r < 0) {
if (errno == EINTR)
continue;
- else
- die("Failed to read reply: %m");
- }
-
- if (r == 0)
+ die("Failed to read reply: %m");
+ } else if (r == 0)
die("Failed, connection closed");
-
- off += r;
- if (rcon_protocol_packet_complete(buf, off))
- break;
-
- if (off >= len)
- die("Reply too large %zu and %zu", off, len);
}
- if (!rcon_protocol_read_packet(buf, off, id, type, msg, &error))
+ if (off >= len)
+ die("Reply too large %zu and %zu", off, len);
+ else if (!rcon_protocol_read_packet(buf, off, id, type, msg, &error))
die("Failed to parse response: %s", error);
}