Added some helper methods to message lib
This commit is contained in:
parent
f1cc0c72fc
commit
4c07b5df6a
2 changed files with 12 additions and 5 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include <bits/stdint-uintn.h>
|
#include <bits/stdint-uintn.h>
|
||||||
|
|
||||||
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);
|
size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out);
|
||||||
|
|
|
||||||
|
|
@ -59,13 +59,18 @@ size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out) {
|
||||||
return cur_len;
|
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) {
|
void msg__recv_login(int *con_sockfd) {
|
||||||
uint8_t msgbuf[MAX_MSG_SIZE];
|
uint8_t msgbuf[MAX_MSG_SIZE];
|
||||||
bzero(msgbuf, sizeof msgbuf);
|
|
||||||
|
|
||||||
if (read(*con_sockfd, msgbuf, sizeof msgbuf) < 0) {
|
read_from_sock(msgbuf, sizeof msgbuf, con_sockfd);
|
||||||
err__warn("Connection did not send login data");
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t msg_len = get_msg_size_in_buffer(MAX_MSG_SIZE, msgbuf);
|
size_t msg_len = get_msg_size_in_buffer(MAX_MSG_SIZE, msgbuf);
|
||||||
PlayerLogin *pl = player_login__unpack(NULL, msg_len, msgbuf);
|
PlayerLogin *pl = player_login__unpack(NULL, msg_len, msgbuf);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue