1# msg_queue 2 3This is the Cuttlefish message queue wrapper library, as one of the IPC options available. Below are the example usages running in two separate processes. 4 5``` 6#define MAX_MSG_SIZE 200 7#define NUM_MESSAGES 100 8 9typedef struct msg_buffer { 10 long mesg_type; 11 char mesg_text[MAX_MSG_SIZE]; 12} msg_buffer; 13 14int example_send() 15{ 16 const std::string message ="Test message" 17 auto msg_queue = SysVMessageQueue::Create("unique_queue_name", false); 18 if (msg_queue == NULL) { 19 LOG(FATAL) << "Create: failed to create" << "unique_queue_name"; 20 } 21 22 struct msg_buffer msg; 23 msg.mesg_type = 1; 24 strcpy(msg.mesg_text, message.c_str()); 25 int rc = msg_queue->Send(&msg, message.length() + 1, true); 26 if (rc == -1) { 27 LOG(FATAL) << "Send: failed to send message to msg_queue"; 28 } 29} 30``` 31 32``` 33#define MAX_MSG_SIZE 200 34 35typedef struct msg_buffer { 36 long mesg_type; 37 char mesg_text[MAX_MSG_SIZE]; 38} msg_buffer; 39 40int example_receive() 41{ 42 auto msg_queue = SysVMessageQueue::Create("unique_queue_name"); 43 if (msg_queue == NULL) { 44 LOG(FATAL) << "create: failed to create" << "unique_queue_name"; 45 } 46 47 struct msg_buffer msg = {0, {0}}; 48 int rc = msg_queue->Receive(&msg, MAX_MSG_SIZE, 1, true); 49 if (rc == -1) { 50 LOG(FATAL) << "receive: failed to receive any messages"; 51 } 52 53 std::string text(msg.mesg_text); 54 LOG(INFO) << "Metrics host received: " << text; 55 return 0; 56} 57``` 58