// NeL - MMORPG Framework // Copyright (C) 2010 Winch Gate Property Limited // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . /* * Layer 3 and Service example, ping server. * * This ping service expects pings, sends pongs back. * * To run this program, ensure there is a file "ping_service.cfg" * containing the location of the naming service (NSHost, NSPort) * in the working directory. The naming service must be running. */ // We're building a server, using the NeL Service framework #include "nel/net/service.h" using namespace NLNET; using namespace NLMISC; using namespace std; // Must be a pointer to control when to start listening socket and when stop it CCallbackServer *Server; vector Clients; // MESSAGES // *************************************************************************** void clientWantsToConnect ( TSockId from, void *arg ) { // Called when a client wants to connect Clients.push_back (from); } // *************************************************************************** void clientWantsToDisconnect ( TSockId from, void *arg ) { // Called when a client wants to disconnect for (uint i = 0; i < Clients.size(); ++i) if (Clients[i] == from) { Clients.erase(Clients.begin()+i); return; } } /* * Callback function called when receiving a "PING" message * * Arguments: * - msgin: the incoming message (coming from a client) * - from: the "sockid" of the sender client * - server: the CCallbackNetBase object (which really is a CCallbackServer object, for a server) * * Input (expected message from a client): PING * - uint32: ping counter * * Output (sent message to the ping server): PONG * - uint32: ping counter */ void cbPing( CMessage& msgin, TSockId from, CCallbackNetBase& server ) { uint32 counter; // Input msgin.serial( counter ); // Output CMessage msgout( "PONG" ); msgout.serial( counter ); server.send( msgout, from ); nlinfo( "PING -> PONG" ); } /* * Callback array for messages received from a client */ TCallbackItem CallbackArray[] = { { "PING", cbPing } }; // SERVICE // *************************************************************************** class CPingService : public IService { public: void init () { // Init the server on port 3333 Server = new CCallbackServer(); Server->init (3333); Server->setConnectionCallback (clientWantsToConnect, NULL); Server->setDisconnectionCallback (clientWantsToDisconnect, NULL); Server->addCallbackArray (CallbackArray, 1); } bool update () { // this function is called every "loop". you return true if you want // to continue or return false if you want to exit the service. // the loop is called evenly (by default, at least one time per second). Server->update(); return true; } void release () { // Must delete the server here delete Server; } }; /* * Declare a service with the class IService, the names "PS" (short) and "ping_service" (long). * The port is automatically allocated (0) and the main callback array is CallbackArray. */ NLNET_SERVICE_MAIN( CPingService, "PS", "ping_service", 0, EmptyCallbackArray, "", "" )