26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using message passing<br />

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG.<br />

You may have noticed that there are two sizes for every buffer transfer (in the client<br />

send case, there’s sbytes on the client side and rbytes on the server side; in the server<br />

reply case, there’s sbytes on the server side and rbytes on the client side.) The two sets<br />

of sizes are present so that the programmers of each component can specify the sizes<br />

of their buffers. This is done for added safety.<br />

In our example, the MsgSend() buffer’s size was the same as the message string’s<br />

length. Let’s look at the server and see how the size is used there.<br />

Server framework<br />

Here’s the overall structure of a server:<br />

#include <br />

...<br />

void<br />

server (void)<br />

{<br />

int rcvid; // indicates who we should reply to<br />

int chid; // the channel ID<br />

char message [512]; // big enough for our purposes<br />

}<br />

// create a channel<br />

chid = ChannelCreate (0);<br />

// this is typical of a server: it runs forever<br />

while (1) {<br />

}<br />

// get the message, and print it<br />

rcvid = MsgReceive (chid, message, sizeof (message),<br />

NULL);<br />

printf ("Got a message, rcvid is %X\n", rcvid);<br />

printf ("Message was \"%s\".\n", message);<br />

// now, prepare the reply. We reuse "message"<br />

strcpy (message, "This is the reply");<br />

MsgReply (rcvid, EOK, message, sizeof (message));<br />

As you can see, MsgReceive() tells the kernel that it can handle messages up to<br />

sizeof (message) (or 512 bytes). Our sample client (above) sent only 28 bytes<br />

(the length of the string). The following diagram illustrates:<br />

Client<br />

28<br />

bytes<br />

Server<br />

28<br />

bytes<br />

484<br />

bytes<br />

not<br />

written<br />

Transferring less data than expected.<br />

96 Chapter 2 • Message Passing April 30, 2009

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!