Search code examples
c++csolace

Get solace proprietary binary message into sscanf


I am sending a message like this:

char buffer[175];
sprintf(buffer, "MD: %4ld %2d %10s %5s %7.2f %5d\n"
    , id
    , position
    , *(MktDepthOperation::ENUMS) operation
    , *(MktDeptSide::ENUMS)side
    , price
    , size);

PrintProcessId, printf(buffer);
SolSendMessage("testhello", buffer);
...

void SolSendMessage(const char* topic, const char *text_p)
{
    ...
    if (s_canSend) {
            if ((rc = solClient_session_sendMsg(session_p, msg_p)) != SOLCLIENT_OK) {
...
}

On the sub side, I am just dumping the message. How do I sscanf the fields back from the binary buffer that encodes the solace proprietary format? I am trying to avoid google protocol buffers and using the recommended Solace proprietary format.

solClient_rxMsgCallback_returnCode_t
messageReceiveCallback ( solClient_opaqueSession_pt opaqueSession_p, solClient_opaqueMsg_pt msg_p, void *user_p )
{
    //printf ( "Received message:\n" );
    solClient_msg_dump ( msg_p, NULL, 0 );
    printf ( "\n" );

    msgCount++;

    return SOLCLIENT_CALLBACK_OK;
}

Solution

  • It is not clear from your code snippet how your buffer is being set on the message. To simply send and receive a string in the binary attachment of the message with the Solace API, you can use solClient_msg_setBinaryAttachment when sending and solClient_msg_getBinaryAttachment to retrieve the string when receiving. It is not recommended to sscanf the output of solClient_msg_dump as this will include extra information about the headers of the message. This message dump utility is provided as a programming aid to facilitate the development and testing of messaging applications, not to directly extract the data in a message.

    Another option is to use Solace Structured Data Type. Solace SDTs are structured, language-independent, and architecture-independent data types. They can be used in messages to facilitate the exchange of binary data in a heterogeneous network that has clients that use different hardware architectures and programming languages. If you are sending a fixed data structure, you can use "createBinaryAttachmentStream" to create a stream of structured data.

    e.g. If you have a fixed data structure like:

    struct MD {
            long id;
            int position;
            char operation[10];
            char side[5];
            float price;
            int size);
        }
    

    You can create a stream of structured data and then call addInt64/addInt32/addString/addString/addFloat/addInt for each of the members. On the receiving side, you can retrieve the dataStructure members by calling getInt64/getInt32, etc. Otherwise, if you are not using a known data structure, you can use a map instead of a stream and name each field appropriately.

    More information about Solace Structured Data Types is available here: https://docs.solace.com/Solace-PubSub-Messaging-APIs/Developer-Guide/SDT-Containers.htm