1.1消息能用静态方法Session::sendToTarget发送给对方。这个方法用多个签名:
[cpp]
// send a message that already contains a BeginString, SenderCompID, and a TargetCompID
static bool sendToTarget( Message&, const std::string& qualifier = "" )
throw(SessionNotFound&);
// send a message based on the sessionID, convenient for use
// in fromApp since it provides a session ID for incoming
// messages
static bool sendToTarget( Message&, const SessionID& )
throw(SessionNotFound&);
// append a SenderCompID and TargetCompID before sending
static bool sendToTarget( Message&, const SenderCompID&, const TargetCompID&, const std::string& qualifier = "" )
throw(SessionNotFound&);
// pass SenderCompID and TargetCompID in as strings
static bool sendToTarget( Message&, const std::string&, const std::string&, const std::string& qualifier = "" )
throw(SessionNotFound&);
1.2创建消息
就行读取消息,创建消息也有几个类型安全的层次。
1.2.1最低类型安全
这个别出现在你的代码中:!!!
[cpp]
void sendOrderCancelRequest()
{
FIX::Message message;
// BeginString
message.getHeader().setField(8, "FIX.4.2");
// SenderCompID
message.getHeader().setField(49, "TW");
// TargetCompID, with enumeration
message.getHeader().setField(FIX::FIELD::TargetCompID, "TARGET");
// MsgType
message.getHeader().setField(35, 'F');
// OrigClOrdID
message.setField(41, "123");
// ClOrdID
message.setField(11, "321");
// Symbol
message.setField(55, "LNUX");
// Side, with value enumeration
message.setField(54, FIX::Side_BUY);
// Text
message.setField(58, "Cancel My Order!");
FIX::Session::sendToTarget(message);
}
1.2.2高类型安全
我们用field类,代码会清晰而且增加了类型安全。另外,这个是你在需要开发多个消息或者多个版本Fix版本时用的代码: www.zzzyk.com
[cpp]
void sendOrderCancelRequest()
{
FIX::Message message;
FIX::Header header& = message.getHeader();
header.setField(FIX::BeginString("FIX.4.2"));
header.setField(FIX::SenderCompID(TW));
header.setField(FIX::TargetCompID("TARGET"));
header.setField(FIX::MsgType(FIX::MsgType_OrderCancelRequest));
message.setField(FIX::OrigClOrdID("123"));
message.setField(FIX::ClOrdID("321"));
message.setField(FIX::Symbol("LNUX"));
message.setField(FIX::Side(FIX::Side_BUY));
message.setField(FIX::Text("Cancel My Order!"));
FIX::Session::sendToTarget(message);
}
1.2.2最高类型安全(这么做!!!)
最后,强烈推荐用这个类型安全的消息类。这里,构造函数将包含所有required fields,增加正确的消息类型和BeginString 。另外,用set方法代替setField,编译器不会让你增加不是Fix4.1的OrderCancelRequest 成员的field。记住:你将仍能用setField来强制设置任何你想设置的field。www.zzzyk.com
[cpp]
void sendOrderCancelRequest()
{
FIX41::OrderCancelRequest message(
FIX::OrigClOrdID("123"),
FIX::ClOrdID("321"),
FIX::Symbol("LNUX"),
FIX::Side(FIX::Side_BUY));
message.set(FIX::Text("Cancel My Order!"));
FIX::Session::sendToTarget(message, SenderCompID("TW"), TargetCompID("TARGET"));
}