BastEt 阅读(1496) 评论(0)
封装了一些简单的函数

#pragma once

namespace QuickFAST
{
namespace Messages
{
class FieldSet;
class Sequence;
}
}

extern "C"
{
struct hq_fast_message
{
const QuickFAST::Messages::FieldSet *msg;
};

struct hq_fast_sequence
{
const QuickFAST::Messages::Sequence *seq;
};
int hq_fast_seq_get_size(hq_fast_sequence &sequence);
bool hq_fast_seq_get_msg(hq_fast_sequence &sequence,int index,hq_fast_message &msg);

bool hq_fast_msg_get_uint(hq_fast_message &msg,unsigned int fieldid,unsigned __int64 &value);
bool hq_fast_msg_get_sint(hq_fast_message &msg,unsigned int fieldid,__int64 &value);
bool hq_fast_msg_get_double(hq_fast_message &msg,unsigned int fieldid,double &value);

unsigned __int64 hq_fast_msg_get_uint2(hq_fast_message &msg,unsigned int fieldid,unsigned __int64 defaultvalue=0);
__int64 hq_fast_msg_get_sint2(hq_fast_message &msg,unsigned int fieldid,__int64 defaultvalue=0);
double hq_fast_msg_get_double2(hq_fast_message &msg,unsigned int fieldid,double defaultvalue=0.0f);

int hq_fast_msg_get_str(hq_fast_message &msg,unsigned int fieldid,char *buf,int buflen);

bool hq_fast_msg_get_sequence(hq_fast_message &msg,const char *fieldid,hq_fast_sequence &sequence);
bool hq_fast_msg_get_sequence2(hq_fast_message &msg,unsigned int fieldid,hq_fast_sequence &sequence);

typedef bool (*HQFASTCB)(hq_fast_message &msg,unsigned int tid,void *pdata);

struct hq_fast_decoder;
hq_fast_decoder *hq_fast_create_decoder(const char *xmldeffile);
void hq_fast_decoder_set_cb(hq_fast_decoder &decoder,void *pdata,HQFASTCB cbfunc);
int hq_fast_decoder_parse(hq_fast_decoder &decoder,void *pdata,int datalen);
void hq_fast_reset_decoder(hq_fast_decoder *dc,bool resettempid=true);
void hq_fast_destroy_decoder(hq_fast_decoder *dc);

};


#include
inline std::string hq_fast_msg_get_str2(hq_fast_message &msg,unsigned int fieldid,const char *defaultvalue="")
{
char buf[64]={0};
if(hq_fast_msg_get_str(msg,fieldid,buf,63)<0)
return defaultvalue;
return std::string(buf);
}

#include
#include
#include
#include
#include
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include
#include
#include

#include "XmlTemplateParser_Pugixml.h"


class FastFixConsumer : public QuickFAST::Codecs::MessageConsumer
{
public:
FastFixConsumer()
{
pdata=NULL;
cb=NULL;
}

~FastFixConsumer(){}

virtual bool consumeMessage(QuickFAST::Messages::Message & message)
{
if(!cb)
return true;
hq_fast_message msg;
msg.msg=&message;
return cb(msg,message.gettid(),pdata);
}
virtual bool wantLog(unsigned short level){return false;}
virtual bool logMessage(unsigned short level, const std::string & logMessage){return true;}
virtual bool reportDecodingError(const std::string & errorMessage){return true;}
virtual bool reportCommunicationError(const std::string & errorMessage){return true;}
virtual void decodingStarted(){}
virtual void decodingStopped(){}

HQFASTCB cb;
void *pdata;
};

struct hq_fast_decoder
{
public:
QuickFAST::Codecs::Decoder *decoder;
QuickFAST::Codecs::GenericMessageBuilder *builder;
QuickFAST::Codecs::TemplateRegistryPtr tmeplate_registry;
FastFixConsumer msg_consumer;
};

void hq_fast_reset_decoder(hq_fast_decoder *dc,bool resettempid)
{
dc->decoder->reset(resettempid);
}

hq_fast_decoder *hq_fast_create_decoder(const char *xmldeffile)
{
QuickFAST::Codecs::TemplateRegistryPtr tmeplate_registry=QuickFAST::Codecs::parse_template_file(xmldeffile);
if(tmeplate_registry==NULL)
return NULL;
hq_fast_decoder *result=new hq_fast_decoder();
result->tmeplate_registry=tmeplate_registry;
result->builder=new QuickFAST::Codecs::GenericMessageBuilder(result->msg_consumer);
result->decoder=new QuickFAST::Codecs::Decoder(tmeplate_registry);
return result;
}
void hq_fast_decoder_set_cb(hq_fast_decoder &decoder,void *pdata,HQFASTCB cbfunc)
{
decoder.msg_consumer.pdata=pdata;
decoder.msg_consumer.cb=cbfunc;
}
int hq_fast_decoder_parse(hq_fast_decoder &decoder,void *pdata,int datalen)
{
QuickFAST::Codecs::DataSourceBuffer dsb((unsigned char *)pdata,datalen);
int idecodeitems=0;
for(int i=0;i<40960;i++)
{
try
{
if(dsb.bytesAvailable()<=0)
break;
decoder.decoder->decodeMessage(dsb,*decoder.builder);
idecodeitems++;
}
catch(std::exception &excep)
{
//printf("ERROR:%s:%s\n",msgtype.c_str(),excep.what());
break;
}
catch(...)
{
//printf("unknown error:%s\n",msgtype.c_str());
break;
}
}
return idecodeitems;
}
void hq_fast_destroy_decoder(hq_fast_decoder *dc)
{
if(dc==NULL)
return;
delete dc->builder;
delete dc->decoder;
delete dc;
}

bool hq_fast_msg_get_uint(hq_fast_message &msg,unsigned int fieldid,unsigned __int64 &value)
{
return msg.msg->getUnsignedInteger(fieldid,value);
}
bool hq_fast_msg_get_sint(hq_fast_message &msg,unsigned int fieldid,__int64 &value)
{
return msg.msg->getSignedInteger(fieldid,value);
}
bool hq_fast_msg_get_double(hq_fast_message &msg,unsigned int fieldid,double &value)
{
QuickFAST::Decimal decimal;
if(!msg.msg->getDecimal(fieldid,decimal))
return false;
value=decimal;
return true;
}
unsigned __int64 hq_fast_msg_get_uint2(hq_fast_message &msg,unsigned int fieldid,unsigned __int64 defaultvalue)
{
return msg.msg->getSignedInteger2(fieldid,defaultvalue);
}
__int64 hq_fast_msg_get_sint2(hq_fast_message &msg,unsigned int fieldid,__int64 defaultvalue)
{
return msg.msg->getUnsignedInteger2(fieldid,defaultvalue);
}
double hq_fast_msg_get_double2(hq_fast_message &msg,unsigned int fieldid,double defaultvalue)
{
return msg.msg->getDecimal2(fieldid,defaultvalue);
}

int hq_fast_msg_get_str(hq_fast_message &msg,unsigned int fieldid,char *buf,int buflen)
{
const QuickFAST::StringBuffer *buffield=NULL;
if(!msg.msg->getString(fieldid,buffield))
return -1;
int copybytes=buffield->size();
if(copybytes>buflen)
copybytes=buflen;
memcpy(buf,buffield->c_str(),copybytes);
return copybytes;
}

bool hq_fast_msg_get_sequence(hq_fast_message &msg,const char *fieldid,hq_fast_sequence &sequence)
{
sequence.seq=NULL;
QuickFAST::Messages::SequenceCPtr result;
if(!msg.msg->getSequence(fieldid,result))
return false;
sequence.seq=result.get();
return true;
}
bool hq_fast_msg_get_sequence2(hq_fast_message &msg,unsigned int fieldid,hq_fast_sequence &sequence)
{
sequence.seq=NULL;
QuickFAST::Messages::SequenceCPtr result;
if(!msg.msg->getSequence(fieldid,result))
return false;
sequence.seq=result.get();
return true;
}

int hq_fast_seq_get_size(hq_fast_sequence &sequence)
{
return sequence.seq->size();
}
bool hq_fast_seq_get_msg(hq_fast_sequence &sequence,int index,hq_fast_message &msg)
{
msg.msg=NULL;
const QuickFAST::Messages::FieldSetCPtr fsc=sequence.seq->operator[](index);
msg.msg=fsc.get();
return true;
}

发表评论
切换编辑模式