c++ - 尝试制作 AMF3 数据包

标签 c++ apache-flex amf

我正在尝试制作 AMF 数据包。我正在使用 https://github.com/Ventero/amf-cpp AMF3 的 C++ 实现,但它不包含所有需要的变量。 AMF0 文档描述了如何构建 AMF 数据包 ( http://download.macromedia.com/pub/labs/amf/amf0_spec_121207.pdf ) 前两个字节指定数据包版本:

version = U16 It could be 0 or 3

U16 是大端无符号 16 位整数 (网络)字节序

所以我创造了

typedef unsigned int u16;

在 amf.hpp 中 我现在正在尝试将所有 header 信息添加到单个 std::vector 变量。 我写道:

std::vector<u16> buf = std::vector<u16>{3};  //packet version
std::vector<u16> buf2 = std::vector<u16>{0}; //header-count
std::vector<u16> buf3 = std::vector<u16>{1}; //message count

std::vector<u16> data;

buffermain.insert(data.end(), buf.begin(), buf.end());
buffermain.insert(data.end(), buf2.begin(), buf2.end());
buffermain.insert(data.end(), buf3.begin(), buf3.end());

在结果数据中,我刚刚首先插入了 vector (buf)。

//编辑 我取得了一些进步。

Serializer serializer;
QByteArray outArray; //to insert amf bytes, and send it later

AmfArray Content;
AmfObject Object;

Object.addSealedProperty("Source", AmfNull());
Object.addSealedProperty("operation", AmfNull());
Object.addSealedProperty("clientId", AmfNull());
Object.addSealedProperty("destination", AmfNull());
Object.addSealedProperty("messageId", AmfNull());
Object.addSealedProperty("timestamp", AmfNull());
Object.addSealedProperty("timeToLive", AmfNull());
Object.addSealedProperty("timeToLive", AmfNull());
Object.addSealedProperty("body", AmfNull());
Object.addSealedProperty("headers", AmfNull());

Content.push_back(Object);
serializer << Content;

std::vector<uint8_t> data2 = serializer.data();

char* datas = reinterpret_cast<char*>(data2.data());//

std::vector<unsigned __int32> v;
v.reserve(data.size());

char* sizes = reinterpret_cast<char*>(v.data()); //teoretical size of message in U32

char null = 0;
outArray.append(null); //version first byte
outArray.append(3); //version second byte
outArray.append(null); //header count first byte
outArray.append(null); //header count second byte
outArray.append(null); //messages count first byte
outArray.append(1); //messages count second byte

outArray.append(null); //"Target" lenght first byte 
outArray.append(4); //"Target" lenght second byte 

outArray.append(QByteArray::fromHex("6e756c6c")); // "Target" value

outArray.append(null); // "Response" length first byte
outArray.append(2); // "Response" length second byte

outArray.append(QByteArray::fromHex("2f31"));

outArray.append(sizes); //insert theoretical length of message
outArray.append(datas); //insert message

最佳答案

(我是 amf-cpp 的作者)

我刚刚向 amf-cpp 添加了 AMF3 数据包支持。要使用它,您必须创建一个 AmfPacket 对象,添加 header (PacketHeader) 或消息 (PacketMessage),然后序列化 Amf 数据包。这是一个简单的示例(为简洁起见,它假设类似于 using namespace amf;):

AmfPacket packet;
// first, we construct a simple header in-place
packet.headers.emplace_back(
    "SomeHeader", // header name
    false, // must understand?
    AmfString("Value") // header value
);

// set up the message value
AmfObject object;
// add some properties
object.addSealedProperty("prop", AmfString("val"));
AmfArray content;
content.push_back(object);
// now construct the message in-place
packet.messages.emplace_back(
    "com/example/Object.method", // target uri
    "/1/onResult", // response-uri
    content // value
);

// amf::v8 is a typedef for std::vector<uint8_t>
v8 data = packet.serialize();

生成的 data 对象可以与您的 QDataStream 一起使用。

将来,如果您发现 amf-cpp 缺少您想要使用的功能,请随时在 GitHub issue tracker 上打开错误报告.

关于c++ - 尝试制作 AMF3 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21371396/

相关文章:

Java:传输包含空值的双数据类型数组需要多少字节?

c++ - 优化 uint8 的最大递减量

c++ - <=>(飞船)运算符(operator)的发音

c++ - 前向声明和命名空间 (c++)

apache-flex - Flex 3 中的动画 Gif

apache-flex - AMF 和 AMF3 规范

c++ - 如何初始化不可访问模板类的静态成员?

apache-flex - 我应该使用哪个 hibernate 适配器来处理 BlazeDS/Spring 与 Flex 集成项目中的延迟初始化?

apache-flex - Adobe AIR/Flex 构建的活跃开源项目列表

asp.net - FluorineFX 是否足够成熟以用于大型 Web 应用程序?