diff --git a/CMakeLists.txt b/CMakeLists.txt index 8face6f..f6306b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,16 +39,18 @@ file(GLOB_RECURSE PROTOBUF_SOURCE_FILES file(GLOB_RECURSE PROTOBUF_HEADER_FILES ${CMAKE_SOURCE_DIR}/src/protobuf_gen/*.h) +set_source_files_properties(${PROTOBUF_HEADER_FILES} ${PROTOBUF_SOURCE_FILES} PROPERTIES GENERATED TRUE) -add_library(${PROJECT_NAME} SHARED +add_library(${PROJECT_NAME} STATIC GenerateProtobuf ${LIBRARY_HEADER_FILES} - ${LIBRARY_SOURCE_FILES} ${PROTOBUF_HEADER_FILES} + ${LIBRARY_SOURCE_FILES} ${PROTOBUF_SOURCE_FILES} ) +add_dependencies(${PROJECT_NAME} GenerateProtobuf) -target_link_libraries(${PROJECT_NAME} protobuf-c) +target_link_libraries(${PROJECT_NAME} PUBLIC protobuf-c) # Compile Server @@ -59,7 +61,7 @@ file(GLOB_RECURSE SERVER_HEADER_FILES ${CMAKE_SOURCE_DIR}/src/server/*.h) add_executable(${PROJECT_NAME}-server ${SERVER_HEADER_FILES} ${SERVER_SOURCE_FILES}) -target_link_libraries(${PROJECT_NAME}-server ${PROJECT_NAME}) +target_link_libraries(${PROJECT_NAME}-server PUBLIC ${PROJECT_NAME}) # Compile Client file(GLOB_RECURSE CLIENT_SOURCE_FILES @@ -69,4 +71,4 @@ file(GLOB_RECURSE CLIENT_HEADER_FILES ${CMAKE_SOURCE_DIR}/src/client/*.h) add_executable(${PROJECT_NAME}-client ${CLIENT_HEADER_FILES} ${CLIENT_SOURCE_FILES}) -target_link_libraries(${PROJECT_NAME}-client ${PROJECT_NAME}) +target_link_libraries(${PROJECT_NAME}-client PUBLIC ${PROJECT_NAME}) diff --git a/src/server/main.c b/src/server/main.c index eee833b..d158c1d 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -1,7 +1,7 @@ // Sorry in advance for all the comments. This project is also a way for me to // learn C. -#include "error.h" +#include #include //sockaddr_in #include #include // Keeping for conversion functions @@ -11,6 +11,7 @@ #include int main(int argc, char *argv[]) { + // Set up containers for file descriptor id's int sockfd, clientsockfd, portno, errflag; char msgbuffer[255]; @@ -33,6 +34,10 @@ int main(int argc, char *argv[]) { error_crash("Can't open socket."); } + error_warn("Can't open socket."); + int a = 1; + msg_amsg_test(a); + // Make sure there is no junk data in serve_addr bzero((char *)&serv_addr, sizeof serv_addr); diff --git a/src/shared_lib/error.c b/src/shared_lib/error.c index 7919120..02e1bc9 100644 --- a/src/shared_lib/error.c +++ b/src/shared_lib/error.c @@ -1,4 +1,6 @@ #include "error.h" +#include "../protobuf_gen/amessage.pb-c.h" +#define MAX_MSG_SIZE 1024 void error_crash(const char *msg) { fprintf(stderr, "error crash: %s\n", msg); @@ -9,3 +11,42 @@ void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); } // TODO: Save the logs somewhere void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); } + +void msg_amsg_test(int a) { + AMessage msg = AMESSAGE__INIT; // The massage format + void *buf; // Buffer to store serialized data + unsigned len; // length of serialized data + + msg.a = a; + msg.b = 2; + msg.has_b = 1; + len = amessage__get_packed_size(&msg); + buf = malloc(len); + amessage__pack(&msg, buf); // we now have a serialised message buffer + + fprintf(stderr, "Writing %d serialized bytes\n", + len); // See the length of message + fwrite(buf, len, 1, stderr); + + // unpack the message + AMessage *msg2; + uint8_t buf2[MAX_MSG_SIZE]; + size_t msg_len = len; + memcpy((void *)buf2, (void *)buf, msg_len); + + free(buf); + + msg2 = amessage__unpack(NULL, msg_len, buf2); + if (msg2 == NULL) { + error_crash("error unpacking incoming message\n"); + } + + // display the message's fields. + printf("Received: a=%d", msg2->a); // required field + if (msg2->has_b) // handle optional field + printf(" b=%d", msg2->b); + printf("\n"); + + // Free the unpacked message + amessage__free_unpacked(msg2, NULL); +} diff --git a/src/shared_lib/error.h b/src/shared_lib/error.h index 3d9a50a..9f7b500 100644 --- a/src/shared_lib/error.h +++ b/src/shared_lib/error.h @@ -2,9 +2,12 @@ #include #include +#include void error_crash(const char *msg); void error_warn(const char *msg); void error_log(const char *msg); + +void msg_amsg_test(int a);