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 4 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/time_nl.h"
33 #include "nel/misc/thread.h"
36 using namespace NLNET
;
37 using namespace NLMISC
;
41 const uint NumThreads
= 10;
42 volatile TTime PingDates
[NumThreads
];
43 volatile bool ServiceReady
= false;
45 void cbPong(CMessage
&msgin
, const std::string
&serviceName
, TServiceId sid
)
48 msgin
.serial( counter
);
49 TTime pingTime
= CTime::getLocalTime()-PingDates
[counter
];
50 PingDates
[counter
] = 0;
51 nlinfo("Received PONG %u (%u ms)", counter
, pingTime
);
53 CMessage
msgout("ACK_POS");
54 CUnifiedNetwork::getInstance()->send("PLS", msgout
);
59 PingDates
[i
] = CTime::getLocalTime();
61 CMessage
msgout("PING");
62 msgout
.serial( counter
);
63 nlinfo( "Send PING %d", counter
);
64 CUnifiedNetwork::getInstance()->send("PS", msgout
);
65 nlinfo( "PING %d sent", counter
);
68 class CPinger
: public IRunnable
71 static volatile uint _PingerCount
;
77 _PingerId
= _PingerCount
++;
90 while (totalPing
< 200)
92 i
= rand()*200/RAND_MAX
+200;
94 if (PingDates
[_PingerId
] == 0)
103 volatile uint
CPinger::_PingerCount
= 0;
106 void cbPos(CMessage
&msgin
, const std::string
&serviceName
, TServiceId sid
)
108 CMessage
msgout("POS");
109 CUnifiedNetwork::getInstance()->send("GPMS", msgout
);
111 nlinfo( "Received POS from %s, send POS to GPMS", serviceName
.c_str());
114 void cbAckPos(CMessage
&msgin
, const std::string
&serviceName
, TServiceId sid
)
116 nlinfo( "Received ACK_POS from %s", serviceName
.c_str());
120 void cbUpPS(const std::string
&serviceName
, TServiceId sid
, void *arg
)
122 nlinfo("Ping Service connecting");
125 void cbDownPS(const std::string
&serviceName
, TServiceId sid
, void *arg
)
127 nlinfo("Ping Service disconnecting");
131 void cbUpService(const std::string
&serviceName
, TServiceId sid
, void *arg
)
133 nlinfo("Service %s %d is up", serviceName
.c_str(), sid
.get());
136 void cbDownService(const std::string
&serviceName
, TServiceId sid
, void *arg
)
138 nlinfo("Service %s %d is down", serviceName
.c_str(), sid
.get());
143 * Callback array for messages received from a client
145 TUnifiedCallbackItem CallbackArray
[] =
149 { "ACK_POS", cbAckPos
}
154 class CFloodService
: public IService
169 // Connect to the ping service
170 CUnifiedNetwork
*instance
= CUnifiedNetwork::getInstance();
172 instance
->setServiceUpCallback("PS", cbUpPS
, NULL
);
173 instance
->setServiceDownCallback("PS", cbDownPS
, NULL
);
175 instance
->setServiceUpCallback("*", cbUpService
, NULL
);
176 instance
->setServiceDownCallback("*", cbDownService
, NULL
);
180 for (i
=0; i
<NumThreads
; ++i
)
182 IRunnable
*runnable
= (IRunnable
*)(new CPinger());
183 IThread
*thread
= IThread::create(runnable
);
191 * Declare a service with the class IService, the names "PS" (short) and "ping_service" (long).
192 * The port is automatically allocated (0) and the main callback array is CallbackArray.
194 NLNET_SERVICE_MAIN( CFloodService
, "FLS", "flood_service", 0, CallbackArray
, "", "" )