[nanomsg] nn_recv with padding

  • From: Drew Crawford <drew@xxxxxxxxxxxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Mon, 12 May 2014 05:08:47 -0500

A common case for me is to receive a message to which my application will add 
some additional information of known size.  For a message of fixed size I can do

const int fixedSize;
uint8_t buf[fixedSize+padding];
rc = nn_recv(socket,buf,fixedSize,0);

And similar to get appended rather than prepended padding.  But for messages of 
variable size I have to use the form

void *buf = NULL;
nn_recv(socket,&buf,NN_MSG,0)

which would require memcpy to copy into a buffer with space for padding.  
Ideally I would like something like

struct nn_padding_description {
    int paddingSize = 20;
    int paddingType = NN_PREPEND;
}
nn_recv(socket,&buf,padding_description,NN_LENGTH_IS_PADDING_DESCRIPTION_NOT_ACTUAL_LENGTH);

An alternate idea would be some way to find out the size in advance

int size = nn_peeksize(socket,options);
uint8_t buf[size + padding];
rc = nn_recv(socket,buf,size,0);

This approach has the advantage of supporting size-dependent padding schemes 
(for example, padding that is exactly double message length) but there may be 
some implementation detail why message size cannot be known in advance.

Drew

Other related posts: