-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.hpp
52 lines (40 loc) · 1.94 KB
/
encoder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once
#include "types/numbers.hpp"
#include "types/message.hpp"
#include "types/encode.hpp"
#include "encoder/encode_method.hpp"
#include "encode_settings.hpp"
#include "tools/bitwise.hpp"
namespace ctrader {
namespace encode {
template<connection Connection, encode_options Settings = default_encode_settings >
class encoder {
public:
template<request Request, typename... Field>
void encode(Field... fields) {
if constexpr (Request == request::heart_beat) { heart_beat.prepare(msg_seq_num, fields...); }
if constexpr (Request == request::test_req) { test_req.prepare(msg_seq_num, fields...); }
if constexpr (Request == request::logon) { logon.prepare(msg_seq_num, fields...); }
if constexpr (Request == request::market_data_req) { market_data_req.prepare(msg_seq_num, fields...); }
advance_seq_num();
}
inline void advance_seq_num() { msg_seq_num++; }
inline void reduce_seq_num() { msg_seq_num -= bitwise::gte(msg_seq_num, 1); }
inline void reset_seq_num() { msg_seq_num = 1; }
inline u64 get_seq_num() const { return msg_seq_num; }
template<request Request>
packet_t<Settings, Request> const& get_buffer() const {
if constexpr (Request == request::heart_beat) { return heart_beat.buff; }
if constexpr (Request == request::test_req) { return test_req.buff; }
if constexpr (Request == request::logon) { return logon.buff; }
if constexpr (Request == request::market_data_req) { return market_data_req.buff; }
}
private:
u64 msg_seq_num = 1u;
encode_method<Connection, Settings, request::heart_beat> heart_beat;
encode_method<Connection, Settings, request::test_req> test_req;
encode_method<Connection, Settings, request::logon> logon;
encode_method<Connection, Settings, request::market_data_req> market_data_req;
};
}
}