commit 31bd4a0eb11f8f889a17a1ba0f005362a7e5b2b8
parent c9f28e3c5f003fe79e63695e9503ae8aafaa8071
Author: MikoĊaj Lenczewski <mblenczewski@gmail.com>
Date: Sun, 28 Apr 2024 15:15:21 +0000
Move agents to new protocol definitions
Diffstat:
8 files changed, 102 insertions(+), 41 deletions(-)
diff --git a/agents/example_c_agent/agent.c b/agents/example_c_agent/agent.c
@@ -4,8 +4,7 @@
#define _XOPEN_SOURCE 700
-#include "hex/types.h"
-#include "hex/proto.h"
+#include "hex.h"
#include <assert.h>
#include <errno.h>
@@ -21,6 +20,8 @@
#include <sys/types.h>
#include <unistd.h>
+#define ARRLEN(arr) (sizeof (arr) / sizeof (arr)[0])
+
int
net_init(char *restrict host, char *restrict port);
@@ -37,14 +38,14 @@ enum board_cell {
};
struct move {
- u32 x, y;
+ uint32_t x, y;
};
void
move_swap(struct move *restrict lhs, struct move *restrict rhs);
struct board {
- u32 size;
+ uint32_t size;
enum board_cell *cells;
@@ -53,16 +54,16 @@ struct board {
};
bool
-board_init(struct board *self, u32 size);
+board_init(struct board *self, uint32_t size);
bool
-board_play(struct board *self, enum hex_player player, u32 x, u32 y);
+board_play(struct board *self, enum hex_player player, uint32_t x, uint32_t y);
void
board_swap(struct board *self);
bool
-board_next(struct board *self, u32 *out_x, u32 *out_y);
+board_next(struct board *self, uint32_t *out_x, uint32_t *out_y);
enum game_state {
GAME_START,
@@ -97,7 +98,7 @@ main(int argc, char **argv)
enum hex_player opponent = HEX_PLAYER_WHITE;
enum hex_player winner = HEX_PLAYER_BLACK;
- u32 game_secs, thread_limit, mem_limit_mib; // currently unused
+ uint32_t game_secs, thread_limit, mem_limit_mib; // currently unused
(void) game_secs; (void) thread_limit; (void) mem_limit_mib;
bool game_over = false, first_round = true;
@@ -121,7 +122,7 @@ main(int argc, char **argv)
thread_limit = msg.data.start.thread_limit;
mem_limit_mib = msg.data.start.mem_limit_mib;
- u32 board_size = msg.data.start.board_size;
+ uint32_t board_size = msg.data.start.board_size;
if (!board_init(&board, board_size)) {
fprintf(stderr, "Failed to allocate game board of size %" PRIu32 "x%" PRIu32 "\n",
@@ -257,7 +258,7 @@ net_init(char *restrict host, char *restrict port)
}
static inline size_t
-net_recv_all(int sock, u8 *buf, size_t len)
+net_recv_all(int sock, uint8_t *buf, size_t len)
{
assert(buf);
@@ -278,7 +279,7 @@ net_recv_msg(int sock, struct hex_msg *out, enum hex_msg_type expected[], size_t
assert(out);
assert(expected);
- u8 buf[HEX_MSG_SZ];
+ uint8_t buf[HEX_MSG_SZ];
if (!(net_recv_all(sock, buf, HEX_MSG_SZ) == HEX_MSG_SZ)) return false;
struct hex_msg msg;
@@ -295,7 +296,7 @@ net_recv_msg(int sock, struct hex_msg *out, enum hex_msg_type expected[], size_t
}
static inline size_t
-net_send_all(int sock, u8 *buf, size_t len)
+net_send_all(int sock, uint8_t *buf, size_t len)
{
assert(buf);
@@ -315,7 +316,7 @@ net_send_msg(int sock, struct hex_msg *msg)
{
assert(msg);
- u8 buf[HEX_MSG_SZ];
+ uint8_t buf[HEX_MSG_SZ];
if (!hex_msg_try_serialise(msg, buf)) return false;
return net_send_all(sock, buf, HEX_MSG_SZ) == HEX_MSG_SZ;
@@ -342,7 +343,7 @@ shuffle_moves(struct move *arr, size_t len)
}
bool
-board_init(struct board *self, u32 size)
+board_init(struct board *self, uint32_t size)
{
assert(self);
@@ -374,7 +375,7 @@ board_init(struct board *self, u32 size)
}
bool
-board_play(struct board *self, enum hex_player player, u32 x, u32 y)
+board_play(struct board *self, enum hex_player player, uint32_t x, uint32_t y)
{
assert(self);
@@ -437,7 +438,7 @@ board_swap(struct board *self)
}
bool
-board_next(struct board *self, u32 *out_x, u32 *out_y)
+board_next(struct board *self, uint32_t *out_x, uint32_t *out_y)
{
assert(self);
assert(out_x);
@@ -452,11 +453,11 @@ board_next(struct board *self, u32 *out_x, u32 *out_y)
return true;
}
-extern inline b32
-hex_msg_try_serialise(struct hex_msg const *msg, u8 out[static HEX_MSG_SZ]);
+extern inline bool
+hex_msg_try_serialise(struct hex_msg const *msg, uint8_t out[static HEX_MSG_SZ]);
-extern inline b32
-hex_msg_try_deserialise(u8 buf[static HEX_MSG_SZ], struct hex_msg *out);
+extern inline bool
+hex_msg_try_deserialise(uint8_t buf[static HEX_MSG_SZ], struct hex_msg *out);
extern inline char const *
hexplayerstr(enum hex_player val);
diff --git a/agents/example_c_agent/build.sh b/agents/example_c_agent/build.sh
@@ -5,7 +5,7 @@ CC="${CC:-cc}"
WARNINGS="-Wall -Wextra -Wpedantic -Werror"
CFLAGS="-std=c11 -Og -g"
-CPPFLAGS="-UNDEBUG -Iinclude -I../../server/include"
+CPPFLAGS="-UNDEBUG -Iinclude -I../../proto"
LDFLAGS=""
TARGET="agent"
diff --git a/agents/example_cpp_agent/agent.cpp b/agents/example_cpp_agent/agent.cpp
@@ -4,8 +4,7 @@
#define _XOPEN_SOURCE 700
-#include "hex/types.h"
-#include "hex/proto.h"
+#include "hex.h"
#include <cassert>
#include <cerrno>
@@ -27,6 +26,8 @@
#include <random>
#include <vector>
+#define ARRLEN(arr) (sizeof (arr) / sizeof (arr)[0])
+
enum class Cell {
BLACK = HEX_PLAYER_BLACK,
WHITE = HEX_PLAYER_WHITE,
@@ -34,7 +35,7 @@ enum class Cell {
};
struct Move {
- u32 x, y;
+ uint32_t x, y;
bool operator==(const Move &rhs) {
return this->x == rhs.x && this->y == rhs.y;
@@ -47,18 +48,18 @@ void swap(Move &lhs, Move &rhs) {
}
class Board {
- u32 size;
+ uint32_t size;
std::vector<Cell> cells;
std::vector<Move> moves;
public:
template <class URBG>
- Board(u32 size, URBG &&rng) : size(size) {
+ Board(uint32_t size, URBG &&rng) : size(size) {
cells.reserve(size * size);
moves.reserve(size * size);
- for (u32 j = 0; j < this->size; j++) {
- for (u32 i = 0; i < this->size; i++) {
+ for (uint32_t j = 0; j < this->size; j++) {
+ for (uint32_t i = 0; i < this->size; i++) {
this->cells.push_back(Cell::EMPTY);
Move move{i, j};
@@ -69,7 +70,7 @@ public:
std::shuffle(this->moves.begin(), this->moves.end(), rng);
}
- bool play(enum hex_player player, u32 x, u32 y) {
+ bool play(enum hex_player player, uint32_t x, uint32_t y) {
Cell &cell = this->cells.at(y * this->size + x);
if (cell != Cell::EMPTY) return false;
@@ -97,8 +98,8 @@ public:
void swap(URBG &&rng) {
this->moves.clear();
- for (u32 j = 0; j < this->size; j++) {
- for (u32 i = 0; i < this->size; i++) {
+ for (uint32_t j = 0; j < this->size; j++) {
+ for (uint32_t i = 0; i < this->size; i++) {
Cell &cell = this->cells.at(j * this->size + i);
switch (cell) {
@@ -164,7 +165,7 @@ public:
}
bool recv_msg(struct hex_msg &out, const std::vector<enum hex_msg_type> &expected) {
- u8 buf[HEX_MSG_SZ];
+ uint8_t buf[HEX_MSG_SZ];
size_t nbytes_recv = 0, len = HEX_MSG_SZ;
@@ -186,7 +187,7 @@ public:
}
bool send_msg(const struct hex_msg &msg) {
- u8 buf[HEX_MSG_SZ];
+ uint8_t buf[HEX_MSG_SZ];
if (!hex_msg_try_serialise(&msg, buf)) return false;
size_t nbytes_sent = 0, len = HEX_MSG_SZ;
@@ -240,7 +241,7 @@ main(int argc, char *argv[])
enum hex_player winner = HEX_PLAYER_BLACK;
// game parameters (unused)
- u32 game_secs, thread_limit, mem_limit_mib;
+ uint32_t game_secs, thread_limit, mem_limit_mib;
(void) game_secs; (void) thread_limit; (void) mem_limit_mib;
bool game_over = false, first_round = true;
@@ -261,7 +262,7 @@ main(int argc, char *argv[])
thread_limit = msg.data.start.thread_limit;
mem_limit_mib = msg.data.start.mem_limit_mib;
- u32 board_size = msg.data.start.board_size;
+ uint32_t board_size = msg.data.start.board_size;
board = std::make_unique<Board>(board_size, rand);
diff --git a/agents/example_cpp_agent/build.sh b/agents/example_cpp_agent/build.sh
@@ -5,7 +5,7 @@ CXX="${CXX:-c++}"
WARNINGS="-Wall -Wextra -Wpedantic -Werror"
CFLAGS="-std=c++14 -Og -g"
-CPPFLAGS="-UNDEBUG -Iinclude -I../../server/include"
+CPPFLAGS="-UNDEBUG -Iinclude -I../../proto"
LDFLAGS=""
TARGET="agent"
diff --git a/agents/hexes/build.sh b/agents/hexes/build.sh
@@ -5,7 +5,7 @@ CC="${CC:-cc}"
WARNINGS="-Wall -Wextra -Wpedantic -Werror"
CFLAGS="-std=c11 -Og -g"
-CPPFLAGS="-UNDEBUG -Iinclude -I../../server/include"
+CPPFLAGS="-UNDEBUG -Iinclude -I../../proto"
LDFLAGS=""
TARGET="hexes"
diff --git a/agents/hexes/include/hexes.h b/agents/hexes/include/hexes.h
@@ -15,8 +15,7 @@
#define _DEFAULT_SOURCE 1
#endif
-#include "hex/types.h"
-#include "hex/proto.h"
+#include "hex.h"
#include <assert.h>
#include <errno.h>
@@ -37,6 +36,8 @@
#include <time.h>
#include <unistd.h>
+#include "hexes/types.h"
+
struct opts {
u32 log_level, agent_type;
char *host, *port;
diff --git a/agents/hexes/include/hexes/types.h b/agents/hexes/include/hexes/types.h
@@ -0,0 +1,58 @@
+#ifndef HEXES_TYPES_H
+#define HEXES_TYPES_H
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+typedef int32_t b32;
+
+typedef unsigned char c8;
+
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+typedef int8_t s8;
+typedef int16_t s16;
+typedef int32_t s32;
+typedef int64_t s64;
+
+typedef float f32;
+typedef double f64;
+
+#define ARRLEN(arr) (sizeof (arr) / sizeof (arr)[0])
+
+#define MIN(a, b) ((a) > (b) ? (a) : (b))
+#define MAX(a, b) ((a) < (b) ? (b) : (a))
+
+#define RELPTR_NULL (0)
+
+#define _RELPTR_MASK(ty_relptr) ((ty_relptr)1 << ((sizeof(ty_relptr) * 8) - 1))
+#define _RELPTR_ENC(ty_relptr, ptroff) \
+ ((ty_relptr)((ptroff) ^ _RELPTR_MASK(ty_relptr)))
+#define _RELPTR_DEC(ty_relptr, relptr) \
+ ((ty_relptr)((relptr) ^ _RELPTR_MASK(ty_relptr)))
+
+#define RELPTR_ABS2REL(ty_relptr, base, absptr) \
+ ((absptr) \
+ ? _RELPTR_ENC(ty_relptr, (u8 *) absptr - (u8 *) base) \
+ : RELPTR_NULL)
+
+#define RELPTR_REL2ABS(ty_absptr, ty_relptr, base, relptr) \
+ ((relptr) \
+ ? ((ty_absptr)((u8 *) base + _RELPTR_DEC(ty_relptr, relptr))) \
+ : NULL)
+
+#define NANOSECS (1000000000ULL)
+
+#define TIMESPEC_TO_NANOS(sec, nsec) (((u64) (sec) * NANOSECS) + (nsec))
+
+#define KiB (1024ULL)
+#define MiB (1024ULL * KiB)
+#define GiB (1024ULL * MiB)
+
+#endif /* HEXES_TYPES_H */
diff --git a/agents/hexes/src/network.c b/agents/hexes/src/network.c
@@ -93,8 +93,8 @@ network_recv(struct network *self, struct hex_msg *out, enum hex_msg_type *expec
return false;
}
-extern inline b32
+extern inline bool
hex_msg_try_serialise(struct hex_msg const *msg, u8 out[static HEX_MSG_SZ]);
-extern inline b32
+extern inline bool
hex_msg_try_deserialise(u8 buf[static HEX_MSG_SZ], struct hex_msg *out);