ED2KParser< Parent > Class Template Reference

ED2KParser template class provides a generic interface for parsing ED2K network stream. More...

#include <parser.h>

List of all members.


Public Member Functions

 ED2KParser (Parent *parent)
 The one and only constructor, this initializes the packet parser to parse a stream.
void parse (const std::string &data)
 Continue stream parsing, passing additional data.
Accessors and modifiers for internal data
void setParent (Parent *p)
Parent * getParent () const
bool hasBuffered () const
void clearBuffer ()

Private Types

typedef std::map< uint8_t,
PacketFactory * > 
FactoryMap
typedef FactoryMap::iterator Iter
typedef std::map< uint8_t,
FactoryMap >::iterator 
FIter

Private Member Functions

bool readPacket (std::istringstream &i, InternalPacket &p)
 readPacket() method attempts to read a single packet from the designated stream.

Static Private Member Functions

static std::map< uint8_t,
FactoryMap > & 
factories ()
 While it would be syntactically possible to implement the static data as member of the ED2KParser class, it seems to cause SIGSEGV upon module loading during static data initialization.

Private Attributes

std::string m_buffer
 Internal data buffer.
Parent * m_parent
 Pointer to packets handler class.
InternalPacket m_packet
 Packet currently being parsed.
uint32_t m_need
 When parsing long packets (e.g.

Classes

struct  InternalPacket
 InternalPacket structure is a temporary storage for a single packet data. More...
class  PacketFactory
 PacketFactory is an abstract base class for specific packet factories, which handle specific packet construction and user callbacks after packet construction. More...

Detailed Description

template<typename Parent>
class ED2KParser< Parent >

ED2KParser template class provides a generic interface for parsing ED2K network stream.

The input data is sent to the parser object through parse() member functions, which performs the data parsing. When a packet has been detected in stream, ED2KParser first locates the correct packet-factory to handle the packet, and passes the packet data to the factory. The specific packet factories in turn pass the data to the actual packet object which then performs the final packet parsing and packet object construction. Once the packet has been constructed, the packet-factory in question calls back to the parser client (specified through Parent template argument and parent argument in ED2KParser constructor), calling onPacket() member function and passing the newly created packet object (by reference) to the function. This allows client code to implement overloaded versions of onPacket to perform event-driven packet handling.

Parameters:
Parent Parent class which will receive packet events

Definition at line 55 of file parser.h.


Member Typedef Documentation

template<typename Parent>
typedef std::map<uint8_t, PacketFactory*> ED2KParser< Parent >::FactoryMap [private]
 

Definition at line 177 of file parser.h.

template<typename Parent>
typedef FactoryMap::iterator ED2KParser< Parent >::Iter [private]
 

Definition at line 178 of file parser.h.

template<typename Parent>
typedef std::map<uint8_t, FactoryMap>::iterator ED2KParser< Parent >::FIter [private]
 

Definition at line 179 of file parser.h.


Constructor & Destructor Documentation

template<typename Parent>
ED2KParser< Parent >::ED2KParser Parent *  parent  )  [inline]
 

The one and only constructor, this initializes the packet parser to parse a stream.

Parameters:
parent Pointer to object to which notifications should be sent. Must not be null.

Definition at line 64 of file parser.h.

References ED2KParser< Parent >::m_need, and ED2KParser< Parent >::m_parent.


Member Function Documentation

template<typename Parent>
void ED2KParser< Parent >::setParent Parent *  p  )  [inline]
 

Definition at line 72 of file parser.h.

References ED2KParser< Parent >::m_parent.

template<typename Parent>
Parent* ED2KParser< Parent >::getParent  )  const [inline]
 

Definition at line 73 of file parser.h.

References ED2KParser< Parent >::m_parent.

template<typename Parent>
bool ED2KParser< Parent >::hasBuffered  )  const [inline]
 

Definition at line 74 of file parser.h.

References ED2KParser< Parent >::m_buffer.

template<typename Parent>
void ED2KParser< Parent >::clearBuffer  )  [inline]
 

Definition at line 75 of file parser.h.

References ED2KParser< Parent >::m_buffer.

template<typename Parent>
void ED2KParser< Parent >::parse const std::string &  data  )  [inline]
 

Continue stream parsing, passing additional data.

The data is buffered internally, so

Parameters:
data may be freed after passing to this method. Note that this function triggers a chain-reaction of events when a new packet is detected, which leads back to client code, into the relevant packet handler function. When this function returns, all found packets in stream have been parsed, and all remaining data has been buffered for next parsing sequence.
data Data buffer to be parsed.

Definition at line 89 of file parser.h.

References ED2KParser< Parent >::factories(), ED2KParser< Parent >::m_buffer, ED2KParser< Parent >::InternalPacket::m_data, ED2KParser< Parent >::m_need, ED2KParser< Parent >::InternalPacket::m_opcode, ED2KParser< Parent >::m_packet, ED2KParser< Parent >::m_parent, ED2KParser< Parent >::InternalPacket::m_proto, and ED2KParser< Parent >::readPacket().

template<typename Parent>
static std::map<uint8_t, FactoryMap>& ED2KParser< Parent >::factories  )  [inline, static, private]
 

While it would be syntactically possible to implement the static data as member of the ED2KParser class, it seems to cause SIGSEGV upon module loading during static data initialization.

While similar approach may work within the main application, it's a no-go within a module, and thus we wrap it inside a member function which returns the object by reference.

Returns:
Static map of supported packet factories. The outer map is really small, and contains only two entries - PR_ED2K and PR_EMULE, since those two protocol's are used in ed2k network. The inner map lists all packets in the given protocol. When a new packet is found in stream, the inner map corresponding to the protocol is searched for the packet opcode, and the relevant factory's create() method called, passing the packet data.

Definition at line 198 of file parser.h.

Referenced by ED2KParser< Parent >::PacketFactory::PacketFactory(), and ED2KParser< Parent >::parse().

template<typename Parent>
bool ED2KParser< Parent >::readPacket std::istringstream &  i,
InternalPacket p
[inline, private]
 

readPacket() method attempts to read a single packet from the designated stream.

Parameters:
i Stream to read the packet from
p Packet object to store the packet data in
Returns:
True if packet was found and read; false otherwise
Exceptions:
std::out_of_range if only part of the packet could be read. This generally indicates you should try again later when more data is available.
std::runtime_error on fatal errors. If this is thrown, the stream should be marked unusable, and exception propagated up to client code.

Definition at line 244 of file parser.h.

References ED2KPacket::addOverheadDn(), Zlib::decompress(), ED2KParser< Parent >::m_need, OP_PACKEDCHUNK, OP_SENDINGCHUNK, PR_ED2K, PR_EMULE, and PR_ZLIB.

Referenced by ED2KParser< Parent >::parse().


Member Data Documentation

template<typename Parent>
std::string ED2KParser< Parent >::m_buffer [private]
 

Internal data buffer.

Definition at line 274 of file parser.h.

Referenced by ED2KParser< Parent >::clearBuffer(), ED2KParser< Parent >::hasBuffered(), and ED2KParser< Parent >::parse().

template<typename Parent>
Parent* ED2KParser< Parent >::m_parent [private]
 

Pointer to packets handler class.

Definition at line 275 of file parser.h.

Referenced by ED2KParser< Parent >::ED2KParser(), ED2KParser< Parent >::getParent(), ED2KParser< Parent >::parse(), and ED2KParser< Parent >::setParent().

template<typename Parent>
InternalPacket ED2KParser< Parent >::m_packet [private]
 

Packet currently being parsed.

Definition at line 276 of file parser.h.

Referenced by ED2KParser< Parent >::parse().

template<typename Parent>
uint32_t ED2KParser< Parent >::m_need [private]
 

When parsing long packets (e.g.

data packets - 10k long), this indicates how much more data is needed before completing the packet. This is used for optimizing, on order to reduce the amount of "failed" packet parse attempts.

Definition at line 284 of file parser.h.

Referenced by ED2KParser< Parent >::ED2KParser(), ED2KParser< Parent >::parse(), and ED2KParser< Parent >::readPacket().


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