Skip to content

Commit

Permalink
improve: organization and internal handling
Browse files Browse the repository at this point in the history
This commit improves the organization of the library and how it handles things internally.
  • Loading branch information
ThePedroo committed Apr 9, 2024
1 parent 508fc84 commit a218c53
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 209 deletions.
8 changes: 4 additions & 4 deletions external/jsonb.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ void pjsonb_set_string(struct pjsonb *builder, const char *key, const char *valu
builder->key_state = PJSONB_TO_CLOSE;
}

void pjson_enter_object(struct pjsonb *builder, const char *key) {
void pjsonb_enter_object(struct pjsonb *builder, const char *key) {
int key_length = strlen(key);

builder->string = realloc(builder->string, builder->position + 5 + key_length);
builder->position += snprintf(builder->string + builder->position, 5 + key_length, "\"%s\":{", key);
builder->key_state = PJSONB_NONE;
}

void pjson_leave_object(struct pjsonb *builder) {
void pjsonb_leave_object(struct pjsonb *builder) {
if (builder->key_state == PJSONB_TO_CLOSE) {
builder->string[builder->position - 1] = '}';
builder->string = realloc(builder->string, builder->position + 1);
Expand All @@ -119,15 +119,15 @@ void pjson_leave_object(struct pjsonb *builder) {
builder->key_state = PJSONB_TO_CLOSE;
}

void pjson_enter_array(struct pjsonb *builder, const char *key) {
void pjsonb_enter_array(struct pjsonb *builder, const char *key) {
int key_length = strlen(key);

builder->string = realloc(builder->string, builder->position + 3 + key_length);
builder->position += snprintf(builder->string + builder->position, 3 + key_length, "\"%s\":[", key);
builder->key_state = PJSONB_NONE;
}

void pjson_leave_array(struct pjsonb *builder) {
void pjsonb_leave_array(struct pjsonb *builder) {
if (builder->key_state == PJSONB_TO_CLOSE) {
builder->string[builder->position - 1] = ']';
} else {
Expand Down
8 changes: 4 additions & 4 deletions external/jsonb.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ void pjsonb_set_bool(struct pjsonb *builder, const char *key, int value);

void pjsonb_set_string(struct pjsonb *builder, const char *key, const char *value);

void pjson_enter_object(struct pjsonb *builder, const char *key);
void pjsonb_enter_object(struct pjsonb *builder, const char *key);

void pjson_leave_object(struct pjsonb *builder);
void pjsonb_leave_object(struct pjsonb *builder);

void pjson_enter_array(struct pjsonb *builder, const char *key);
void pjsonb_enter_array(struct pjsonb *builder, const char *key);

void pjson_leave_array(struct pjsonb *builder);
void pjsonb_leave_array(struct pjsonb *builder);

#endif
51 changes: 32 additions & 19 deletions include/codecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,53 @@

#define COGLINK_PARSE_ERROR -1

struct coglink_ready_payload {
struct coglink_ready {
char *session_id;
bool resumed;
};

#define COGLINK_READY 0

struct coglink_player_state_payload {
struct coglink_player_state {
int time;
int position;
bool connected;
int ping;
};

struct coglink_player_update_payload {
struct coglink_player_update {
u64snowflake guildId;
struct coglink_player_state_payload *state;
struct coglink_player_state *state;
};

#define COGLINK_PLAYER_UPDATE 2

struct coglink_stats_memory_payload {
struct coglink_stats_memory {
int free;
int used;
int allocated;
int reservable;
};

struct coglink_stats_cpu_payload {
struct coglink_stats_cpu {
int cores;
int systemLoad;
int lavalinkLoad;
};

struct coglink_stats_frame_stats_payload {
struct coglink_stats_frame_stats {
int sent;
int nulled;
int deficit;
};

struct coglink_stats_payload {
struct coglink_stats {
int players;
int playingPlayers;
int uptime;
struct coglink_stats_memory_payload *memory;
struct coglink_stats_cpu_payload *cpu;
struct coglink_stats_frame_stats_payload *frameStats;
struct coglink_stats_memory *memory;
struct coglink_stats_cpu *cpu;
struct coglink_stats_frame_stats *frameStats;
};

#define COGLINK_STATS 3
Expand Down Expand Up @@ -84,7 +84,7 @@ struct coglink_tracks {
size_t size;
};

struct coglink_track_start_payload {
struct coglink_track_start {
u64snowflake guildId;
struct coglink_track *track;
};
Expand All @@ -99,7 +99,7 @@ enum coglink_track_end_reason {
COGLINK_TRACK_END_REASON_CLEANUP
};

struct coglink_track_end_payload {
struct coglink_track_end {
u64snowflake guildId;
struct coglink_track *track;
enum coglink_track_end_reason reason;
Expand All @@ -113,29 +113,29 @@ enum coglink_exception_severity {
COGLINK_EXCEPTION_SEVERITY_FAULT
};

struct coglink_exception_payload {
struct coglink_exception {
char *message;
enum coglink_exception_severity severity;
char *cause;
};

struct coglink_track_exception_payload {
struct coglink_track_exception {
u64snowflake guildId;
struct coglink_track *track;
struct coglink_exception_payload *exception;
struct coglink_exception *exception;
};

#define COGLINK_TRACK_EXCEPTION 43

struct coglink_track_stuck_payload {
struct coglink_track_stuck {
u64snowflake guildId;
struct coglink_track *track;
int thresholdMs;
};

#define COGLINK_TRACK_STUCK 44

struct coglink_websocket_closed_payload {
struct coglink_websocket_closed {
int code;
char *reason;
bool byRemote;
Expand Down Expand Up @@ -283,6 +283,13 @@ struct coglink_node_info {
struct coglink_node_info_filters *filters;
};

struct coglink_node_version {
int major;
int minor;
int patch;
char *preRelease;
};

#define coglink_parse_track(track_info, pairs, json) \
char *path[] = { "encoded", NULL }; \
FIND_FIELD_PATH(json, pairs, encoded, "encoded", 1); \
Expand Down Expand Up @@ -340,6 +347,8 @@ int coglink_parse_websocket_data(const char *json, size_t json_length, void **re

void coglink_free_track(struct coglink_track *track);

void coglink_free_tracks(struct coglink_tracks *tracks);

int coglink_parse_load_tracks(struct coglink_load_tracks *response, const char *json, size_t json_length);

void coglink_free_load_tracks(struct coglink_load_tracks *response);
Expand All @@ -366,6 +375,10 @@ int coglink_parse_node_info(struct coglink_node_info *response, const char *json

void coglink_free_node_info(struct coglink_node_info *node_info);

int coglink_parse_stats(struct coglink_stats_payload *response, const char *json, size_t json_length);
int coglink_parse_stats(struct coglink_stats *response, const char *json, size_t json_length);

int coglink_parse_version(struct coglink_node_version *response, const char *version, size_t version_length);

void coglink_free_node_version(struct coglink_node_version *version);

#endif
16 changes: 8 additions & 8 deletions include/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

struct coglink_events {
int (*on_raw)(struct coglink_client *c_client, const char *data, size_t length);
void (*on_ready)(struct coglink_ready_payload *ready);
void (*on_ready)(struct coglink_ready *ready);
void (*on_close)(enum ws_close_reason wscode, const char *reason);
void (*on_track_start)(struct coglink_track_start_payload *trackStart);
void (*on_track_end)(struct coglink_track_end_payload *trackEnd);
void (*on_track_excetion)(struct coglink_track_exception_payload *trackException);
void (*on_track_stuck)(struct coglink_track_stuck_payload *trackStuck);
void (*on_websocket_closed)(struct coglink_websocket_closed_payload *websocketClosed);
void (*on_player_update)(struct coglink_player_update_payload *playerUpdate);
void (*on_stats)(struct coglink_stats_payload *stats);
void (*on_track_start)(struct coglink_track_start *trackStart);
void (*on_track_end)(struct coglink_track_end *trackEnd);
void (*on_track_excetion)(struct coglink_track_exception *trackException);
void (*on_track_stuck)(struct coglink_track_stuck *trackStuck);
void (*on_websocket_closed)(struct coglink_websocket_closed *websocketClosed);
void (*on_player_update)(struct coglink_player_update *playerUpdate);
void (*on_stats)(struct coglink_stats *stats);
};

#endif /* COGLINK_LAVALINK_EVENTS_H */
2 changes: 1 addition & 1 deletion include/lavalink.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct coglink_node {
bool ssl;
/* Public info */
char *session_id;
struct coglink_stats_payload *stats;
struct coglink_stats *stats;
/* Internal */
uint64_t tstamp;
CURLM *mhandle;
Expand Down
16 changes: 7 additions & 9 deletions include/rest.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ struct coglink_update_player_params {

struct coglink_user *coglink_get_user(struct coglink_client *c_client, u64snowflake user_id);

int coglink_join_voice_channel(struct discord *client, u64snowflake guild_id, u64snowflake channel_id);
int coglink_join_voice_channel(struct coglink_client *c_client, struct discord *client, u64snowflake guild_id, u64snowflake channel_id);

int coglink_leave_voice_channel(struct coglink_client *c_client, struct discord *client, u64snowflake guild_id);

struct coglink_player *coglink_create_player(struct coglink_client *c_client, u64snowflake guild_id);

Expand All @@ -140,24 +142,20 @@ int coglink_remove_track_from_queue(struct coglink_client *c_client, struct cogl

int coglink_remove_player(struct coglink_client *c_client, struct coglink_player *player);

int coglink_decode_track(struct coglink_client *c_client, struct coglink_node *node, char *track, struct coglink_track *response);
int coglink_load_tracks(struct coglink_client *c_client, struct coglink_node *node, char *identifier, struct coglink_load_tracks *response);

void coglink_free_decode_track(struct coglink_track *track);
int coglink_decode_track(struct coglink_client *c_client, struct coglink_node *node, char *track, struct coglink_track *response);

int coglink_decode_tracks(struct coglink_client *c_client, struct coglink_node *node, struct coglink_decode_tracks_params *params, struct coglink_tracks *response);

void coglink_free_decode_tracks(struct coglink_tracks *track);

int coglink_update_player(struct coglink_client *c_client, struct coglink_player *player, struct coglink_update_player_params *params, struct coglink_update_player *response);

void coglink_destroy_player(struct coglink_client *c_client, struct coglink_player *player);

int coglink_get_node_info(struct coglink_client *c_client, struct coglink_node *node, struct coglink_node_info *info);

int coglink_get_node_version(struct coglink_client *c_client, struct coglink_node *node, char **version);

void coglink_free_node_version(char *version);
int coglink_get_node_version(struct coglink_client *c_client, struct coglink_node *node, struct coglink_node_version *version);

int coglink_get_stats(struct coglink_client *c_client, struct coglink_node *node, struct coglink_stats_payload *stats);
int coglink_get_stats(struct coglink_client *c_client, struct coglink_node *node, struct coglink_stats *stats);

#endif
8 changes: 5 additions & 3 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@

#define PAIR_TO_D_STRING(json, pair, output) \
output = malloc((pair->v.len + 1) * sizeof(char)); \
memcpy(output, json + pair->v.pos, pair->v.len); \
output[pair->v.len] = '\0';
snprintf(output, pair->v.len + 1, "%.*s", (int)pair->v.len, json + pair->v.pos);

#define FREE_NULLABLE(ptr) \
if (ptr != NULL) free(ptr);

#ifdef COGLINK_DEBUG
#define FATAL(...) \
Expand Down Expand Up @@ -67,6 +69,6 @@ struct coglink_response {
size_t size;
};

int _coglink_perform_request(struct coglink_node *nodeInfo, struct coglink_request_params *req, struct coglink_response *res);
int _coglink_perform_request(struct coglink_node *node_info, struct coglink_request_params *req, struct coglink_response *res);

#endif
Loading

0 comments on commit a218c53

Please sign in to comment.