senf::PacketTypeMixin< Self, Registry > Class Template Reference

Mixin to provide standard implementations for nextPacketRange and nextPacketType. More...

#include <senf/Packets/PacketType.hh>

Public Types

typedef Registry::key_t key_t
 
typedef boost::optional< key_toptional_key_t
 

Static Public Member Functions

static optional_key_t key (Packet const &p)
 Find key of packet from registry. More...
 
static PacketTypeBase::factory_t lookup (key_t key)
 Lookup the key in the registry. More...
 

PacketType interface implementation

static PacketInterpreterBase::optional_range nextPacketRange (ConcretePacket< Self > const &p)
 
static PacketInterpreterBase::factory_t nextPacketType (ConcretePacket< Self > const &p)
 
static PacketInterpreterBase::size_type initSize ()
 
static void init (ConcretePacket< Self > const &p)
 

Detailed Description

template<class Self, class Registry = void>
class senf::PacketTypeMixin< Self, Registry >

Mixin to provide standard implementations for nextPacketRange and nextPacketType.

This mixin class simplifies the definition of simple packets:

  • The packets consist of three sections: The header, the payload and an optional trailer.
  • If the packet has a trailer, both the header and the trailer must have a fixed size.

This mixin provides the nextPacketRange() member as well as initSize() and init(). If you additionally provide the optional Registry argument, PacketTypeMixin provides a simple implementation of nextPacketType().

When using the PacketTypeMixin, the implementation of a packet is simplified to:

// Here 'SomeRegistryTag' is optional
struct SimplePacketType
public senf::PacketTypeMixin<SimplePacketType, SomeRegistryTag>
{
typedef SomePacketParser parser;
using mixin::nextPacketRange;
using mixin::nextPacketType; // Only if the optional 'Registry' argument is provided
using mixin::initSize;
using mixin::init;
static key_t nextPacketKey(packet p)
{
// Return the key in the registry under which the next packet
// header is to be found. This member must be given if a Registry argument is
// passed to the PacketTypeMixin template.
return p->typeField();
}
static void finalize(packet p)
{
// Set the type field by querying the type of the next packet. This is an
// optional assignment: If the key is not found, the value returned by 'key'
// is an empty optional and the assignment will be skipped.
p->typeField << key(p.next(senf::nothrow));
// optionally complete the packet by generating auto-generated information
// (like checksums)
}
static void dump(packet p, std::ostream & os)
{
// Write out a readable representation of the packet for debug purposes
}
static interpreter::size_type initHeadSize()
{
// This member is optional. It returns the \e fixed header size if the packet has a
// trailer.
return header_size;
}
};
    Most of the members are optional, which reduces the minimal implementation of a packet to:
struct SimplePacketType
public senf::PacketTypeMixin<SimplePacketType, SomeRegistryTag>
{
typedef SomePacketParser parser;
using mixin::nextPacketRange;
using mixin::nextPacketType;
using mixin::initSize;
using mixin::init;
// 'typeField()' is one of the fields defined in the parser which holds
// the next-header information
static key_t nextPacketKey(packet p) { return p->typeField(); }
static void finalize(packet p) { p->typeField() << key(p.next(senf::nothrow)); }
static void dump(packet p) {
// should always be implemented although optional
}
};

Definition at line 300 of file PacketType.hh.

Member Typedef Documentation

◆ key_t

template<class Self, class Registry = void>
typedef Registry::key_t senf::PacketTypeMixin< Self, Registry >::key_t

Definition at line 303 of file PacketType.hh.

◆ optional_key_t

template<class Self, class Registry = void>
typedef boost::optional<key_t> senf::PacketTypeMixin< Self, Registry >::optional_key_t

Definition at line 304 of file PacketType.hh.

Member Function Documentation

◆ init()

template<class Self, class Registry = void>
static void senf::PacketTypeMixin< Self, Registry >::init ( ConcretePacket< Self > const &  p)
static

◆ initSize()

template<class Self, class Registry = void>
static PacketInterpreterBase::size_type senf::PacketTypeMixin< Self, Registry >::initSize ( )
static

◆ key()

template<class Self, class Registry = void>
static optional_key_t senf::PacketTypeMixin< Self, Registry >::key ( Packet const &  p)
static

Find key of packet from registry.

key() will query the registry to find the key of the given packet. Whereas nextPacketKey() as implemented by the mixin user will provide the registry key of the next packet from information stored in the current packets header, the key() member will look up the type of packet p in the registry and return it's key.

If either p is an in - valid() packet or the packet type is not found in the registry, the returned optional value will be empty.

◆ lookup()

template<class Self, class Registry = void>
static PacketTypeBase::factory_t senf::PacketTypeMixin< Self, Registry >::lookup ( key_t  key)
static

Lookup the key in the registry.

lookup() will query the registry and find the factory for the given key. If the key cannot be found, no_factory() will be returned.

◆ nextPacketRange()

template<class Self, class Registry = void>
static PacketInterpreterBase::optional_range senf::PacketTypeMixin< Self, Registry >::nextPacketRange ( ConcretePacket< Self > const &  p)
static

◆ nextPacketType()

template<class Self, class Registry = void>
static PacketInterpreterBase::factory_t senf::PacketTypeMixin< Self, Registry >::nextPacketType ( ConcretePacket< Self > const &  p)
static

The documentation for this class was generated from the following file: