从API开始理解QNX -- 消息传递 http://www.openqnx.com/chinese/viewtopic.php?f=5&t=2161
1. 频道与连接
Channel, Connect
server:
ChannelId = ChannelCreate(Flags);
client:
ConnectionId = ConnectAttach(Node, Pid, Chid, Index, Flag);
Node: 机器号; Pid是服务进程号; Chid就是ChannelCreate后得到的频道号.
连接的终止是ConnectDetach(),而频道的结束则是ChannelDestroy()了。不过,一般服务器都是长久存在的,不大有需要ChannelDestroy()的时候。
2. 发送,接收与应答
Send, Receive, Reply
server:
RecieveId = MsgReceive(ChannelId, ReceiveBuffer, ReceiveBufLength,&MsgInfo);
...Deal Recvd Msg....
MsgReply(ReceiveId, ReplyBuf, ReplyLen);
client:
MsgSend(ConnectionId, SendBuf, SendLen, ReplyBuf, ReplyLen);
....然后由OS将这个线程挂起...
....当服务器MsgReply()后,OS解除线程阻塞状态,客户端可以检查自己的ReceiveBuf看看应答效果...
3. 数据区与iov
虽然在客户端Header同databuf是两块不相邻的内存,但传递到服务器端的ReceiveBuffer里,就是连续的了。
客户端: "header" 与 "databuf" 是不连续的两块数据。
SETIOV(&iov[0], &header, sizeof(header));
SETIOV(&iov[1], databuf, datalen);
MsgSendvs(ConnectionId, iov, 2, Replybf, ReplyLen);
服务器: 接收后,"header"与"databuf"被连续地存在ReceiveBuffer里。
ReceiveId = MsgReceive(ChannelId, ReceiveBuffer, ReceiveBufLength, &MsgInfo);
header = (struct header *)ReceiveBuffer;
databuf = (char *)((char *)header + sizeof(*header));
当指定的ReceiveBufLength小于实际收到的字节数,即MsgReceive不一定读完了所有来自client的数据,因此还需要查看MsgInfo,并使用MsgRead(ReceiveId,
ReceiveBuffer+ReceiveBufLength, // 指定存数据buffer起始地址
ReceiveBufLength, // 去缓冲区读数据时候的偏移量
MsgInfo->srcmsglen - MsgInfo->msglen // 未读取完的数据长度
));