1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Layer 5 and Service example, ping server.
21 * This ping service expects pings, sends pongs back.
23 * To run this program, ensure there is a file "ping_service.cfg"
24 * containing the location of the naming service (NSHost, NSPort)
25 * in the working directory. The naming service must be running.
29 // We're using the NeL Service framework, and layer 5
30 #include "nel/net/service.h"
31 #include "nel/misc/displayer.h"
33 using namespace NLNET
;
34 using namespace NLMISC
;
36 CFileDisplayer
fd("ps.log");
39 * Callback function called when receiving a "PING" message
42 * - msgin: the incoming message (coming from a client)
43 * - from: the "sockid" of the sender client
44 * - server: the CCallbackNetBase object (which really is a CCallbackServer object, for a server)
46 * Input (expected message from a client): PING
47 * - uint32: ping counter
49 * Output (sent message to the ping server): PONG
50 * - uint32: ping counter
52 void cbPing(CMessage
&msgin
, const std::string
&serviceName
, TServiceId sid
)
57 msgin
.serial( counter
);
59 // Output (uses layer 4 but this is not really necessary, see server.cpp in layer 3 example)
60 CMessage
msgout("PONG");
61 msgout
.serial( counter
);
62 CUnifiedNetwork::getInstance()->send(sid
, msgout
);
64 nlinfo( "PING -> PONG %u", counter
);
68 void cbUpService(const std::string
&serviceName
, TServiceId sid
, void *arg
)
70 nlinfo("Service %s %d is up", serviceName
.c_str(), sid
.get());
72 // Output (uses layer 4 but this is not really necessary, see server.cpp in layer 3 example)
73 CMessage
msgout("PONG");
74 uint32 counter
= 0xFFFFFFFF;
75 msgout
.serial( counter
);
76 CUnifiedNetwork::getInstance()->send(sid
, msgout
);
79 void cbDownService(const std::string
&serviceName
, TServiceId sid
, void *arg
)
81 nlinfo("Service %s %d is down", serviceName
.c_str(), sid
.get());
86 * Callback array for messages received from a client
88 TUnifiedCallbackItem CallbackArray
[] =
95 class CPingService
: public IService
104 DebugLog
->addDisplayer (&fd
);
105 InfoLog
->addDisplayer (&fd
);
106 WarningLog
->addDisplayer (&fd
);
107 ErrorLog
->addDisplayer (&fd
);
109 // Connect to the ping service
110 CUnifiedNetwork
*instance
= CUnifiedNetwork::getInstance();
112 instance
->setServiceUpCallback("*", cbUpService
, NULL
);
113 instance
->setServiceDownCallback("*", cbDownService
, NULL
);
119 * Declare a service with the class IService, the names "PS" (short) and "ping_service" (long).
120 * The port is automatically allocated (0) and the main callback array is CallbackArray.
122 NLNET_SERVICE_MAIN( CPingService
, "PS", "ping_service", 0, CallbackArray
, "", "" )