# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

How to create a new service ?

Author(s):
Olivier Cado

Date:
Updated May 4, 2001

Introduction

A service is set of functionnalities provided by a server. The class NLNET::IService is the base class of all services.

NLNET::IService performs automatically the basic functionnalities of any service, such as registration to the Naming Service, server start-up and shutdown.

NLNET::IService is located in NeL, but user-defined services are located in /code/nelns.

How to create a user-defined service ?

  1. Create a class inherited from NLNET::IService (optional).
  2. Create an array of callbacks, called CallbackArray. Example :
    void cbProcessConnection( CMessage& message, TSockId from, CCallbackNetBase& nb )
    {
            // Process here
    }
    
    void cbProcessReceivedMsg( CMessage& message, TSockId from, CCallbackNetBase& nb )
    {
            // Process incoming message here
    }
    
    TCallbackItem CallbackArray[] =
    {
            { "LOG", cbProcessReceivedMsg },
            { "C", cbProcessConnection }
    };
  3. Reimplement the methods init(), update() and release() if you need to (optional). After each call to update(), CNetManager::update() is automatically called, so that the server can handle incoming messages (by calling the callbacks) and flush buffers of outgoing data.
  4. Add in your source file the NLNET_SERVICE_MAIN line, with the following arguments :
  • The name of your class (or NLNET::IService if you do not inherit from it (*))
  • The short name of your service
  • Its long name
  • The chosen port number (set it to 0 for "auto-assigned by the naming service")
  • The callback array
Example:
NLNET_SERVICE_MAIN( CMyService, "MS", "my_service", 0, CallbackArray );

(*) Note: if your service does nothing else calling your callbacks, i.e. init(), update() and release() are empty, you don't need to create a new class : you can use IService directly and write IService instead of CMyService.

To run your service, create a file "<service_long_name>.cfg" in the working directory, containing the location of your naming service.

Example: my_service.cfg:

NSHost = "itsalive.nevrax.org";
NSPort = 50000;

Examples

See the nelns services source code for examples on how to create a class inherited from IService.