Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#Actualnfactorial

Posted 05 June 2013 - 06:18 AM

You can use a jump table, for example. You could define something similar to:

 

struct IPacketHandler
{
    virtual void ProcessMessage( Message& message ) = 0;
};
 
class MessageDispatcher
{
    IPacketHandler* m_packetHandlers[ MAXIMUM_HANDLERS ];
 
public:
    void RegisterHandler( IPacketHandler *packetHandler, int messageId )
    {
        assert( messageId >= 0 && messageId < MAXIMUM_HANDLERS );
        m_packetHandlers[ messageId ] = packetHandler;
    }
 
    void Dispatch( Message &message )
    {
        if ( NULL != m_packetHandlers[ message.id ] )
            m_packetHandlers[ message.id ]->ProcessMessage( message );
    }
}

 

This is not a complete solution, just to demonstrate what I mean. There would need to be more error checking/handling, initialization etc. and it could be expanded on a fair amount also. This is also C++ code, but the equivalent in almost any other language should be pretty similar.

 

n!


#2nfactorial

Posted 05 June 2013 - 06:15 AM

You can use a jump table, for example. You could define something similar to:

 

struct IPacketHandler
{
    virtual void ProcessMessage( Message& message ) = 0;
};
 
class MessageDispatcher
{
    IPacketHandler* m_packetHandlers[ MAXIMUM_HANDLERS ];
 
public:
    void RegisterHandler( IPacketHandler *packetHandler, int messageId )
    {
        assert( messageId >= 0 && messageId < MAXIMUM_HANDLERS );
        m_packetHandlers[ messageId ] = packetHandler;
    }
 
    void Dispatch( Message &message )
    {
        if ( NULL != m_packetHandlers[ message.id ] )
            m_packetHandlers[ message.id ]->ProcessMessage( message );
    }
}

 

This is not a complete solution, just to demonstrate what I mean. There would need to be more error checking/handling, initialization etc. and it could be expanded on a fair amount also.

 

n!


#1nfactorial

Posted 05 June 2013 - 06:13 AM

You can use a jump table, for example. You can define an interface:

struct IPacketHandler
{
    virtual void ProcessMessage( Message& message ) = 0;
};
 
class MessageDispatcher
{
    IPacketHandler* m_packetHandlers[ MAXIMUM_HANDLERS ];
 
public:
    void RegisterHandler( IPacketHandler *packetHandler, int messageId )
    {
        assert( messageId >= 0 && messageIId < MAXIMUM_HANDLERS );
        m_packetHandlers[ messageId ] = packetHandler;
    }
 
    void Dispatch( Message &message )
    {
        if ( NULL != m_packetHandlers[ message.id ] )
            m_packetHandlers[ message.id ]->ProcessMessage( message );
    }
}

 

This is not a complete solution, just to demonstrate what I mean. There would need to be more error checking/handling etc.

 

n!


PARTNERS