diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-12-08 16:31:24 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-12-08 17:00:23 +0100 |
| commit | 4da7a3e44d2bbd7b21ae05c7b6604748e7227227 (patch) | |
| tree | cbb81eed24dd2bed75998e4f8ffb803dd479a6df /src/transport | |
| parent | de3532bd6b685c66015202a48d53e1b8fa4900f5 (diff) | |
| download | jancity-4da7a3e44d2bbd7b21ae05c7b6604748e7227227.tar.gz | |
wip2
Diffstat (limited to 'src/transport')
| -rw-r--r-- | src/transport/src/heap.c | 6 | ||||
| -rw-r--r-- | src/transport/src/transport.c | 34 | ||||
| -rw-r--r-- | src/transport/test/test.c | 16 |
3 files changed, 22 insertions, 34 deletions
diff --git a/src/transport/src/heap.c b/src/transport/src/heap.c index cf43426..bcf0d6f 100644 --- a/src/transport/src/heap.c +++ b/src/transport/src/heap.c @@ -74,12 +74,12 @@ int transport_pop(struct transport_handle *const h) if (!h->n_packets) return -1; - union transport_packet *const p = h->packets[h->n_packets - 1]; + union transport_packet *const p = h->packets[--h->n_packets]; - if (--h->n_packets) + if (h->n_packets) { if (!(h->packets = realloc(h->packets, - (h->n_packets - 1) * sizeof *h->packets))) + h->n_packets * sizeof *h->packets))) return -1; } else diff --git a/src/transport/src/transport.c b/src/transport/src/transport.c index fe2d54f..52093d4 100644 --- a/src/transport/src/transport.c +++ b/src/transport/src/transport.c @@ -34,7 +34,7 @@ int transport_disconnect(struct transport_handle *const h) int transport_send(struct transport_handle *const h, const void *const buf, size_t n) { - const char *b = buf; + const uint8_t *b = buf; while (n) { @@ -98,7 +98,7 @@ static int read_header(const struct transport_cfg *const cfg, if (header >= MAX_TRANSPORT_PACKET_TYPES) { - fprintf(stderr, "%s: invalid header %" PRIu8 "\n", + fprintf(stderr, "%s: invalid header %#" PRIx8 "\n", __func__, header); return -1; } @@ -239,7 +239,8 @@ static int read_checksum(const struct transport_cfg *const cfg, if (checksum != expected) { - fprintf(stderr, "%s: invalid checksum %#" PRIx8 ", expected %#" PRIx8, + fprintf(stderr, "%s: invalid checksum %#" PRIx8 + ", expected %#" PRIx8 "\n", __func__, checksum, expected); return -1; } @@ -319,25 +320,15 @@ static void send_event(const struct transport_cfg *const cfg, } static int remove_packet(struct transport_handle *const h, - union transport_packet *const p) + union transport_packet **const pp) { - for (size_t i = 0; i < h->n_packets; i++) - { - union transport_packet **pp = &h->packets[i]; + size_t i = pp - h->packets; + const size_t n = i + 1; - if (*pp == p) - { - if (i + 1 < h->n_packets) - for (union transport_packet **pr = &h->packets[i + 1]; - pr - h->packets < h->n_packets; - pp++, pr++) - *pp = *pr; + if (n < h->n_packets) + memmove(pp, pp + 1, h->n_packets - n); - return transport_pop(h); - } - } - - return -1; + return transport_pop(h); } static void get_event(const struct transport_cfg *const cfg, @@ -367,13 +358,14 @@ static int process_ack(struct transport_handle *const h, { for (size_t i = 0; i < h->n_packets; i++) { - union transport_packet *const p = h->packets[i]; + union transport_packet **const pp = &h->packets[i]; + const union transport_packet *const p = *pp; if (p->common.ttl && a->checksum == calc_checksum(p)) { get_event(&h->cfg, p, &h->input.ev); - if (remove_packet(h, p)) + if (remove_packet(h, pp)) return -1; break; diff --git a/src/transport/test/test.c b/src/transport/test/test.c index 2bb8e7f..971f849 100644 --- a/src/transport/test/test.c +++ b/src/transport/test/test.c @@ -103,29 +103,25 @@ static void server_received(const struct transport_event *const ev, static int loop(struct server *const s, struct client *const c) { - if (transport_update(&s->h)) - return -1; - - if (!freopen("client", "w+b", s->io.in)) + if (transport_update(&s->h) + || !freopen("client", "w+b", s->io.in)) return -1; rewind(s->io.out); - if (transport_update(&c->h)) - return -1; - - if (!freopen("server", "w+b", c->io.in)) + if (transport_update(&c->h) + || !freopen("server", "w+b", c->io.in)) return -1; rewind(c->io.out); - return 0; } int main(void) { int ret = EXIT_FAILURE; - FILE *const cf = fopen("client", "w+b"), *sf = fopen("server", "w+b"); + FILE *const cf = fopen("client", "w+b"), + *const sf = fopen("server", "w+b"); if (!cf) { |
