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/>.
19 #ifndef NL_FE_SEND_SUB_H
20 #define NL_FE_SEND_SUB_H
22 #include "nel/misc/types_nl.h"
23 #include <nel/misc/md5.h>
25 #include "fe_receive_sub.h"
30 const uint SYNCSendLatency
= 100; // number of ms between sends of SYNC system message
31 const uint PROBESendLatency
= 300; // number of ms between sends of PROBE system message
35 * Front-end Send Subsystem
36 * \author Olivier Cado
37 * \author Nevrax France
50 typedef bool TSBState
;
52 /// Destination address
53 NLNET::CInetAddress DestAddress
;
55 /// Used (connected) or not
56 volatile TSBState SBState
;
61 /// Default constructor
62 CSendBuffer() : SBState(false), OutBox(false, 512) {} // prealloc 512 bytes to avoid bitmemstream reallocation
64 /// Set the new address. state should be SBReady or SBNotReady only.
65 void setAddress( const NLNET::CInetAddress
*addr
, TSBState state
)
67 // Copy the address (just a link would not work)
72 /// Enable or disable the current address
73 void enableSendBuffer( TSBState state
)
78 /// Send the current outbox
79 void sendOutBox( NLNET::CUdpSock
*datasock
);
82 /// Vector for send buffers indexed by TClientId
83 typedef std::vector
<CSendBuffer
> TSendBuffers
;
85 /// Set of ids of buffers to enable in the flushing buffers before the next swapping
86 typedef std::set
<TClientId
> TBuffersToEnable
;
95 _TotalBitBandwidth(0xFFFFFFFF),
96 _ClientBitBandwidth(150*8),
104 void init( NLNET::CUdpSock
*datasock
, THostMap
*clientmap
, CHistory
*history
, CPrioSub
*priosub
);
109 /// Set client bandwidth per cycle in bytes
110 void setClientBandwidth( uint32 bytes
);
112 /// Set total bandwidth per cycle in bytes (limited to 512 MB) (currently not used!)
113 void setTotalBandwidth( uint32 bytes
)
115 if ( bytes
< 536870912 ) // 512 MB
116 _TotalBitBandwidth
= bytes
*8;
118 _TotalBitBandwidth
= 0xFFFFFFFF; // prevent from overflow
121 uint32
clientBandwidth() const { return _ClientBitBandwidth
; }
123 uint32
totalBandwidth() const { return _TotalBitBandwidth
; }
125 TClientIdCont
& clientIdCont() { return _ClientIdCont
; }
127 volatile uint32
&sendCounter() { return _SendCounter
; }
129 /// Set the address for send buffer, to match a connected client
130 void setSendBufferAddress( TClientId id
, const NLNET::CInetAddress
*addr
)
132 // We set the address for both, but we can't enable the buffer yet
133 ((*_CurrentFillingBuffers
)[id
]).setAddress( addr
, false /*true*/ );
134 ((*_CurrentFlushingBuffers
)[id
]).setAddress( addr
, false );
135 //_BuffersToEnable.insert( id ); // OBSOLETE: now it is enabled/disabled in CFeSendSub::fillPrioritizedActions
138 /// Unset a send buffer, when a client disconnects
139 void disableSendBuffer( TClientId id
)
141 // We can disable both, because no problem if the flushing thread sees the state of a buffer change to unused
142 ((*_CurrentFillingBuffers
)[id
]).enableSendBuffer( false );
143 ((*_CurrentFlushingBuffers
)[id
]).enableSendBuffer( false );
145 // But we must remove the id for _BuffersToEnable in the case when there was
146 // a connection and then a disconnection for the same client in the same cycle
147 // (maybe, not necessary to manage, because unlikely to happen)
148 //_BuffersToEnable.erase( id ); // OBSOLETE
151 /// Enable a send buffer, the first time when the buffer is ready to be flushed out
152 void enableSendBuffer( TClientId id
)
154 // In the filling one, we enabled the buffer as soon as the client connected
155 ((*_CurrentFlushingBuffers
)[id
]).enableSendBuffer( true );
158 /// Access the outbox (for filling)
159 TOutBox
& outBox( TClientId id
)
161 return (*_CurrentFillingBuffers
)[id
].OutBox
;
164 /// Call before a send cycle (even if there is no client)
165 void prepareSendCycle()
168 _NbImpulseActions
= 0;
171 /// Setup headers for outgoing messages of current cycle (only if there are clients)
172 void prepareHeadersAndFillImpulses();
174 /// Fill prioritized actions into outgoing messages (only if there are clients)
175 void fillPrioritizedActions();
177 /// Swap send buffers
178 void swapSendBuffers();
180 /// Send outgoing messages
181 void flushMessages();
186 NLNET::CUdpSock
*_DataSock
;
188 /** Client id container (insert()/erase() are done by the receive subsystem who manages the clients).
189 * For a vector, there are as many elements as possibles clients, non-attributed elements are set to NULL.
191 TClientIdCont _ClientIdCont
;
193 /// Urgent-important actions
194 // CUrgentImportantActions _UIActions;
197 THostMap
*_ClientMap
;
199 /// Packet History access
205 /** Max total bandwidth (bytes per update)
206 * Warning: uint32 for bits => limitation is 512 KB ((2^32-1)/8))
207 * see setTotalBitBandwidth
209 uint32 _TotalBitBandwidth
;
211 /// Max bandwidth per client (bytes per update)
212 uint32 _ClientBitBandwidth
;
214 /// Number of bits stored for the current cyle
215 //uint32 _OutputBits;
217 /// Number of messages effectively sent (flushed)
218 volatile uint32 _SendCounter
;
220 /// Number of actions stored for the current cycle (important + prioritized)
223 /// Number of important actions stored for the current cycle
224 uint32 _NbImpulseActions
;
226 TSendBuffers _SendBuffers1
, _SendBuffers2
;
227 TSendBuffers
*_CurrentFillingBuffers
, *_CurrentFlushingBuffers
;
229 /// MD5 hash keys of msg.xml and database.xml
230 NLMISC::CHashKeyMD5 _MsgXmlMD5
;
231 NLMISC::CHashKeyMD5 _DatabaseXmlMD5
;
233 //TBuffersToEnable _BuffersToEnable; // OBSOLETE
237 #endif // NL_FE_SEND_SUB_H
239 /* End of fe_send_sub.h */