C++ Boost IPC消息队列重试接收抛出进程间异常::库错误
我正在使用boost::interprocess::message_queue在我的两个进程之间进行进程间通信 这是我第一次使用它,所以我不清楚这个异常,因为我找不到任何关于它的文档 我的课程设置如下:C++ Boost IPC消息队列重试接收抛出进程间异常::库错误,c++,exception,boost,c++11,boost-interprocess,C++,Exception,Boost,C++11,Boost Interprocess,我正在使用boost::interprocess::message_queue在我的两个进程之间进行进程间通信 这是我第一次使用它,所以我不清楚这个异常,因为我找不到任何关于它的文档 我的课程设置如下: struct Pos{float X,Y,Z;}; struct Quat{float W,X,Y,Z;}; typedef unsigned char Byte; struct NPCDataFoot { //some Pos and Quat variables here too
struct Pos{float X,Y,Z;};
struct Quat{float W,X,Y,Z;};
typedef unsigned char Byte;
struct NPCDataFoot
{
//some Pos and Quat variables here too
unsigned short AnimationIndex;
void Apply(NPCDataFoot &data){AnimationIndex=data.AnimationIndex;}
NPCDataFoot(){AnimationIndex=0;}
};
struct NPCDataVehicle
{
//many many more
unsigned short lrAnalog;
void Apply(NPCDataVehicle &data){lrAnalog=data.lrAnalog;}
NPCDataVehicle(){lrAnalog = 0;}
};
enum TransmissionDataType{
TDT_NewNPC,//many more...
};
const unsigned short QueueMaxSize = 256;
struct ExchangeData
{
unsigned short CommandType;
unsigned short NPCPlayerID;
Byte State;
NPCDataFoot OnFootData;
NPCDataVehicle InCarData;
float MoveSpeed;
Pos MoveToPos;
//206
ExchangeData(unsigned short CommandType = 0, unsigned short NPCPlayerid = 0xFFFF)
: CommandType(CommandType), NPCPlayerID(NPCPlayerid)
{}
ExchangeData(unsigned short CommandType, unsigned short NPCPlayerid, NPCDataFoot& foot_data, NPCDataVehicle& car_data)
: CommandType(CommandType), NPCPlayerID(NPCPlayerid), OnFootData(foot_data), InCarData(car_data)
{}
};
我的两个程序都设置为使用/zp1标志编译(将结构/类对齐为1字节对齐)
现在,每当我达到这个代码:
ServerMsgQueue * message_queue = NULL;
PLUGIN_EXPORT void PLUGIN_CALL
ProcessTick()
{
static bool init = false;
static ExchangeData DataTransmision;
if(!init)
{
try
{
ServerMsgQueue = new message_queue(open_or_create
,string_format("REMOTESHAREDMEMORYBTWNPRCS%04x",GetServerVarAsInt("port")).c_str()
,1024 * QueueMaxSize,sizeof(ExchangeData));
}
catch(interprocess_exception &ex)
{
std::cout << ex.what() << ":" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << std::endl;
}
init = true;
}
static unsigned int unused;
if(ServerMsgQueue)
{
try
{
if(ServerMsgQueue->try_receive(&DataTransmision,sizeof(ExchangeData),unused,unused))
{
switch(DataTransmision.CommandType)
{//some cases
default:{std::cout << "UNKNOWN RECEIVED DATA!!!" << std::endl;break;}
}
}
}
catch(interprocess_exception &ex)
{
//this happens always
std::cout << ex.what() << ":" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << std::endl;
}
}
}
编辑:我似乎已经“解决”了。。神奇地 似乎此代码就是错误:
static unsigned int unused;
if(ServerMsgQueue->try_receive(&DataTransmision,sizeof(ExchangeData),unused,unused))
改成
unsigned int Priority;
size_t sizexxx;
if(ServerMsgQueue->try_receive(&DataTransmision,sizeof(ExchangeData),sizexxx,Priority))
有人能解释一下为什么这个代码有效而其他代码无效吗?这很可能是由于您使用了
/zp1
。您的程序及其链接到的库将认为结构具有不同的大小和/或成员位置。不要这样做。请参阅的源代码(从消息队列调用::try_receive)do_receive
首先将消息大小写入“out”参数recvd_size
(代码中对unused
的引用),然后将优先级写入“out”参数priority
(也是代码中对未使用的的引用,从而有效地用优先级
值覆盖recvd_大小
)。紧接着,对memcpy
的调用使用了现在不正确的recvd_大小
,导致您观察到的行为(复制不完整或缓冲区溢出,具体取决于优先级
)
可以说,库的作者应该为memcpy
字节数使用原始的top\u msg->len
(而不是“out”参数recvd\u size
)。但是,为了避免这些意外情况,始终为“out”参数使用不同的变量(如果您愿意,可以称它们为unused1
和unused2
。大小似乎是一样的:(使用了以下代码:MessageBox(空,字符串格式(“Pos(%d):Quat(%d):NPCDataFoot(%d):npcdata(%d):npcdata(%d)”,sizeof(Pos),sizeof(Quat),sizeof(NPCDataFoot),sizeof(npcdata),sizeof(npcdata),sizeof(ExchangeData)).c_str(),“报告大小客户端”,0);
)
unsigned int Priority;
size_t sizexxx;
if(ServerMsgQueue->try_receive(&DataTransmision,sizeof(ExchangeData),sizexxx,Priority))