mFast解析FAST行情内容

目录

一、准备

二、mFast功能

三、解码

四、结果


mFAST

FAST协议解析(五)开源解析库mFAST的使用 - nil - xuanyu.li

FAST是将明文内容通过一定规则编码为二进制内容,目的是压缩数据,减小传输压力。解码的时候,可以使用一些开源库去解码,例如mFast(C++),openFast是java写的。

一、准备

GitHub - objectcomputing/mFAST at v1.2.1

首先下载最新的mFast源码,然后使用cmake进行配置,mFast依赖boost,所以配置的时候需要指定boost路径,然后configure,generate,接着用VS打开工程。

 打开后编译的时候,提示tinyxml2没有源码,需要手动下载tinyxml2的源码,放在mFast下的tinyxml2文件夹下,然后重新用cmake生成一下。

接着编译,mfast_test项目会提示找不到Catch.hpp,这个文件也是个开源测试文件,只需要一个头文件即可,可以从

https://github.com/philsquared/Catch/releases/download/v1.7.2/catch.hpp

这里下载,然后添加进mfast_test项目即可。

编译完成后,可以运行hello_world项目,展示了一个解码fast的例子。

项目包含目录里制定了mFast库的头文件和boost的头文件

二、mFast功能

hello_world工程:模板写死在代码里,同时代码里给了一段fast编码后的内容,接着对其进行了解析,并转成json格式进行了输出。

fast_type_gen工程:

这个工程用来将xml模板转为.h、.cpp、.inl文件,用法为fast_type_gen.exe template.xml

三、解码

自己写了个解码类,include生成的模板头文件即可进行解码

mfastDecoder.h

#pragma once
#include"template_2_26.h"//模板头文件
#include<iostream>
#include<mFast.h>
#include <mfast/coder/fast_decoder.h>
#include <mfast/json/json.h>

class mfastDecoder
{
public:
	//解码器
	std::shared_ptr<mfast::fast_decoder> m_fastDecoder;
public:
	mfastDecoder();
	//初始化
	void init();
	//解码
	void decoder(std::string&msg);
};

 mfastDecoder.cpp

#include "mfastDecoder.h"

mfastDecoder::mfastDecoder() {
	init();
}

//初始化
void mfastDecoder::init() {
	const mfast::templates_description* descriptions[] = { template_2_26::description() };
	m_fastDecoder = std::make_shared<mfast::fast_decoder>();
	m_fastDecoder->include(descriptions);
}

//解码
void mfastDecoder::decoder(std::string&msg) {
	const char* start = msg.c_str();
	const char* end = start + msg.length();
	bool first_quote = true;
	std::ostringstream json_message;
	while (start != end)
	{
		if (!first_quote)
		{
			mfast::message_cref fastMsg = m_fastDecoder->decode(start, end, false);
			bool result = mfast::json::encode(json_message, fastMsg, 0);
		}
		else
		{
			// first quote need to reset FAST decoder
			mfast::message_cref fastMsg = m_fastDecoder->decode(start, end, true);
			bool result = mfast::json::encode(json_message, fastMsg, 0);
			first_quote = false;
		}
		json_message << std::endl;
	}
	std::cout << "decoder result: \n" << json_message.str() << std::endl;
}

 用法:

std::string _fast;//待解码的fast内容

//解码
mfastDecoder m;
m.decoder(_fast);

四、结果

需要注意的是,待解码的fast内容,如果包含以0x0004c453开头的40字节包头,需要将其去掉,从数据区开始解析

猜你喜欢

转载自blog.csdn.net/ljjjjjjjjjjj/article/details/132666571