Home / Downloads / Message port / Help
Message port
Message port provides the high-performance interprocess communication system.
#include "windows.h"
#include "msgaccss.h"
class callback_notify : public msg_callback_notify
{
public:
// this function for incoming messages
virtual bool incoming_callback(msg_t* msg, msg_t* reply)
{
return false; // return false if you don't take responsibility to destroy incoming msg
}
// this function for asynchronous replies
virtual bool async_callback(msg_t* reply, size_t ident)
{
return false; // return false if you don't take responsibility to destroy incoming reply
}
};
int main()
{
callback_notify notifier;
msgaccess msg_creator;
msg_port* port = msg_creator.get_msg_port();
bool mres = port->init("c:\\test.xml", 0);
mres = port->start(¬ifier, 0);
const char* msgBody = "This is a message body which can contains anything you want";
size_t len = strlen(msgBody);
msg_t* msg = port->construct(len + 1); // add space for zero byte
memcpy(port->get_buffer(msg), msgBody, len + 1);
guid_t receiver = {0xD8589807, 0x1D14, 0x4872, {0xB4, 0x2A, 0x51, 0xED, 0xF2, 0xEE, 0x12, 0x39}};
mres = port->set_receiver(msg, receiver);
msg->msgid = 1;
msg->timeout = 1000; // 1 second timeout
int count = 300; // send message 300 times
while (--count)
{
msg_t* reply = 0;
mres = port->send(true, // specify true, port will make a copy of message
msg, &reply);
if (!mres)
{
// if send method returns false, caller is still responsible for msg object
port->destroy(msg);
msg = 0;
break;
}
if (reply) // if reply is specified
{
// get message info here
// for example message body
const void* bufReply = port->get_buffer(reply);
// then destroy reply
port->destroy(reply);
}
}
if (msg) // we still must destroy message
port->destroy(msg);
// stop port
mres = port->stop();
// uninit port
mres = port->uninit();
// destroy port object
delete port;
return 0;
}
How does port know where the another program (port) located?
All settings come from init xml file, below the DTD for such files
<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY % kind "(rpc | sock | http)">
<!ELEMENT msgPort (listeners?, connections?)>
<!ATTLIST msgPort
address CDATA #REQUIRED >
<!ELEMENT listeners (listener)*>
<!ELEMENT connections (connection)*>
<!ELEMENT connection EMPTY>
<!ATTLIST connection
type %kind; #REQUIRED
address CDATA #REQUIRED
port CDATA #IMPLIED
network CDATA #IMPLIED
ping CDATA #IMPLIED
info CDATA #IMPLIED
security CDATA #IMPLIED
password CDATA #IMPLIED >
<!ELEMENT listener (accept | reject)?>
<!ATTLIST listener
type %kind; #REQUIRED
port CDATA #IMPLIED
connections CDATA #IMPLIED
ping CDATA #IMPLIED
info CDATA #IMPLIED
security CDATA #IMPLIED
password CDATA #IMPLIED >
<!ELEMENT reject (peer)+>
<!ELEMENT accept (peer)+>
<!ELEMENT peer EMPTY>
<!ATTLIST peer
address CDATA #REQUIRED
security CDATA #IMPLIED
password CDATA #IMPLIED >
Format of address attribute is follow:
D85898071D144872B42A51EDF2EE1239
Heximal representation of 16 bytes UUID/GUID.
Every new program MUST use new generated UUID.
Two Message Ports with the same UUID are not allowed.
|
|