protocol

protocol tests
Log | Files | Refs | Submodules | README | git clone https://git.ne02ptzero.me/git/protocol

commit 665944e40931805b7e323266f93d63cf1818fd2e
parent 8f7362a26a9684e16667da03a4e884345e514f12
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 17 Mar 2017 21:47:45 +0100

Add(Helpers): Add helpers in order to build package easily

Diffstat:
Minc/package.h | 9+++++++++
Msrc/client_test.c | 1+
Msrc/package.c | 40++++++++++++++++++++++++++++++++++++++++
3 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/inc/package.h b/inc/package.h @@ -62,6 +62,9 @@ typedef struct error_pkg_s { char *err; } SF_PACKED error_pkg_t; +# define SIZEOF_ERR(err) (sizeof(err->error_type) + sizeof(err->error_len) \ + + err->error_len) + /* Error possible types */ typedef enum error_type_e { ERR_SERVER_FAULT = 1, @@ -81,6 +84,11 @@ typedef struct req_get_pkg_s { char *version; } SF_PACKED req_get_pkg_t; +# define SIZEOF_REQ_GET_PKG(pkg) (sizeof(pkg->id) + sizeof(pkg->state) + \ + sizeof(pkg->name_len) + sizeof(pkg->categ_len) + \ + sizeof(pkg->version_len) + pkg->name_len + pkg->categ_len + \ + pkg->version_len) + /* Stability of packages */ typedef enum package_state_e { PKG_STABLE = 1, @@ -161,5 +169,6 @@ void *write_package(package_t *pkg, size_t *size); package_t *read_pkg(void *data); void *pkg_build_auth(size_t *size, int major_version, int minor_version); void *pkg_build_auth_ack(size_t *size, int major_version, int minor_version); +void *pkg_build_error(size_t *size, error_type_t type, const char *error); #endif /* PACKAGE_H */ diff --git a/src/client_test.c b/src/client_test.c @@ -48,6 +48,7 @@ TEST(pkg_auth_1_read) { pkg = read_pkg(ret); TEST_ASSERT(pkg, "Can't read package"); TEST_ASSERT(pkg->type == PKG_TYPE_AUTH_ACK, "Package type is wrong"); + free(ret); return TEST_SUCCESS; } diff --git a/src/package.c b/src/package.c @@ -31,3 +31,43 @@ void *pkg_build_auth_ack(size_t *size, int major_version, int minor_version) { ret = write_package(pkg, size); return ret; } + +void *pkg_build_error(size_t *size, error_type_t type, const char *error) { + error_pkg_t *err; + package_t *pkg; + + err = malloc(sizeof(error_pkg_t)); + err->error_type = type; + err->error_len = strlen(error); + err->err = strdup(error); + + pkg = malloc(sizeof(package_t)); + pkg->type = PKG_TYPE_ERROR; + pkg->payload = NULL; + pkg->number = 1; + list_add(pkg->payload, err, SIZEOF_ERR(err)); + return write_package(pkg, size); +} + +void *pkg_build_req_get_pkg(size_t *size, u64_t id, u8_t state, const char *name, + const char *category, const char *version) { + req_get_pkg_t *req; + package_t *pkg; + + req = malloc(sizeof(req_get_pkg_t)); + req->id = id; + req->state = state; + req->name_len = strlen(name); + req->categ_len = strlen(category); + req->version_len = strlen(version); + req->name = strdup(name); + req->category = strdup(category); + req->version = strdup(version); + + pkg = malloc(sizeof(package_t)); + pkg->type = PKG_TYPE_REQ_GET_PKG; + pkg->payload = NULL; + pkg->number = 1; + list_add(pkg->payload, req, SIZEOF_REQ_GET_PKG(req)); + return write_package(pkg, size); +}