1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
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/>.
20 #ifndef FRONTEND_SERVICE_H
21 #define FRONTEND_SERVICE_H
23 #include "nel/misc/time_nl.h"
24 #include "nel/misc/command.h"
25 #include "nel/misc/variable.h"
26 #include "nel/misc/file.h"
27 #include "nel/misc/stop_watch.h"
28 #include "nel/misc/command.h"
29 #include "nel/misc/hierarchical_timer.h"
30 #include "nel/net/login_cookie.h"
31 #include "nel/net/service.h"
32 #include "nel/net/udp_sock.h"
34 #include "fe_receive_sub.h"
35 #include "fe_send_sub.h"
39 #include "client_id_lookup.h"
46 #define DECLARE_CF_CALLBACK( varname ) \
47 friend void cfcb##varname( NLMISC::CConfigFile::CVar& var )
58 typedef CHashMap
< TDataSetIndex
, std::string
> TEntityNamesMap
;
60 extern NLMISC::CVariable
<bool> UseWebPatchServer
;
61 extern NLMISC::CVariable
<bool> AcceptClientsAtStartup
;
62 extern NLMISC::CVariable
<std::string
> PatchingURLFooter
;
65 // Return the name of an entity (if previously retrieved or "" if no name)
66 std::string
getEntityName( const TDataSetRow
& entityIndex
);
69 void beepIfAllowed( uint freq
, uint duration
);
71 // Disconnection by UserId (requested by the login system)
72 void cbDisconnectClient (TUid userId
, const std::string
&reqServiceName
);
74 // send an impulsion to a client
75 void sendImpulsion( TClientId clientid
, NLNET::CMessage
& msgin
, uint8 channel
, const char *extendedMsg
, bool forceSingleShot
);
79 //#define MEASURE_SENDING
81 //extern CDebugDisplayer FEDebugDisplayer;
84 * CFrontEndService, based on IService5
86 class CFrontEndService
: public NLNET::IService
, public NLMISC::ICommandsHandler
90 /// Return the instance of the service
91 static CFrontEndService
*instance() { return (CFrontEndService
*)IService::getInstance(); }
97 virtual void release();
100 virtual bool update();
102 /// After mirror system is ready
105 CFeReceiveSub
*receiveSub() { return &_ReceiveSub
; }
106 CFeSendSub
*sendSub() { return &_SendSub
; }
107 CHistory
*history() { return &_History
; }
108 CEntityContainer
& entityContainer() { return _EntityContainer
; }
110 /// Priority Subsystem
119 UserDurationPAverage(10),
120 ProcessVisionWatch(10),
121 BackEndRecvWatch1(5),
122 BackEndRecvWatch2(5),
123 BackEndRecvWatch3(5),
127 SentActionsLastCycle(0),
128 //ScannedPropsLastCycle(0),
129 VisibilityDistance(250000), // 250 m
131 AcceptClients(false),
133 _UpdateDuration(100),
135 _ClientLagTime(3000),
136 _ClientTimeOut(10000),
144 /// Called when there is a tick
147 /// returns the number of bit that the level can manage (biggest action it can manage)
148 uint
getImpulseMaxBitSize(uint level
) { return CImpulseEncoder::maxBitSize (level
); }
150 /// Send an impulse to a given client (0<=level<=3)
151 void addImpulseToClient(TClientId client
, CLFECOMMON::CActionImpulsion
*action
, uint level
);
153 /// Send an impulse to a given entity, provided it is a client (0<=level<=3)
154 void addImpulseToEntity( const TEntityIndex
& entity
, CLFECOMMON::CActionImpulsion
*action
, uint level
);
156 /// Remove clients that do not send datagrams anymore
157 void updateClientsStates();
159 /// Set clients to stalled or "server down" mode (and send stalled msg immediately)
160 void sendServerProblemStateToClients( uint connectionState
);
162 /// Set clients to synchronize mode
163 void setClientsToSynchronizeState();
165 /// Monitor client (disabled if 0)
166 void monitorClient( TClientId id
) { MonitoredClient
= id
; }
168 /// Set game cycle ratio
169 void setGameCycleRatio( sint gcratio
) { _GCRatio
= gcratio
; _GCCount
= 0; }
171 /// Callback called when a cookie become acceptable (a new player as logged
172 /// in and will connect here soon).
173 static void newCookieCallback(const NLNET::CLoginCookie
&cookie
);
176 /// StopWatch value for stats
177 NLMISC::CStopWatch ReceiveWatch
; // All Receive Sub
178 NLMISC::CStopWatch SendWatch
; // All Send Sub
179 NLMISC::CStopWatch UserLWatch
;
180 NLMISC::CStopWatch CycleWatch
;
181 NLMISC::TMsDuration UserDurationPAverage
; // Userloop
183 //NLMISC::CStopWatch HeadWatch; // Sending: Setup header
184 //NLMISC::CStopWatch FillWatch; // Sending: Filling impulse and prioritized
185 //NLMISC::CStopWatch SndtWatch; // Sending: Flushing
187 NLMISC::CStopWatch ProcessVisionWatch
; // PrioSub: Process Vision
188 NLMISC::CStopWatch BackEndRecvWatch1
; // Netloop: cbDeltaUpdate
189 NLMISC::CStopWatch BackEndRecvWatch2
; // Netloop: cbDeltaUpdateRemove
190 NLMISC::CStopWatch BackEndRecvWatch3
; // Netlopp: cbDeltaNewVision
192 uint32 SentActionsLastCycle
;
194 NLMISC::TTime LastTickTime
;
196 NLMISC::TTime LastStallTime
;
198 /// Visibility distance
199 CLFECOMMON::TCoord VisibilityDistance
;
201 /// Flag set at init and when the shard is known as down by a disconnection callback (EGS/IOS)
204 /// Entity names (debugging purpose)
205 TEntityNamesMap EntityNames
;
207 /// If not null, output stats about this client (debugging purpose)
208 TClientId MonitoredClient
;
215 /// Initialises module callback and module manager
216 void initModuleManagers();
218 DECLARE_CF_CALLBACK( PriorityMode
);
219 DECLARE_CF_CALLBACK( TotalBandwidth
);
220 DECLARE_CF_CALLBACK( ClientBandwidth
);
221 DECLARE_CF_CALLBACK( LimboTimeOut
);
222 DECLARE_CF_CALLBACK( ClientTimeOut
);
223 DECLARE_CF_CALLBACK( AllowBeep
);
224 DECLARE_CF_CALLBACK( GameCycleRatio
);
225 DECLARE_CF_CALLBACK( CalcDistanceExecutionPeriod
);
226 DECLARE_CF_CALLBACK( SortPrioExecutionPeriod
);
227 DECLARE_CF_CALLBACK( DistanceDeltaRatioForPos
);
228 /*DECLARE_CF_CALLBACK( PositionPrioExecutionPeriod );
229 DECLARE_CF_CALLBACK( OrientationPrioExecutionPeriod );
230 DECLARE_CF_CALLBACK( DiscreetPrioExecutionPeriod );*/
234 CEntityContainer _EntityContainer
;
236 /// Update duration (ms)
237 uint32 _UpdateDuration
;
239 /// Length of datagrams
242 /// Lag time to probe for a client that does not send datagrams anymore (in ms)
243 uint32 _ClientLagTime
;
245 /// Time-out to "disconnect" a client that does not send datagrams anymore (in ms)
246 uint32 _ClientTimeOut
;
248 /// Time-out to "disconnect" a client that is in limbo mode and that doesn't ack
249 uint32 _LimboTimeOut
;
251 /// Receive Subsystem
252 CFeReceiveSub _ReceiveSub
;
261 std::vector
< ::CModuleManager
*> _ModuleManagers
;
263 /// Counter for game cycle ratio
269 virtual const std::string
&getCommandHandlerName() const
271 static std::string
name("fe");
275 NLMISC_COMMAND_HANDLER_TABLE_BEGIN(CFrontEndService
)
276 NLMISC_COMMAND_HANDLER_ADD(CFrontEndService
, dump
, "dump the frontend internal state", "no param");
277 NLMISC_COMMAND_HANDLER_TABLE_END
279 NLMISC_CLASS_COMMAND_DECL(dump
);