62 lines
1.9 KiB
C
62 lines
1.9 KiB
C
#ifndef OMNI_PROTOCOL_H
|
|
#define OMNI_PROTOCOL_H
|
|
|
|
#include "omni_common.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef enum message_type {
|
|
MSG_TYPE_TEXT = 0,
|
|
MSG_TYPE_FILE = 1,
|
|
MSG_TYPE_REGISTER = 2,
|
|
MSG_TYPE_ERROR = 3,
|
|
MSG_TYPE_INVALID = 255
|
|
} message_type_t;
|
|
|
|
#define SERVER_PEER_ID "server"
|
|
|
|
typedef struct message {
|
|
message_type_t type;
|
|
uint64_t id;
|
|
char from[OMNI_MAX_PEER_ID];
|
|
char to[OMNI_MAX_PEER_ID];
|
|
char file_name[OMNI_MAX_FILE_NAME];
|
|
uint8_t *body;
|
|
size_t body_len;
|
|
} message_t;
|
|
|
|
typedef struct protocol_frame_decoder {
|
|
uint8_t *buffer;
|
|
size_t len;
|
|
size_t cap;
|
|
} protocol_frame_decoder_t;
|
|
|
|
const char *protocol_message_type_name(message_type_t type);
|
|
int protocol_message_type_from_name(const char *raw, message_type_t *out);
|
|
|
|
void protocol_message_init(message_t *msg);
|
|
void protocol_message_clear(message_t *msg);
|
|
int protocol_message_copy(message_t *dst, const message_t *src);
|
|
|
|
int protocol_validate_message(const message_t *msg, char *err, size_t err_len);
|
|
|
|
int protocol_encode_message_datagram(const message_t *msg, uint8_t **out, size_t *out_len);
|
|
int protocol_decode_message_datagram(const uint8_t *data, size_t data_len, message_t *out_msg, char *err, size_t err_len);
|
|
|
|
int protocol_encode_message_stream(const message_t *msg, uint8_t **out, size_t *out_len);
|
|
int protocol_decode_message_stream_payload(const uint8_t *payload, size_t payload_len, message_t *out_msg, char *err, size_t err_len);
|
|
|
|
void protocol_frame_decoder_init(protocol_frame_decoder_t *decoder);
|
|
void protocol_frame_decoder_reset(protocol_frame_decoder_t *decoder);
|
|
void protocol_frame_decoder_destroy(protocol_frame_decoder_t *decoder);
|
|
int protocol_frame_decoder_feed(protocol_frame_decoder_t *decoder, const uint8_t *data, size_t data_len);
|
|
int protocol_frame_decoder_next(protocol_frame_decoder_t *decoder, uint8_t **payload, size_t *payload_len);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|