我正在使用 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
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;
}
}
}
try_receive
应用程序在正在使用的块中不断发出“boost::interprocess_exception::library_error” 。
在这种情况下我做错了什么?我确定发送的数据大小相同,因为 1)我使用相同的标头和 2)我使用相同的选项进行编译。
我已经确认变量大小与两个程序中的以下代码相同:
MessageBox(NULL,string_format(
"Pos(%d):Quat(%d):NPCDataFoot(%d):NPCDataVehicle(%d):ExchangeData(%d)",
sizeof(Pos),sizeof(Quat),sizeof(NPCDataFoot),sizeof(NPCDataVehicle),sizeof(ExchangeData)).c_str()
,"Reported sizes Client",0);
编辑:我似乎已经“解决”了它。
这段代码似乎是错误:
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))
有人可以解释为什么这有效而其他代码无效吗?