StateMachine< Identifier, Packet > Class Template Reference

State machine framework. More...

#include <stateMachine.hpp>

Inheritance diagram for StateMachine< Identifier, Packet >:
[legend]

Classes

class  ConnectionPool
 
class  FunIface
 Main interface for the needs of a state function. More...
 
struct  State
 Represents one connection. More...
 

Public Types

using stateFun = std::function< void(State &, Packet *, FunIface &)>
 This is the signature any state function needs to expose. More...
 
using timeoutFun = std::function< void(State &, FunIface &)>
 This is the signature any timeout function needs to expose. More...
 

Public Member Functions

 StateMachine ()
 
 ~StateMachine ()
 
size_t getStateTableSize ()
 Get the number of tracked connections This is probably only for statistics. More...
 
void registerFunction (StateID id, stateFun function)
 Register a function for a given state. More...
 
void registerEndStateID (StateID endStateID)
 This registers an end state If a connections reaches this id, it will be destroyed. More...
 
void registerStartStateID (StateID startStateID, std::function< void *(ConnectionID)> startStateFun)
 This method describes, how to proceed with incoming connections. More...
 
void registerGetPktCB (std::function< Packet *()> fun)
 Register a callback in order to get new buffer. More...
 
void setConnectionPool (ConnectionPool *cp)
 
void removeState (ConnectionID id)
 Remove a connection. More...
 
void addState (ConnectionID id, State st, BufArray< Packet > &pktsIn)
 Open an outgoing connection. More...
 
void runPktBatch (BufArray< Packet > &pktsIn)
 Run a batch of packets. More...
 

Static Public Attributes

static constexpr auto StateIDInvalid = std::numeric_limits<StateID>::max()
 Represents an invalid StateID. More...
 

Detailed Description

template<class Identifier, class Packet>
class StateMachine< Identifier, Packet >

State machine framework.

This class is a comprehensive framework, on top of which a developer can build high-performance state machines.

It can be specialized to work with different underlying packet packet sources and packet identifiers.

The Identifier needs to have the following form:

class Identifier {
// This class needs to be unique for each packet
class ConnectionID {
bool operator==(const ConnectionID &c) const;
bool operator<(const ConnectionID &c) const;
operator std::string() const;
ConnectionID(const ConnectionID &c);
ConnectionID();
};
struct Hasher {
// This should be uniformly distributed
// Collisions can happen, but are not great
uint64_t operator()(const ConnectionID &c) const;
};
static ConnectionID identify(Packet *pkt);
};

The Packet needs to have the following form:

struct Packet {
void *getData();
uint16_t getDataLen();
void setDataLen(uint16_t l);
uint16_t getBufLen();
};
Template Parameters
IdentifierThis class is used to uniquely identify incoming packets (see above)
PacketThis class wraps around any kind of packet buffer (see above)

Definition at line 66 of file stateMachine.hpp.

Member Typedef Documentation

◆ stateFun

template<class Identifier, class Packet>
using StateMachine< Identifier, Packet >::stateFun = std::function<void(State &, Packet *, FunIface &)>

This is the signature any state function needs to expose.

Definition at line 83 of file stateMachine.hpp.

◆ timeoutFun

template<class Identifier, class Packet>
using StateMachine< Identifier, Packet >::timeoutFun = std::function<void(State &, FunIface &)>

This is the signature any timeout function needs to expose.

Definition at line 86 of file stateMachine.hpp.

Constructor & Destructor Documentation

◆ StateMachine()

template<class Identifier, class Packet>
StateMachine< Identifier, Packet >::StateMachine ( )
inline

Definition at line 571 of file stateMachine.hpp.

◆ ~StateMachine()

template<class Identifier, class Packet>
StateMachine< Identifier, Packet >::~StateMachine ( )
inline

Definition at line 575 of file stateMachine.hpp.

Member Function Documentation

◆ addState()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::addState ( ConnectionID  id,
State  st,
BufArray< Packet > &  pktsIn 
)
inline

Open an outgoing connection.

This is the function you want to call, if you want to connect to a server.

Parameters
idThe connection id this connection will use
stThe state data
pktsInPacket buffer for the state to work with (only one packet)

Definition at line 652 of file stateMachine.hpp.

◆ getStateTableSize()

template<class Identifier, class Packet>
size_t StateMachine< Identifier, Packet >::getStateTableSize ( )
inline

Get the number of tracked connections This is probably only for statistics.

Returns
Number of connections

Definition at line 587 of file stateMachine.hpp.

◆ registerEndStateID()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::registerEndStateID ( StateID  endStateID)
inline

This registers an end state If a connections reaches this id, it will be destroyed.

Parameters
endStateIDthe state in question

Definition at line 610 of file stateMachine.hpp.

◆ registerFunction()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::registerFunction ( StateID  id,
stateFun  function 
)
inline

Register a function for a given state.

This function should be called once for each state you wish to use

Parameters
idID of the state for which to set a function
functionThis function will be called, when a connection is in state id, and gets a packet

Definition at line 597 of file stateMachine.hpp.

◆ registerGetPktCB()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::registerGetPktCB ( std::function< Packet *()>  fun)
inline

Register a callback in order to get new buffer.

Parameters
funFunction to call, if new buffers are needed

Definition at line 632 of file stateMachine.hpp.

◆ registerStartStateID()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::registerStartStateID ( StateID  startStateID,
std::function< void *(ConnectionID)>  startStateFun 
)
inline

This method describes, how to proceed with incoming connections.

If you call this function, you allow for incoming connections. They will start using the parameters.

Parameters
startStateIDAll new connections will start in this state
startStateFunThis function is called to populate the void* for the state (may be nullptr)

Definition at line 621 of file stateMachine.hpp.

◆ removeState()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::removeState ( ConnectionID  id)
inline

Remove a connection.

Using this function you can delete a connection.

Parameters
idThe connection id of the connection to remove

Definition at line 642 of file stateMachine.hpp.

◆ runPktBatch()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::runPktBatch ( BufArray< Packet > &  pktsIn)
inline

Run a batch of packets.

This method is the function you want to call, in order to pump new packets into the state machine. Timeouts get handled, as soon as this function is called.

Parameters
pktsInIncoming packets

Definition at line 692 of file stateMachine.hpp.

◆ setConnectionPool()

template<class Identifier, class Packet>
void StateMachine< Identifier, Packet >::setConnectionPool ( ConnectionPool cp)
inline

Definition at line 634 of file stateMachine.hpp.

Member Data Documentation

◆ StateIDInvalid

template<class Identifier, class Packet>
constexpr auto StateMachine< Identifier, Packet >::StateIDInvalid = std::numeric_limits<StateID>::max()
static

Represents an invalid StateID.

Definition at line 89 of file stateMachine.hpp.


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