通过 PHP 的 Unpack 函数读取结构体中的结构体
我想知道如何通过 php 的 unpack 函数读取结构中的结构。当我收到 IS_MCI 数据包时,我检查它的类型以确保它等于 ISP_MCI,然后检查 NumC 以找出该数据包中有多少个 CompCar 结构。问题是尝试通过单个函数将这些内容解压到数组中。我总是得到一个未定义的偏移量。所以,我正在寻找对此事的新看法。
你会如何处理这个数据包?
有问题的结构是这样的:
struct IS_MCI // Multi Car Info - if more than 8 in race then more than one of these is sent
{
byte Size; // 4 + NumC * 28
byte Type; // ISP_MCI
byte ReqI; // 0 unless this is a reply to an TINY_MCI request
byte NumC; // number of valid CompCar structs in this packet
CompCar Info[8]; // car info for each player, 1 to 8 of these (NumC)
};
struct CompCar // Car info in 28 bytes - there is an array of these in the MCI (below)
{
word Node; // current path node
word Lap; // current lap
byte PLID; // player's unique id
byte Position; // current race position : 0 = unknown, 1 = leader, etc...
byte Info; // flags and other info - see below
byte Sp3;
int X; // X map (65536 = 1 metre)
int Y; // Y map (65536 = 1 metre)
int Z; // Z alt (65536 = 1 metre)
word Speed; // speed (32768 = 100 m/s)
word Direction; // direction of car's motion : 0 = world y direction, 32768 = 180 deg
word Heading; // direction of forward axis : 0 = world y direction, 32768 = 180 deg
short AngVel; // signed, rate of change of heading : (16384 = 360 deg/s)
};
I want to know how to read a struct within a struct via php's unpack function. When I get an IS_MCI packet, I check it's Type to make sure it's equal to ISP_MCI, and then I check NumC to find out how many CompCar structs there are within this packet. The problem is trying to unpack these contents into an array via a single function. I always get a undefined offset. So, i'm looking for some fresh eyes on the matter.
How would you handle this packet?
The struct in question is this:
struct IS_MCI // Multi Car Info - if more than 8 in race then more than one of these is sent
{
byte Size; // 4 + NumC * 28
byte Type; // ISP_MCI
byte ReqI; // 0 unless this is a reply to an TINY_MCI request
byte NumC; // number of valid CompCar structs in this packet
CompCar Info[8]; // car info for each player, 1 to 8 of these (NumC)
};
struct CompCar // Car info in 28 bytes - there is an array of these in the MCI (below)
{
word Node; // current path node
word Lap; // current lap
byte PLID; // player's unique id
byte Position; // current race position : 0 = unknown, 1 = leader, etc...
byte Info; // flags and other info - see below
byte Sp3;
int X; // X map (65536 = 1 metre)
int Y; // Y map (65536 = 1 metre)
int Z; // Z alt (65536 = 1 metre)
word Speed; // speed (32768 = 100 m/s)
word Direction; // direction of car's motion : 0 = world y direction, 32768 = 180 deg
word Heading; // direction of forward axis : 0 = world y direction, 32768 = 180 deg
short AngVel; // signed, rate of change of heading : (16384 = 360 deg/s)
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在
,该代码做出了一些您可能不希望认为是理所当然的假设(即添加更多的错误/读取数据处理)。
Size
必须介于 0...228 之间,NumC 必须介于 0...8 之间,并且两个值必须匹配在一起,依此类推。word
,我使用了v
,它代表“无符号短整型(始终为 16 位,小尾数字节顺序)。但是对于int
我使用了l
:“signed long(始终为32位,机器字节顺序)”,但在我的机器上搜索文档。数据字节顺序的协议$msg 中的测试数据取自结果 。的
prints
Now, that code makes some assumptions that you might not want to take for granted (i.e. add a lot more error/read-data handling).
Size
must be between 0...228, NumC must be between 0...8 and both values must fit together and so on.word
I've usedv
which stands for "unsigned short (always 16 bit, little endian byte order). But forint
I've usedl
: "signed long (always 32 bit, machine byte order)". That's ok on my machine. But search the documentation of the protocol for the endianness of the data.The testdata in $msg has been taken from the result of
我正在使用这个:
和 CompCar 类:
一切都工作正常!
I'm using this:
And CompCar class:
And everything it's working fine!