Only send Negotiate if the session is new

This commit is contained in:
Eric Harding committed 2020-02-28 15:33:27 -06:00
1 parent 95830fafdf
commit 6fb5b83249
3 files changed
+142 -43

No files matched your search

+7 -6
View File
@@ -1,12 +1,13 @@
Session ID Access ID Secret Key Creation Date Expiration Date Environment
Session ID Access ID Secret Key Creation Date Expiration Date Environment
M4D lLtjH74y1Kh5XUJTLyH2 f1K_tROQp2lTIplVp34WxKa7xDUgfFnIteolfik061o 2019-09-03 2020-09-03 CERT
M4D lLtjH74y1Kh5XUJTLyH2 f1K_tROQp2lTIplVp34WxKa7xDUgfFnIteolfik061o 2019-09-03 2020-09-03 CERT
MC1 VEMlkDuLz74OVHSuacQB IuHyYT3b-9KUo3WsuXykt-fXsjzp7L-JOrdCSQZLNl0 2019-09-03 2020-09-03 CERT
MC1 VEMlkDuLz74OVHSuacQB IuHyYT3b-9KUo3WsuXykt-fXsjzp7L-JOrdCSQZLNl0 2019-09-03 2020-09-03 CERT
MC5 77lzSUwPLlM9obaXh2mK z6aA9qfASDKfZkYTVKFqJd-78i0HZ2Q9ec3CKCYIqHg 2019-09-03 2020-09-03 CERT
MC5 77lzSUwPLlM9obaXh2mK z6aA9qfASDKfZkYTVKFqJd-78i0HZ2Q9ec3CKCYIqHg 2019-09-03 2020-09-03 CERT
MC7 RphzI3rn71oGUEiYnX9e y6OvvPPyoyj2S8YAnE1gFduXT8v29HIMjO5EngYxSNg 2019-09-03 2020-09-03 CERT
MC7 RphzI3rn71oGUEiYnX9e y6OvvPPyoyj2S8YAnE1gFduXT8v29HIMjO5EngYxSNg 2019-09-03 2020-09-03 CERT
MC8 PYBIm2TI9gFPIxmHbuYl 6o-ZolGH2DdeQOlGtJ5Zqoh_kg7VdbxkmjeOHmigKwU 2019-09-03 2020-09-03 CERT
MC8 PYBIm2TI9gFPIxmHbuYl 6o-ZolGH2DdeQOlGtJ5Zqoh_kg7VdbxkmjeOHmigKwU 2019-09-03 2020-09-03 CERT
NGG 1OXPqNxPhbqbzm0kF92e qlUrwY1KG1Pyq-TcbJgMqYXmBTQoAi-wEEBERRDRZPE 2019-12-20 2020-12-20 NEWRELEASE
+123 -32
View File
@@ -3,18 +3,43 @@
#include "lib/utils/SecureLogon.h"
#include "ILink3Utils.h"
/*
# Some notes about session binding and initialization: #
## Official documentation:
https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Binary+Order+Entry+-+Session+Layer#iLink3BinaryOrderEntry-SessionLayer-WeeklyReset
## Begining of week (new session?)
- config.session = X
- config.outgoing_sequence = 1
-> Negotiate session=X
<- NegotiateResponse session=X, oldsession=Y, oldSeq=N
if oldSeq > 0 -> ReplayRequest
-> Establish seq=1
<- EstablishAck oldsession=Y, oldSeq=N
## Middle of week (same session)
- config.session = X
- config.outgoing_sequence > 1
-> Establish seq=N
*/
namespace bts {
namespace orderman {
namespace ilink3 {
ILinkGW::ILinkGW(IConnection& con, const Config& conf) :
_config(conf),
_xstate(State::INIT),
_x_state(State::INIT),
_buffer_size(16384),
_next(0),
buffer(new char[_buffer_size]),
_on_state_change([](auto){}),
_connection(con),
_sequence(conf.outgoing_sequence),
_msg_count(0),
_time()
{
@@ -26,15 +51,15 @@ void ILinkGW::connect() {
_connection.connect(); // -> on_connect_success
}
// This determines the session uuid
void ILinkGW::send_negotiate() {
// add 2 bytes for the empty 'credentials' (39008) length which must be present
auto msg = frame<iLinkBinary::Negotiate500>(2);
// constant msg.customerFlow()
// constant msg.hMACVersion()
msg.uUID(_config.session);
msg.uUID(_config.session_uuid);
msg.requestTimestamp(_time.now());
msg.putSession(_config.cme_session);
// Need this call to set the length of the credential block to 0
// We need this call to set the length of the credential block to 0
// This behavior is specific to "data" in the xml template
msg.putCredentials("");
msg.putFirm(_config.firm);
const auto ld = glbx::calculate_hmac(
@@ -46,11 +71,26 @@ void ILinkGW::send_negotiate() {
);
msg.putAccessKeyID(ld.access_key);
msg.putHMACSignature(ld.hmac);
flush();
}
void ILinkGW::send_party_details(const api::orderman::RoutingRecord& route) {
void ILinkGW::send_establish() {
// add 2 bytes for the empty 'credentials' (39008) length which must be present
// auto msg = frame<iLinkBinary::Negotiate500>(2);
}
void ILinkGW::send_heartbeat() { send_sequence(); }
void ILinkGW::send_sequence() {
auto msg = frame<iLinkBinary::Sequence506>();
msg.uUID(_config.session_uuid);
msg.nextSeqNo(_sequence);
msg.faultToleranceIndicator(iLinkBinary::FTI::NULL_VALUE);
msg.keepAliveIntervalLapsed(iLinkBinary::KeepAliveLapsed::NotLapsed);
}
void ILinkGW::send_retransmit_request(uint32_t) {
}
uint64_t ILinkGW::send_party_details(const api::orderman::RoutingRecord& route) {
using namespace iLinkBinary;
auto msg = frame<PartyDetailsDefinitionRequest518>(
sizeof(PartyDetailsDefinitionRequest518::NoPartyDetails)
@@ -81,6 +121,8 @@ void ILinkGW::send_party_details(const api::orderman::RoutingRecord& route) {
auto details = msg.noPartyDetailsCount(1);
details.partyDetailRole(PartyDetailRole::CustomerAccount);
// In the future we may check for existing party details definitions and return that id
return 0;
}
bool needs_stop_px(api::orderman::NativeOrderType order_type) {
@@ -93,7 +135,7 @@ void ILinkGW::send_order(const CreateOrderMsg& create) {
bts_assert_d(create.security);
bts_assert_d(create.security->has_glbx());
send_party_details(*create.routing_record);
auto party = send_party_details(*create.routing_record);
auto msg = frame<iLinkBinary::NewOrderSingle514>();
msg.price().mantissa(to_price9(create.price));
@@ -103,7 +145,7 @@ void ILinkGW::send_order(const CreateOrderMsg& create) {
msg.seqNum(next_seq());
msg.putSenderID(create.routing_record->operator_id());
// msg.putClOrdID(create.clordid);
msg.partyDetailsListReqID(0); // Always 0 unless we decide pre-register details
msg.partyDetailsListReqID(party);
msg.orderRequestID(++_msg_count);
msg.sendingTimeEpoch(_time.now());
if (needs_stop_px(create.order_type)) {
@@ -129,8 +171,8 @@ void ILinkGW::send_cancel_replace(const CancelReplaceOrderMsg& replace) {
bts_assert_d(create.routing_record);
bts_assert_d(create.security);
bts_assert_d(create.security->has_glbx());
send_party_details(*create.routing_record);
auto party = send_party_details(*create.routing_record);
auto msg = frame<iLinkBinary::OrderCancelReplaceRequest515>();
msg.orderID(replace.exchange_order_id.value());
@@ -142,7 +184,7 @@ void ILinkGW::send_cancel_replace(const CancelReplaceOrderMsg& replace) {
msg.seqNum(next_seq());
msg.putSenderID(create.routing_record->operator_id());
// msg.putClOrdID(create.clordid);
msg.partyDetailsListReqID(0); // Always 0 unless we decide pre-register details
msg.partyDetailsListReqID(party);
msg.orderRequestID(++_msg_count);
msg.sendingTimeEpoch(_time.now());
if (needs_stop_px(create.order_type)) {
@@ -169,11 +211,11 @@ void ILinkGW::send_cancel(const CancelOrderMsg& cancel) {
bts_assert_d(cancel.security);
bts_assert_d(cancel.security->has_glbx());
send_party_details(*cancel.routing_record);
auto party = send_party_details(*cancel.routing_record);
auto msg = frame<iLinkBinary::OrderCancelRequest516>();
msg.orderID(cancel.exchange_order_id.value());
msg.partyDetailsListReqID(0);
msg.partyDetailsListReqID(party);
msg.manualOrderIndicator(map_manual_order_indicator(cancel.manual_order_indicator));
msg.seqNum(next_seq());
msg.putSenderID(cancel.routing_record->operator_id());
@@ -187,10 +229,12 @@ void ILinkGW::send_cancel(const CancelOrderMsg& cancel) {
}
void ILinkGW::flush() {
_connection.send(std::string_view(buffer.get(), _buffer_size));
// If we're careful to set every field we could technically avoid this memset
memset(buffer.get(), 0, _next);
_next = 0;
if (_next > 0) {
_connection.send(std::string_view(buffer.get(), _buffer_size));
// If we're careful to set every field we could technically avoid this memset
memset(buffer.get(), 0, _next);
_next = 0;
}
}
std::string state_string(State s) {
@@ -206,8 +250,8 @@ std::string state_string(State s) {
}
void ILinkGW::set_state(State s) {
LOG(OrderMan, Diag, "ILinkGW {}: {}->{}",
_config.session, state_string(_xstate), state_string(s));
_xstate = s;
_config.session_uuid, state_string(_x_state), state_string(s));
_x_state = s;
if (_on_state_change) _on_state_change(s);
}
@@ -215,8 +259,20 @@ void ILinkGW::set_state(State s) {
// IConnectionHandler
void ILinkGW::on_connect_success() {
set_state(State::NEGOTATING);
send_negotiate();
if (_config.incoming_sequence = 1) {
set_state(State::NEGOTATING);
// Sequence = 1 indicates a new session
// so we have to go through a negotiate
// to create the session at CME
send_negotiate();
}
else {
set_state(State::ESTABLISHING);
// sequence > 1 indicates an existing session
// We only need to establish the current sequence number
send_establish();
}
flush();
}
void ILinkGW::on_disconnect() {
LOG(OrderMan, Error, "Disconnected.");
@@ -257,7 +313,7 @@ void ILinkGW::on_message(const FramingHeader* header, const char* data) {
constexpr size_t offset = sizeof(FramingHeader) + mheader.encodedLength();
switch(mheader.templateId()) {
// Originally generated by a tool - operations/codegen/ilink3gen
// Generated by a tool - operations/codegen/ilink3gen
case 501: handle<iLinkBinary::NegotiationResponse501>(data, offset, header->message_size); break;
case 502: handle<iLinkBinary::NegotiationReject502>(data, offset, header->message_size); break;
case 504: handle<iLinkBinary::EstablishmentAck504>(data, offset, header->message_size); break;
@@ -294,31 +350,66 @@ void ILinkGW::on_message(const FramingHeader* header, const char* data) {
}
}
// Message Handlers
void ILinkGW::handle(const iLinkBinary::NegotiationResponse501&) {
// TODO: send an establish message
template <class T>
std::string to_string(const T& t) {
std::stringstream buf;
buf << t;
return buf.str();
}
void ILinkGW::handle(const iLinkBinary::NegotiationReject502&) {
// Message Handlers
void ILinkGW::handle(const iLinkBinary::NegotiationResponse501& msg) {
}
void ILinkGW::handle(const iLinkBinary::NegotiationReject502& msg) {
LOG(OrderMan, Error, "Negotiate Reject: {} '{}'.\nRaw: \"{}\"",
msg.errorCodes(),
std::string(translate_negotiation_reject(msg.errorCodes())),
to_string(msg));
set_state(State::ERROR);
_connection.disconnect();
}
void ILinkGW::handle(const iLinkBinary::EstablishmentAck504&) {
}
void ILinkGW::handle(const iLinkBinary::EstablishmentReject505&) {
void ILinkGW::handle(const iLinkBinary::EstablishmentReject505& msg) {
LOG(OrderMan, Error, "Establishment Reject: {} '{}'. NextSeq: {}.\nRaw: \"{}\"",
msg.errorCodes(),
std::string(translate_establish_reject(msg.errorCodes())),
msg.nextSeqNo(),
to_string(msg));
set_state(State::ERROR);
_connection.disconnect();
}
void ILinkGW::handle(const iLinkBinary::Sequence506&) {
void ILinkGW::handle(const iLinkBinary::Sequence506& s) {
if (s.keepAliveIntervalLapsed() == iLinkBinary::KeepAliveLapsed::Lapsed) {
send_heartbeat();
flush();
}
}
void ILinkGW::handle(const iLinkBinary::Terminate507&) {
void ILinkGW::handle(const iLinkBinary::Terminate507& msg) {
LOG(OrderMan, Error, "Terminated by exchange. Code: {} Reason: {}",
msg.errorCodes(),
msg.reason()
);
set_state(State::ERROR);
_connection.disconnect();
}
void ILinkGW::handle(const iLinkBinary::Retransmission509&) {
}
void ILinkGW::handle(const iLinkBinary::RetransmitReject510&) {
}
void ILinkGW::handle(const iLinkBinary::NotApplied513&) {
// Sequence number problem?
}
void ILinkGW::handle(const iLinkBinary::PartyDetailsDefinitionRequestAck519&) {
// Future:
// We can handle the party details
}
void ILinkGW::handle(const iLinkBinary::BusinessReject521&) {
void ILinkGW::handle(const iLinkBinary::BusinessReject521& msg) {
LOG(OrderMan, Error, "Business Reject: {} '{}'.\nRaw: \"{}\"",
msg.businessRejectReasonId(),
std::string(translate_business_reject_reason(msg.businessRejectReasonId())),
to_string(msg));
set_state(State::ERROR);
}
void ILinkGW::handle(const iLinkBinary::ExecutionReportNew522&) {
}
+12 -5
View File
@@ -79,7 +79,9 @@ struct IConnection {
};
struct Config {
uint64_t session;
uint64_t session_uuid;
uint32_t outgoing_sequence;
uint32_t incoming_sequence;
std::string cme_session; // NGG
std::string firm; // 004
bool failover_on_disconnect;
@@ -89,7 +91,8 @@ enum class State {
INIT,
CONNECTING,
NEGOTATING,
TRANSFER,
ESTABLISHING,
RETRANSMITTING,
READY,
ERROR
};
@@ -103,7 +106,7 @@ struct FramingHeader {
// Does not allow re-entrance
class ILinkGW : IConnectionHandler {
Config _config;
State _xstate;
State _x_state;
size_t _buffer_size;
size_t _next;
std::unique_ptr<char[]> buffer;
@@ -117,14 +120,18 @@ public:
void connect();
void send_negotiate();
void send_party_details(const api::orderman::RoutingRecord&);
void send_establish();
void send_heartbeat();
void send_sequence();
void send_retransmit_request(uint32_t from);
uint64_t send_party_details(const api::orderman::RoutingRecord&);
void send_order(const CreateOrderMsg&);
void send_cancel_replace(const CancelReplaceOrderMsg&);
void send_cancel(const CancelOrderMsg&);
void flush();
// state change
State state() { return _xstate; }
State state() { return _x_state; }
void watch_state(OnStateChange o) { _on_state_change = o; }
private: // Functions