#ifndef TRANSPORT_PRIVATE_H
#define TRANSPORT_PRIVATE_H

#include <transport.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif

typedef uint8_t transport_checksum;

union transport_packet
{
    struct transport_common
    {
        enum transport_packet_type
        {
            TRANSPORT_PACKET_TYPE_CONNECT,
            TRANSPORT_PACKET_TYPE_DISCONNECT,
            TRANSPORT_PACKET_TYPE_ACK,
            TRANSPORT_PACKET_TYPE_NACK,
            TRANSPORT_PACKET_TYPE_DATA,

            MAX_TRANSPORT_PACKET_TYPES
        } type;

        enum transport_state
        {
            TRANSPORT_STATE_HEADER,
            TRANSPORT_STATE_LEN,
            TRANSPORT_STATE_BODY,
            TRANSPORT_STATE_CHECKSUM
        } state;

        bool done;
        unsigned ttl;
    } common;

    struct transport_packet_data
    {
        struct transport_common common;
        uint8_t buf[TRANSPORT_MAX_DATA_LEN];
        size_t n, written;
    } data;

    struct transport_packet_ack
    {
        struct transport_common common;
        transport_checksum checksum;
    } ack;
};

union transport_packet *transport_packet_alloc(enum transport_packet_type t);
void transport_packet_free(union transport_packet *p);
int transport_append(struct transport_handle *h, union transport_packet *p);
int transport_pop(struct transport_handle *h);

#ifdef __cplusplus
}
#endif

#endif /* TRANSPORT_PRIVATE_H */
