Skip to content

Handle your packets/messages from networking easilly.

Notifications You must be signed in to change notification settings

igorsegallafa/message-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MessageHandler

Handle your packets/messages from networking easilly.

Dependencies

  • C++ 17
  • Unordered Map C++ STD Library
  • Vector C++ STD Library
  • Functional C++ STD Library

Usage

Including library

For use this you just need to include "MessageHandler.h" file into your project.

Implementing

How each network interface is implemented on different ways, you will need to modify the code places where uses the Connection (pointer for the sender of message) & MemoryBuffer (pointer for the message buffer) classes.

Also, you will need to customize your packet handler (where messages from socket are received). This is made on Message::HandleMessage function, and you are free to let this how you want.

Example

//Instantiating Message Handler
Handler::Message messageHandler;

//Adding Global Validations (will validate every message handled from this instance)
messageHandler.AddValidation( std::bind( &GlobalValidation1, std::placeholders::_1 ) );
messageHandler.AddValidation( std::bind( &GlobalValidation2, std::placeholders::_1 ) );

//Adding Global Handling (will handle every message handled from this instance)
messageHandler.AddProcessing( std::bind( &GlobalProcessing1, std::placeholders::_1, std::placeholders::_2 ) );

//Handler for specific messages
messageHandler.Handle( 1 ).Validate( std::bind( &Message1Validation1, std::placeholders::_1 ), std::bind( &Message1Validation2, std::placeholders::_1 ) );
messageHandler.Handle( 2 ).Validate( std::bind( &Message2Validation1, std::placeholders::_1 ) );
messageHandler.Handle( 3 ).Process( std::bind( &Message3Process1, std::placeholders::_1 ) );
messageHandler.Handle( 4 );

//Testing
messageHandler.HandleMessage( 1, nullptr );
messageHandler.HandleMessage( 2, nullptr );
messageHandler.HandleMessage( 3, nullptr );
messageHandler.HandleMessage( 4, nullptr );

Message Handlers

Every handler used for some message (validation and processing), must be boolean function. When message is being handled, and at some step this has failed, everything else will be discarded. Example of validation handler:

bool GlobalValidation1( Connection* sender )
{
    if( sender )
    {
        std::cout << "Validating Message Global 1" << std::endl;
        return true;
    }

    return false;
}

Observations

Is important that you adapt the library code for your networking structure. Code files used here is just an example, and a guide for you know what to do.

Credits

About

Handle your packets/messages from networking easilly.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages