question

Upvotes
Accepted
488 12 13 27

Adjust Buffer size when encoding a message

Hello

When encoding a message using the rssl functions, often the size of the resulting message is unknown so at the start a big buffer needs to be created (sometimes way larger than the resulting message size). Is there a clean way of creating a buffer and then making it bigger if it is not large enough?

Thanks

elektronrefinitiv-realtimeelektron-sdktrepbuffer
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

1 Answer

Upvotes
Accepted
401 15 18 33

UPA has a helper method exactly for this purpose. The general idea when you see BUFFER_TOO_SMALL when encoding, you can use the rsslRealignEncodeIteratorBuffer method to associate a new buffer with your encoding iterator and continue encoding from where you left off.

The general process is that you would get a new buffer, ensuring you have a reference to the old buffer if it came from rsslGetBuffer(…). If your failed encoding occurred via an EncodeInit method, you should roll back to the last successful point (by calling the EncodeComplete method with success=false). If you were using a non-Init encode method (e.g. rsslEncodeFieldEntry()) you do not need to roll back as these will automatically roll back upon failure. Once content is rolled back to the last successful point of encode and you have a new, larger buffer you pass the encode iterator and the new buffer into rsslRealignEncodeIteratorBuffer. This will copy your encoding and update the iterator. You then continue encoding by calling the encode method again (e.g. EncodeInit or rsslEncodeFieldEntry()). If the old buffer (the one that was too small) came from rsslGetBuffer, you need to call rsslReleaseBuffer() to ensure it goes back to the pool.

Here is a brief sample, you may have to change this to suit your needs. In this sample, rsslEncodeFieldEntry fails with BUFFER_TOO_SMALL, we get a new transport buffer, realign the iterator, then release the old buffer, and then reencode the field entry.

ret = rsslEncodeFieldEntry(&eIter, &field, &uint);
if (ret == RSSL_RET_BUFFER_TOO_SMALL)
{
       RsslBuffer *pNewBuffer = rsslGetBuffer(&channel, newSize, RSSL_FALSE, &error);
       if (RSSL_RET_SUCCESS == rsslRealignEncodeIteratorBuffer(&eIter, pNewBuffer))
       {
              rsslReleaseBuffer(pOriginalBuffer, &error);
              ret = rsslEncodeFieldEntry(&eIter, &field, &uint));
              // Check return codes and continue on with the rest of encoding
       }
       else
       {
                                //realign didn’t work.  Handle failure
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea