I have the following data structure:
typedef struct
{
short id;
string name;
short age;
} person_struct;
Using boost message queue, I tried to send this data structure to the message queue receiver in another process. However, after receiving, I have segmentation fault when accessing the 'name' variable in the above structure.
Below is my sender function:
person_struct MyRec;
MyRec.id = 1;
MyRec.name = "ABC123";
MyRec.age = 20;
message_queue mqSender(create_only, "MSG_Q", 100, sizeof(person_struct));
mqSender.send(&MyRec, sizeof(person_struct), MQ_PRIORITY);
Below is my receiver function:
message_queue myReceiver(open_only, "MSG_Q");
person_struct *recvMsg = new person_struct();
size_t msg_size;
unsigned int priority;
myReceiver.receive(recvMsg, sizeof(person_struct), msg_size, priority);
cout << "ID: " << (*recvMsg).id << endl;
cout << "Name: " << (*recvMsg).name << endl;
cout << "Age: " << (*recvMsg).age << endl;
The cout for (*recvMsg).id is okay, but segmentation fault occurred at cout for (*recvMsg).name. Read somewhere that I need to do serialization for the structure, but can't figure out how to do it. Can anyone suggest?
From the boost doc for message queue:
A message queue just copies raw bytes between processes and does not send objects. This means that if we want to send an object using a message queue the object must be binary serializable. For example, we can send integers between processes but not a std::string. You should use Boost.Serialization or use advanced Boost.Interprocess mechanisms to send complex data between processes.
Use Boost.Serialization to serialize your object and de-serialize on receiving end.
Some quick, working code:
info.hpp
#include <boost/serialization/string.hpp>
#define MAX_SIZE 1000
class info
{
public:
info (int i = 0, std::string n = "")
: id(i), name(n)
{};
int id;
std::string name;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & id;
ar & name;
}
};
send.cpp
#include <string>
#include <sstream>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "info.hpp"
using namespace boost::interprocess;
int main ()
{
try
{
message_queue mq
(
open_or_create,
"mq",
100,
MAX_SIZE
);
info me(1, "asdf");
std::stringstream oss;
boost::archive::text_oarchive oa(oss);
oa << me;
std::string serialized_string(oss.str());
mq.send(serialized_string.data(), serialized_string.size(), 0);
}
catch(interprocess_exception &ex)
{
std::cerr << ex.what() << std::endl;
}
}
receive.cpp
#include <string>
#include <iostream>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/archive/text_iarchive.hpp>
#include "info.hpp"
using namespace boost::interprocess;
int main ()
{
try
{
message_queue mq
(
open_only,
"mq"
);
message_queue::size_type recvd_size;
unsigned int priority;
info me;
std::stringstream iss;
std::string serialized_string;
serialized_string.resize(MAX_SIZE);
mq.receive(&serialized_string[0], MAX_SIZE, recvd_size, priority);
iss << serialized_string;
boost::archive::text_iarchive ia(iss);
ia >> me;
std::cout << me.id << std::endl;
std::cout << me.name << std::endl;
}
catch(interprocess_exception &ex)
{
std::cerr << ex.what() << std::endl;
}
message_queue::remove("mq");
}
One way of making transfer of complex datastructures is by doing it the old fashion way -- make your own data encoder / decoder. if you use the basic concept of ASN1 (Abstract Syntax Notation One), then you can encode data into a binary field, then transfer it and decode it using your decoder
/* Example: Creating an event message to be send to a server asking it to use method MusicPlayer passing on a transaction id and action Start/Stop All data is in the pCompressedData !!! it is this memory that should be send.
// Client code
// Create DataEncoderDecoder response
// Encode
DED_START_ENCODER(encoder_ptr);
DED_PUT_STRUCT_START( encoder_ptr, "event" );
DED_PUT_METHOD ( encoder_ptr, "name", "MusicPlayer" );
DED_PUT_USHORT ( encoder_ptr, "trans_id", trans_id);
DED_PUT_BOOL ( encoder_ptr, "startstop", action );
DED_PUT_STRUCT_END( encoder_ptr, "event" );
DED_GET_ENCODED_DATA(encoder_ptr,data_ptr,iLengthOfTotalData,pCompressedData,sizeofCompressedData);
// Data to be sent is in pCompressedData
// Server code
// retrieve data ...
//...
std::string strName,strValue;
unsigned short iValue;
bool bValue;
DED_PUT_DATA_IN_DECODER(decoder_ptr,pCompressedData,sizeofCompressedData);
// decode data ...
if( DED_GET_STRUCT_START( decoder_ptr, "event" ) &&
DED_GET_METHOD ( decoder_ptr, "name", strValue ) &&
DED_GET_USHORT ( decoder_ptr, "trans_id", iValue) &&
DED_GET_BOOL ( decoder_ptr, "startstop", bValue ) &&
DED_GET_STRUCT_END( decoder_ptr, "event" ))
{
TRACE0(_T("FAIL!!!\n"));
}
else
{
TRACE0(_T("SUCCESS!!!\n"));
}
*/
Create DED_xxx as basic macros bundling the ASN1 solution!
ASN1 info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With