Program Listing for File event.h

Return to documentation for file (server/event.h)

#ifndef EVENT_H_
    #define EVENT_H_

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

    #include "utils/debug.h"

static constexpr const int COMMAND_WORD_COUNT = 5;

constexpr const int CLIENT_DEAD = -2;

bool command_split(char *buff, char *argv[static COMMAND_WORD_COUNT],
    size_t command_len);

typedef struct {
    uint64_t timestamp; // Timestamp of the event in milliseconds
    int client_idx;
    int client_id;
    uint8_t arg_count;
    union {
        char *command[COMMAND_WORD_COUNT]; // Command words for the event
        char *action[COMMAND_WORD_COUNT]; // Action words for the event
    };
} event_t;

typedef struct {
    event_t *buff; // Pointer to the array of events
    size_t nmemb; // Number of events currently in the heap
    size_t capacity; // Maximum number of events the heap can hold
} event_heap_t;

bool event_heap_init(event_heap_t *heap);
void event_heap_free(event_heap_t *heap);
bool event_heap_push(event_heap_t *heap, const event_t *event);
event_t event_heap_pop(event_heap_t *heap);

static inline
bool event_heap_is_empty(const event_heap_t *heap)
{
    return heap->nmemb == 0;
}

static inline
const event_t *event_heap_peek(const event_heap_t *heap)
{
    if (heap->nmemb == 0) {
        DEBUG_MSG("Event heap is empty, Can't peek");
        return nullptr;
    }
    return &heap->buff[0];
}

typedef struct client_state_s client_state_t;
typedef struct server_s server_t;

client_state_t *event_get_client(server_t *srv, event_t const *event);

#endif /* EVENT_H_ */