libeis: use the shared next_message helper

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2020-08-18 19:33:24 +10:00
parent 4daab4de92
commit a20d09c049
2 changed files with 9 additions and 23 deletions

View file

@ -393,21 +393,21 @@ client_dispatch(struct source *source, void *userdata)
size_t idx = 0;
while (rc == 0) {
_cleanup_message_free_ struct message *msg = NULL;
const char *data = iobuf_data(buf) + idx;
size_t len = iobuf_len(buf) - idx;
size_t consumed = 0;
if (len == 0)
break;
msg = eisproto_parse_message(data, &len);
_cleanup_message_ struct message *msg =
eis_proto_parse_message(data, len, &consumed);
if (!msg) {
rc = -EBADMSG;
goto error;
}
idx += len;
idx += consumed;
switch (client->state) {
case EIS_CLIENT_STATE_NEW:

View file

@ -32,6 +32,7 @@
#include "proto/ei.pb-c.h"
#include "libeis-proto.h"
#include "brei-shared.h"
void
message_free(struct message *msg)
@ -146,32 +147,17 @@ eis_proto_send_device_removed(struct eis_client *client, struct eis_device *devi
struct message *
eis_proto_parse_message(const char *data, size_t len, size_t *consumed)
{
/* Every message is prefixed by a fixed-length Frame message which
* contains the length of the next message. Parse that one first,
* then the real message */
static size_t framelen = 0;
if (framelen == 0) {
Frame f = FRAME__INIT;
f.length = 0xffff;
framelen = frame__get_packed_size(&f);
assert(framelen >= 5);
}
Frame *frame = frame__unpack(NULL, framelen, (const unsigned char *)data);
if (!frame)
return NULL;
size_t headerbytes = 0;
size_t msglen = 0;
const unsigned char *msgdata = brei_next_message(data, &msglen, &headerbytes);
size_t msglen = frame->length;
frame__free_unpacked(frame, NULL);
const unsigned char *msgdata = (const unsigned char *)data + framelen;
_cleanup_(message_freep) struct message *msg = xalloc(sizeof(*msg));
ClientMessage *proto = client_message__unpack(NULL, msglen, msgdata);
if (!proto) {
return NULL;
}
*len = framelen + msglen;
*consumed = headerbytes + msglen;
bool success = true;
switch (proto->msg_case) {