This layer is responsible to find and validate the synchronisation prefix.
namespace comms{// TField is type of the field used to read/write SYNC prefix// TNext is the next layer this one wrapstemplate <typenameTField,typenameTNext>classSyncPrefixLayer{public: // Type of the field object used to read/write SYNC prefix.usingField=TField; // Take type of the ReadIterator from the next layerusingReadIterator=typename TNext::ReadIterator; // Take type of the WriteIterator from the next layerusingWriteIterator=typename TNext::WriteIterator; // Take type of the message interface from the next layerusingMessage=typename TNext::Message; // Take type of the message interface pointer from the next layerusingMsgPtr=typename TNext::MsgPtr; template <typenameTMsgPtr>ErrorStatusread(TMsgPtr& msgPtr,ReadIterator& iter, std::size_t len) { Field field;auto es =field.read(iter, len);if (es != ErrorStatus::Success) {return es; }if (field.value() !=Field().value()) { // doesn't match expectedreturn ErrorStatus::ProtocolError; }returnm_next.read(msgPtr, iter, len -field.length()); } ErrorStatuswrite(constMessage& msg,WriteIterator& iter, std::size_t len) const { Field field;auto es =field.write(iter, len);if (es != ErrorStatus::Success) {return es; }returnm_next.write(msg, iter, len -field.length()); } std::size_tlength(constTMessage& msg) const {returnField().length() +m_next.length(msg); }private: TNext m_next;};} // namespace comms
Note, that the value of the SYNC prefix is expected to be equal to the value of the default constructed TField field type. The default construction value may be set using comms::option::DefaultNumValue option described in Generalising Fields Implementation chapter.
For example, 2 bytes synchronisation prefix 0xab 0xcd with big endian serialisation may be defined as: