184 lines
4.7 KiB
C++
184 lines
4.7 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/jetfix/codec/MessageTypes.h"
|
|
#include "lib/tracing/PerfLog.h"
|
|
|
|
namespace bts {
|
|
namespace jetfix {
|
|
|
|
// holds the location and size of a single raw FIX message inside a
|
|
// shared buffer. allows rudimentary parsing for key fields.
|
|
|
|
class InMsgBuffer
|
|
{
|
|
public:
|
|
InMsgBuffer () : _data (),_size (),_type (),_buffer (),_trace() {}
|
|
InMsgBuffer (InMsgBuffer const&src) : _data (src._data),
|
|
_size (src._size),
|
|
_type (src._type),
|
|
_buffer (src._buffer),
|
|
_trace(src._trace)
|
|
{}
|
|
|
|
InMsgBuffer (std::string const &data) : _data (),_size (),_type (),_buffer (),_trace()
|
|
{
|
|
_buffer = std::make_shared<std::string> (data);
|
|
_data = _buffer->data ();
|
|
_size = _buffer->size ();
|
|
_type = get_type ();
|
|
}
|
|
|
|
InMsgBuffer (const char*data, size_t sz, std::shared_ptr<std::string> const &buffer)
|
|
: _data (data),
|
|
_size (sz),
|
|
_type (get_type ()),
|
|
_buffer (buffer),
|
|
_trace()
|
|
{}
|
|
|
|
InMsgBuffer &operator=(InMsgBuffer const&msg)
|
|
{
|
|
_data = msg._data;
|
|
_size = msg._size;
|
|
_type = msg._type;
|
|
_buffer = msg._buffer;
|
|
_trace = msg._trace;
|
|
return *this;
|
|
}
|
|
|
|
const char*data () const
|
|
{return _data;}
|
|
|
|
size_t size () const
|
|
{return _size;}
|
|
|
|
std::string to_str () const
|
|
{
|
|
return std::string (_data,_size);
|
|
}
|
|
|
|
MessageTypes type () const
|
|
{return _type;}
|
|
|
|
uint32_t get_seq_num () const
|
|
{return get_uint_field ("\00134=",sizeof("\00134=")-1);}
|
|
|
|
uint32_t get_last_seq_num_processed () const
|
|
{return get_uint_field ("\001369=",sizeof("\001369=")-1);}
|
|
|
|
uint32_t get_new_seq_no () const
|
|
{return get_uint_field ("\00136=",sizeof("\00136=")-1);}
|
|
|
|
uint32_t get_begin_seq_no () const
|
|
{return get_uint_field ("\0017=",sizeof("\0017=")-1);}
|
|
|
|
uint32_t get_end_seq_no () const
|
|
{return get_uint_field ("\00116=",sizeof("\00116=")-1);}
|
|
|
|
bool get_poss_dup_flag () const
|
|
{return get_uint_field ("\00143=",sizeof("\00143=")-1)=='Y';}
|
|
bool has_poss_dup_flag_flag () const
|
|
{
|
|
auto loc =
|
|
find_field ("\00143=",sizeof("\00143=")-1);
|
|
return loc.first != nullptr;
|
|
}
|
|
|
|
bool has_test_req_id () const
|
|
{
|
|
auto loc =
|
|
find_field ("\001112=",sizeof("\001112=")-1);
|
|
return loc.first != nullptr;
|
|
}
|
|
|
|
bool has_text () const
|
|
{
|
|
auto loc =
|
|
find_field ("\00158=",sizeof("\00158=")-1);
|
|
return loc.first != nullptr;
|
|
}
|
|
|
|
std::string get_text () const
|
|
{
|
|
if (has_text ())
|
|
return get_string_field ("\00158=",sizeof("\00158=")-1);
|
|
else
|
|
return "";
|
|
}
|
|
|
|
std::string get_test_req_id () const
|
|
{return get_string_field ("\001112=",sizeof("\001112=")-1);}
|
|
|
|
trace::CorID const&trace() const {return _trace;}
|
|
|
|
void set_trace(trace::CorID const&t) {_trace = t;}
|
|
|
|
private:
|
|
const char *_data;
|
|
size_t _size;
|
|
MessageTypes _type;
|
|
std::shared_ptr<std::string> _buffer;
|
|
trace::CorID _trace;
|
|
|
|
MessageTypes get_type () const
|
|
{
|
|
auto loc = find_field ("\00135=",sizeof("\00135=")-1);
|
|
bts_assert (loc.first);
|
|
uint32_t result = 0;
|
|
for (auto i = 0ul; i < loc.second; i++) {
|
|
result *= 256;
|
|
result += *(loc.first+i);
|
|
}
|
|
return (MessageTypes) result;
|
|
}
|
|
|
|
uint32_t get_uint_field (const char *field, size_t field_sz) const
|
|
{
|
|
auto loc = find_field (field,field_sz);
|
|
bts_assert (loc.first);
|
|
uint32_t result = 0;
|
|
for (auto i = 0ul; i < loc.second; i++) {
|
|
result *= 10;
|
|
result += *(loc.first+i) - '0';
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::string get_string_field (const char *field, size_t field_sz) const
|
|
{
|
|
auto loc = find_field (field,field_sz);
|
|
bts_assert (loc.first);
|
|
return std::string (loc.first,loc.second);
|
|
}
|
|
|
|
std::pair<const char*,uint32_t> find_field (const char*fn,size_t field_sz) const
|
|
{
|
|
auto begin = data ();
|
|
auto end = begin + size ();
|
|
|
|
while (begin != end) {
|
|
auto it = std::find (begin,end,*fn);
|
|
if (it == end || (end - it) < (int) field_sz)
|
|
break;
|
|
else if (memcmp (it,fn,field_sz) == 0) {
|
|
// found it
|
|
it += field_sz;
|
|
auto field_start = it;
|
|
auto field_stop = std::find (field_start,end,1);
|
|
bts_assert (field_stop != end);
|
|
return std::make_pair (field_start, field_stop - field_start);
|
|
}
|
|
else
|
|
begin++;
|
|
}
|
|
return std::make_pair<const char*,uint32_t> (nullptr,0);
|
|
}
|
|
};
|
|
|
|
}}
|