packet: Hide internals of pa_packet, introduce pa_packet_data()

Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
This commit is contained in:
Peter Meerwald 2014-10-23 11:43:04 +02:00 committed by Peter Meerwald
parent 9f97f08f40
commit 5a2c41e5bf
5 changed files with 50 additions and 24 deletions

View file

@ -25,9 +25,17 @@
#include <pulse/xmalloc.h>
#include <pulsecore/macro.h>
#include <pulsecore/refcnt.h>
#include "packet.h"
typedef struct pa_packet {
PA_REFCNT_DECLARE;
enum { PA_PACKET_APPENDED, PA_PACKET_DYNAMIC } type;
size_t length;
uint8_t *data;
} pa_packet;
pa_packet* pa_packet_new(size_t length) {
pa_packet *p;
@ -57,6 +65,16 @@ pa_packet* pa_packet_new_dynamic(void* data, size_t length) {
return p;
}
const void* pa_packet_data(pa_packet *p, size_t *l) {
pa_assert(PA_REFCNT_VALUE(p) >= 1);
pa_assert(p->data);
pa_assert(l);
*l = p->length;
return p->data;
}
pa_packet* pa_packet_ref(pa_packet *p) {
pa_assert(p);
pa_assert(PA_REFCNT_VALUE(p) >= 1);

View file

@ -23,18 +23,13 @@
#include <sys/types.h>
#include <inttypes.h>
#include <pulsecore/refcnt.h>
typedef struct pa_packet {
PA_REFCNT_DECLARE;
enum { PA_PACKET_APPENDED, PA_PACKET_DYNAMIC } type;
size_t length;
uint8_t *data;
} pa_packet;
typedef struct pa_packet pa_packet;
pa_packet* pa_packet_new(size_t length);
pa_packet* pa_packet_new_dynamic(void* data, size_t length);
const void* pa_packet_data(pa_packet *p, size_t *l);
pa_packet* pa_packet_ref(pa_packet *p);
void pa_packet_unref(pa_packet *p);

View file

@ -293,19 +293,20 @@ int pa_pdispatch_run(pa_pdispatch *pd, pa_packet *packet, const pa_cmsg_ancil_da
uint32_t tag, command;
pa_tagstruct *ts = NULL;
int ret = -1;
const void *pdata;
size_t plen;
pa_assert(pd);
pa_assert(PA_REFCNT_VALUE(pd) >= 1);
pa_assert(packet);
pa_assert(PA_REFCNT_VALUE(packet) >= 1);
pa_assert(packet->data);
pa_pdispatch_ref(pd);
if (packet->length <= 8)
pdata = pa_packet_data(packet, &plen);
if (plen <= 8)
goto finish;
ts = pa_tagstruct_new_fixed(packet->data, packet->length);
ts = pa_tagstruct_new_fixed(pdata, plen);
if (pa_tagstruct_getu32(ts, &command) < 0 ||
pa_tagstruct_getu32(ts, &tag) < 0)

View file

@ -491,14 +491,16 @@ static void prepare_next_write_item(pa_pstream *p) {
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_FLAGS] = 0;
if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
size_t plen;
pa_assert(p->write.current->packet);
p->write.data = p->write.current->packet->data;
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl((uint32_t) p->write.current->packet->length);
if (p->write.current->packet->length <= MINIBUF_SIZE - PA_PSTREAM_DESCRIPTOR_SIZE) {
memcpy(&p->write.minibuf[PA_PSTREAM_DESCRIPTOR_SIZE], p->write.data, p->write.current->packet->length);
p->write.minibuf_validsize = PA_PSTREAM_DESCRIPTOR_SIZE + p->write.current->packet->length;
p->write.data = (void *) pa_packet_data(p->write.current->packet, &plen);
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl((uint32_t) plen);
if (plen <= MINIBUF_SIZE - PA_PSTREAM_DESCRIPTOR_SIZE) {
memcpy(&p->write.minibuf[PA_PSTREAM_DESCRIPTOR_SIZE], p->write.data, plen);
p->write.minibuf_validsize = PA_PSTREAM_DESCRIPTOR_SIZE + plen;
}
} else if (p->write.current->type == PA_PSTREAM_ITEM_SHMRELEASE) {
@ -787,6 +789,7 @@ static int do_read(pa_pstream *p, struct pstream_read *re) {
channel = ntohl(re->descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]);
if (channel == (uint32_t) -1) {
size_t plen;
if (flags != 0) {
pa_log_warn("Received packet frame with invalid flags value.");
@ -795,7 +798,7 @@ static int do_read(pa_pstream *p, struct pstream_read *re) {
/* Frame is a packet frame */
re->packet = pa_packet_new(length);
re->data = re->packet->data;
re->data = (void *) pa_packet_data(re->packet, &plen);
} else {

View file

@ -35,26 +35,35 @@ static unsigned packets_checksum;
static size_t packets_length;
static void packet_received(pa_pstream *p, pa_packet *packet, const pa_cmsg_ancil_data *ancil_data, void *userdata) {
const uint8_t *pdata;
size_t plen;
unsigned i;
fail_unless(packets_length == packet->length);
pdata = pa_packet_data(packet, &plen);
fail_unless(packets_length == plen);
packets_received++;
for (i = 0; i < packet->length; i++)
packets_checksum += packet->data[i];
for (i = 0; i < plen; i++)
packets_checksum += pdata[i];
}
static void packet_test(unsigned npackets, size_t plength, pa_mainloop *ml, pa_pstream *p1, pa_pstream *p2) {
pa_packet *packet = pa_packet_new(plength);
unsigned i;
unsigned psum = 0, totalsum = 0;
uint8_t *pdata;
size_t plen;
pa_log_info("Sending %d packets of length %zd", npackets, plength);
packets_received = 0;
packets_checksum = 0;
packets_length = plength;
pa_pstream_set_receive_packet_callback(p2, packet_received, NULL);
for (i = 0; i < plength; i++) {
packet->data[i] = i;
psum += packet->data[i];
pdata = (uint8_t *) pa_packet_data(packet, &plen);
for (i = 0; i < plen; i++) {
pdata[i] = i;
psum += pdata[i];
}
for (i = 0; i < npackets; i++) {