Skip to content

Commit

Permalink
refactor!: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryAlien committed Jan 30, 2023
1 parent b103033 commit ae4e205
Show file tree
Hide file tree
Showing 23 changed files with 318 additions and 338 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ add_library(ssq STATIC
src/a2s/info.c
src/a2s/player.c
src/a2s/rules.c
src/buf.c
src/error.c
src/packet.c
src/query.c
src/response.c
src/server.c
src/stream.c
)
22 changes: 11 additions & 11 deletions example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,43 @@ int main(int argc, char *argv[]) {
if (server == NULL) {
fprintf(stderr, "ssq_server_new: memory exhausted\n");
exit(EXIT_FAILURE);
} else if (!ssq_server_ok(server)) {
fprintf(stderr, "ssq_server_new: %s: %s\n", hostname, ssq_server_err_msg(server));
} else if (!ssq_server_eok(server)) {
fprintf(stderr, "ssq_server_new: %s: %s\n", hostname, ssq_server_emsg(server));
ssq_server_free(server);
exit(EXIT_FAILURE);
}
ssq_server_timeout(server, SSQ_TIMEOUT_RECV | SSQ_TIMEOUT_SEND, 10000);

/* A2S_INFO */
A2S_INFO *info = ssq_info(server);
if (ssq_server_ok(server)) {
if (ssq_server_eok(server)) {
ssq_info_print(info);
ssq_info_free(info);
} else {
fprintf(stderr, "ssq_info: %s\n", ssq_server_err_msg(server));
ssq_server_err_clr(server);
fprintf(stderr, "ssq_info: %s\n", ssq_server_emsg(server));
ssq_server_eclr(server);
}

/* A2S_PLAYER */
uint8_t player_count = 0;
A2S_PLAYER *players = ssq_player(server, &player_count);
if (ssq_server_ok(server)) {
if (ssq_server_eok(server)) {
ssq_player_print(players, player_count);
ssq_player_free(players, player_count);
} else {
fprintf(stderr, "ssq_player: %s\n", ssq_server_err_msg(server));
ssq_server_err_clr(server);
fprintf(stderr, "ssq_player: %s\n", ssq_server_emsg(server));
ssq_server_eclr(server);
}

/* A2S_RULES */
uint16_t rule_count = 0;
A2S_RULES *rules = ssq_rules(server, &rule_count);
if (ssq_server_ok(server)) {
if (ssq_server_eok(server)) {
ssq_rules_print(rules, rule_count);
ssq_rules_free(rules, rule_count);
} else {
fprintf(stderr, "ssq_rules: %s\n", ssq_server_err_msg(server));
ssq_server_err_clr(server);
fprintf(stderr, "ssq_rules: %s\n", ssq_server_emsg(server));
ssq_server_eclr(server);
}

/* Cleanup */
Expand Down
2 changes: 2 additions & 0 deletions include/ssq/a2s/info.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** info.h -- A2S_INFO server queries. */

#ifndef SSQ_A2S_INFO_H
#define SSQ_A2S_INFO_H

Expand Down
2 changes: 2 additions & 0 deletions include/ssq/a2s/player.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* player.h -- A2S_PLAYER server queries. */

#ifndef SSQ_A2S_PLAYER_H
#define SSQ_A2S_PLAYER_H

Expand Down
2 changes: 2 additions & 0 deletions include/ssq/a2s/rules.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* rules.h -- A2S_RULES server queries. */

#ifndef SSQ_A2S_RULES_H
#define SSQ_A2S_RULES_H

Expand Down
47 changes: 0 additions & 47 deletions include/ssq/buf.h

This file was deleted.

14 changes: 8 additions & 6 deletions include/ssq/error.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* error.h -- Error information. */

#ifndef SSQ_ERROR_H
#define SSQ_ERROR_H

Expand All @@ -9,12 +11,12 @@ extern "C" {
#endif /* __cplusplus */

typedef enum ssq_error_code {
SSQE_OK = 0,
SSQE_SYSTEM = 1,
SSQE_INVALID_RESPONSE = 2,
SSQE_UNSUPPORTED = 3,
SSQE_GAI = 4,
SSQE_NO_SOCKET = 5,
SSQE_OK = 0,
SSQE_SYSTEM,
SSQE_INVALID_RESPONSE,
SSQE_UNSUPPORTED,
SSQE_GAI,
SSQE_NO_SOCKET,
} SSQ_ERROR_CODE;

typedef struct ssq_error {
Expand Down
10 changes: 6 additions & 4 deletions include/ssq/helper.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* helper.h -- Utility functions. */

#ifndef SSQ_HELPER_H
#define SSQ_HELPER_H

Expand All @@ -13,19 +15,19 @@
#define SSQ_PORT_LEN 5
#define SSQ_PORT_SIZE (SSQ_PORT_LEN + 1)

static inline size_t ssq_helper_minz(const size_t x, const size_t y) {
static inline size_t ssq_helper_minz(size_t x, size_t y) {
return (x < y) ? x : y;
}

static inline void ssq_helper_strncpy(char dest[], const char src[], const size_t len) {
static inline void ssq_helper_strncpy(char dest[], const char src[], size_t len) {
#ifdef _WIN32
strncpy_s(dest, len + 1, src, len);
#else /* !_WIN32 */
strncpy(dest, src, len);
#endif /* _WIN32 */
}

static inline int ssq_helper_port_to_str(const uint16_t port, char port_str[SSQ_PORT_SIZE]) {
static inline int ssq_helper_port_to_str(uint16_t port, char port_str[SSQ_PORT_SIZE]) {
#ifdef _WIN32
return sprintf_s(port_str, SSQ_PORT_SIZE, "%" PRIu16, port);
#else /* !_WIN32 */
Expand All @@ -34,7 +36,7 @@ static inline int ssq_helper_port_to_str(const uint16_t port, char port_str[SSQ_
}

#ifndef _WIN32
static inline void ssq_helper_fill_timeval(const time_t value_in_ms, struct timeval *const tv) {
static inline void ssq_helper_millis_to_timeval(time_t value_in_ms, struct timeval *tv) {
tv->tv_sec = value_in_ms / 1000;
tv->tv_usec = value_in_ms % 1000 * 1000;
}
Expand Down
5 changes: 4 additions & 1 deletion include/ssq/packet.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* packet.h -- Representation of a Source server packet. */

#ifndef SSQ_PACKET_H
#define SSQ_PACKET_H

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

#include "ssq/error.h"

#define SSQ_PACKET_SIZE 1400
Expand All @@ -30,7 +33,7 @@ typedef struct ssq_packet {
SSQ_PACKET *ssq_packet_from_datagram(const uint8_t *datagram, uint16_t datagram_len, SSQ_ERROR *err);
void ssq_packet_free(SSQ_PACKET *packet);

bool ssq_packets_check_integrity(const SSQ_PACKET *const *packets, const uint8_t packet_count);
bool ssq_packets_check_integrity(const SSQ_PACKET *const *packets, uint8_t packet_count);
uint8_t *ssq_packets_to_response(const SSQ_PACKET *const *packets, uint8_t packet_count, size_t *response_len, SSQ_ERROR *err);
void ssq_packets_free(SSQ_PACKET **packets, uint8_t packet_count);

Expand Down
2 changes: 2 additions & 0 deletions include/ssq/query.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* query.h -- Server query routine. */

#ifndef SSQ_QUERY_H
#define SSQ_QUERY_H

Expand Down
2 changes: 2 additions & 0 deletions include/ssq/response.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* response.h -- Query response utility. */

#ifndef SSQ_RESPONSE_H
#define SSQ_RESPONSE_H

Expand Down
8 changes: 4 additions & 4 deletions include/ssq/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ void ssq_server_timeout(SSQ_SERVER *server, SSQ_TIMEOUT_SELECTOR which
void ssq_server_timeout(SSQ_SERVER *server, SSQ_TIMEOUT_SELECTOR which, time_t value_in_ms);
#endif /* _WIN32 */

bool ssq_server_ok(const SSQ_SERVER *server);
SSQ_ERROR_CODE ssq_server_err_code(const SSQ_SERVER *server);
const char *ssq_server_err_msg(const SSQ_SERVER *server);
void ssq_server_err_clr(SSQ_SERVER *server);
bool ssq_server_eok(const SSQ_SERVER *server);
SSQ_ERROR_CODE ssq_server_ecode(const SSQ_SERVER *server);
const char *ssq_server_emsg(const SSQ_SERVER *server);
void ssq_server_eclr(SSQ_SERVER *server);

#ifdef __cplusplus
}
Expand Down
44 changes: 44 additions & 0 deletions include/ssq/stream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* stream.h -- Data input stream. */

#ifndef SSQ_STREAM_H
#define SSQ_STREAM_H

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

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

typedef struct ssq_stream {
const uint8_t *buf;
size_t size;
size_t pos;
} SSQ_STREAM;

void ssq_stream_wrap(SSQ_STREAM *stream, const void *buf, size_t size);

void ssq_stream_advance(SSQ_STREAM *stream, size_t n);
size_t ssq_stream_remaining(const SSQ_STREAM *stream);
bool ssq_stream_end(const SSQ_STREAM *stream);

void ssq_stream_read(SSQ_STREAM *src, void *dest, size_t n);
int8_t ssq_stream_read_int8_t(SSQ_STREAM *stream);
int16_t ssq_stream_read_int16_t(SSQ_STREAM *stream);
int32_t ssq_stream_read_int32_t(SSQ_STREAM *stream);
int64_t ssq_stream_read_int64_t(SSQ_STREAM *stream);
uint8_t ssq_stream_read_uint8_t(SSQ_STREAM *stream);
uint16_t ssq_stream_read_uint16_t(SSQ_STREAM *stream);
uint32_t ssq_stream_read_uint32_t(SSQ_STREAM *stream);
uint64_t ssq_stream_read_uint64_t(SSQ_STREAM *stream);
float ssq_stream_read_float(SSQ_STREAM *stream);
double ssq_stream_read_double(SSQ_STREAM *stream);
bool ssq_stream_read_bool(SSQ_STREAM *stream);
char *ssq_stream_read_string(SSQ_STREAM *stream, size_t *len);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* !SSQ_STREAM_H */
Loading

0 comments on commit ae4e205

Please sign in to comment.