diff --git a/urchin-util/include/message_internal.h b/urchin-util/include/message_internal.h index bcc738b..de28d90 100644 --- a/urchin-util/include/message_internal.h +++ b/urchin-util/include/message_internal.h @@ -2,6 +2,8 @@ #include "error.h" #include -void msg__read_from_socket(int *sockfd, uint8_t *msgbuf, unsigned bufsize); +// One method to fill the buffer from a connection +void read_from_sock(uint8_t *recvbuf, unsigned bufsize, int *sockfd); +// One method to see how much data is in a buffer to unpack size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out); diff --git a/urchin-util/src/message.c b/urchin-util/src/message.c index c95f8df..f900547 100644 --- a/urchin-util/src/message.c +++ b/urchin-util/src/message.c @@ -59,13 +59,18 @@ size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out) { return cur_len; } +void read_from_sock(uint8_t *recvbuf, unsigned bufsize, int *sockfd) { + bzero(recvbuf, bufsize); + + if (read(*sockfd, recvbuf, bufsize) < 0) { + err__warn("Connection did not send data"); + } +} + void msg__recv_login(int *con_sockfd) { uint8_t msgbuf[MAX_MSG_SIZE]; - bzero(msgbuf, sizeof msgbuf); - if (read(*con_sockfd, msgbuf, sizeof msgbuf) < 0) { - err__warn("Connection did not send login data"); - } + read_from_sock(msgbuf, sizeof msgbuf, con_sockfd); size_t msg_len = get_msg_size_in_buffer(MAX_MSG_SIZE, msgbuf); PlayerLogin *pl = player_login__unpack(NULL, msg_len, msgbuf);