Got protobuff serializing data.
This commit is contained in:
parent
8bf421d3a7
commit
68ebcdd39e
4 changed files with 57 additions and 6 deletions
|
|
@ -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})
|
||||
|
|
|
|||
|
|
@ -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 <error.h>
|
||||
#include <netinet/in.h> //sockaddr_in
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // Keeping for conversion functions
|
||||
|
|
@ -11,6 +11,7 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,12 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void error_crash(const char *msg);
|
||||
|
||||
void error_warn(const char *msg);
|
||||
|
||||
void error_log(const char *msg);
|
||||
|
||||
void msg_amsg_test(int a);
|
||||
|
|
|
|||
Loading…
Reference in a new issue