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 />

Establishing a connection<br />

So, let’s look at these functions in turn. The first thing we need to do is to establish a<br />

connection. We do this <strong>with</strong> the function ConnectAttach(), which looks like this:<br />

#include <br />

int ConnectAttach (int nd,<br />

pid_t pid,<br />

int chid,<br />

unsigned index,<br />

int flags);<br />

ConnectAttach() is given three identifiers: the nd, which is the Node Descriptor, the<br />

pid, which is the process ID, and the chid, which is the channel ID. These three IDs,<br />

commonly referred to as “ND/PID/CHID,” uniquely identify the server that the client<br />

wants to connect to. We’ll ignore the index and flags (just set them to 0).<br />

So, let’s assume that we want to connect to process ID 77, channel ID 1 on our node.<br />

Here’s the code sample to do that:<br />

int coid;<br />

coid = ConnectAttach (0, 77, 1, 0, 0);<br />

As you can see, by specifying a nd of zero, we’re telling the kernel that we wish to<br />

make a connection on our node.<br />

Sending messages<br />

How did I figure out I wanted to talk to process ID 77 and channel ID 1? We’ll see that<br />

shortly (see “Finding the server’s ND/PID/CHID,” below).<br />

At this point, I have a connection ID — a small integer that uniquely identifies a<br />

connection from my client to a specific channel on a particular server.<br />

I can use this connection ID when sending to the server as many times as I like. When<br />

I’m done <strong>with</strong> it, I can destroy it via:<br />

ConnectDetach (coid);<br />

So let’s see how I actually use it.<br />

Message passing on the client is achieved using some variant of the MsgSend*()<br />

function family. We’ll look at the simplest member, MsgSend():<br />

#include <br />

int MsgSend (int coid,<br />

const void *smsg,<br />

int sbytes,<br />

void *rmsg,<br />

int rbytes);<br />

MsgSend()’s arguments are:<br />

• the connection ID of the target server (coid),<br />

92 Chapter 2 • Message Passing April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!