Skip to content

Commit fcc6814

Browse files
committed
2 parents 23dd5ba + 64d8ddb commit fcc6814

10 files changed

Lines changed: 94 additions & 29 deletions

File tree

Include/raknet/RakPeer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ namespace RakNet
476476
/// \sa RakNetStatistics.h
477477
RakNetStatisticsStruct * GetStatistics( const PlayerID playerId ) override;
478478

479+
#ifndef RAKNET_BUILD_FOR_CLIENT
480+
/// Reserves a number of connection slots for internal use (e.g., NPCs).
481+
/// Reserved slots are subtracted from the maximum peer limit when accepting new connections.
482+
/// \param[in] count: The number of slots to reserve.
483+
void ReserveSlots(unsigned short count) override;
484+
#endif
485+
479486
// --------------------------------------------------------------------------------------------EVERYTHING AFTER THIS COMMENT IS FOR INTERNAL USE ONLY--------------------------------------------------------------------------------------------
480487
/// \internal
481488
RPCMap *GetRPCMap( const PlayerID playerId) override;
@@ -594,6 +601,11 @@ namespace RakNet
594601
///Store number of active peers.
595602
unsigned short activePeersCount;
596603

604+
#ifndef RAKNET_BUILD_FOR_CLIENT
605+
///Store reserved slots, for the times slots are taken outside of RakNet
606+
unsigned short reservedSlots;
607+
#endif
608+
597609
//05/02/06 Just using maximumNumberOfPeers instead
598610
///Store the maximum number of peers able to connect, including reserved connection slots for pings, etc.
599611
//unsigned short remoteSystemListSize;

Include/raknet/RakPeerInterface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,13 @@ namespace RakNet
448448
/// \sa RakNetStatistics.h
449449
virtual RakNetStatisticsStruct * GetStatistics( const PlayerID playerId )=0;
450450

451+
#ifndef RAKNET_BUILD_FOR_CLIENT
452+
/// Reserves a number of connection slots for internal use (e.g., NPCs).
453+
/// Reserved slots are subtracted from the maximum peer limit when accepting new connections.
454+
/// \param[in] count: The number of slots to reserve.
455+
virtual void ReserveSlots(unsigned short count) = 0;
456+
#endif
457+
451458
// --------------------------------------------------------------------------------------------EVERYTHING AFTER THIS COMMENT IS FOR INTERNAL USE ONLY--------------------------------------------------------------------------------------------
452459
/// \internal
453460
virtual RPCMap *GetRPCMap( const PlayerID playerId)=0;

Include/raknet/RakServer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ namespace RakNet
443443
/// Get Remote System data for a player from their ID
444444
virtual RakPeer::RemoteSystemStruct* GetRemoteSystemFromPlayerID(const PlayerID playerId) override;
445445

446+
#ifndef RAKNET_BUILD_FOR_CLIENT
447+
/// Reserves a number of connection slots for internal use (e.g., NPCs).
448+
/// Reserved slots are subtracted from the maximum peer limit when accepting new connections.
449+
/// \param[in] count: The number of slots to reserve.
450+
void ReserveSlots(unsigned short count) override;
451+
#endif
452+
446453
private:
447454
unsigned int seed, nextSeed;
448455
RakNetTime broadcastPingsTime, nextSeedUpdate;

Include/raknet/RakServerInterface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ namespace RakNet
436436

437437
/// Get Remote System data for a player from their ID
438438
virtual RakPeer::RemoteSystemStruct* GetRemoteSystemFromPlayerID(const PlayerID playerId) = 0;
439+
440+
#ifndef RAKNET_BUILD_FOR_CLIENT
441+
/// Reserves a number of connection slots for internal use (e.g., NPCs).
442+
/// Reserved slots are subtracted from the maximum peer limit when accepting new connections.
443+
/// \param[in] count: The number of slots to reserve.
444+
virtual void ReserveSlots(unsigned short count) = 0;
445+
#endif
439446
};
440447
}
441448

SAMPRakNet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ unsigned int SAMPRakNet::messagesLimit_ = 500;
2929
unsigned int SAMPRakNet::messageHoleLimit_ = 3000;
3030
unsigned int SAMPRakNet::acksLimit_ = 3000;
3131
unsigned int SAMPRakNet::networkLimitsBanTime_ = 60000;
32+
float SAMPRakNet::minimumSendBitsPerSecond_ = 96000.0f;
3233
bool SAMPRakNet::logCookies_ = false;
3334
ICore* SAMPRakNet::core_ = nullptr;
3435
FlatHashSet<uint32_t> SAMPRakNet::incomingConnections_;

SAMPRakNet.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class SAMPRakNet
166166
static void SetGracePeriod(unsigned int time) { gracePeriod_ = RakNet::GetTime() + time; }
167167
static RakNet::RakNetTime GetGracePeriod() { return gracePeriod_; }
168168

169+
static void SetMinimumSendBitsPerSecond(float bps) { minimumSendBitsPerSecond_ = bps; }
170+
static inline float GetMinimumSendBitsPerSecond() { return minimumSendBitsPerSecond_; }
171+
169172
static ICore* GetCore() { return core_; }
170173

171174
static void ReplyToOmpClientAccessRequest(SOCKET connectionSocket, const RakNet::PlayerID& playerId, uint32_t encryptionKey);
@@ -272,6 +275,7 @@ class SAMPRakNet
272275
static unsigned int messageHoleLimit_;
273276
static unsigned int acksLimit_;
274277
static unsigned int networkLimitsBanTime_;
278+
static float minimumSendBitsPerSecond_;
275279
static ICore* core_;
276280
static FlatHashSet<uint32_t> incomingConnections_;
277281
static RakNet::RakNetTime gracePeriod_;

Source/RakPeer.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ RakPeer::RakPeer()
177177
maximumIncomingConnections = 0;
178178
maximumNumberOfPeers = 0;
179179
activePeersCount = 0;
180+
#ifndef RAKNET_BUILD_FOR_CLIENT
181+
reservedSlots = 0;
182+
#endif
180183
//remoteSystemListSize=0;
181184
remoteSystemList = 0;
182185
bytesSentPerSecond = bytesReceivedPerSecond = 0;
@@ -292,6 +295,9 @@ bool RakPeer::Initialize( unsigned short maxConnections, unsigned short localPor
292295
// Clear the lookup table. Safe to call from the user thread since the network thread is now stopped
293296
remoteSystemLookup.Clear();
294297
activePeersCount = 0;
298+
#ifndef RAKNET_BUILD_FOR_CLIENT
299+
reservedSlots = 0;
300+
#endif
295301
}
296302

297303
// For histogram statistics
@@ -698,6 +704,9 @@ void RakPeer::Disconnect( unsigned int blockDuration, unsigned char orderingChan
698704
maximumNumberOfPeers = 0;
699705
//remoteSystemListSize = 0;
700706
activePeersCount = 0;
707+
#ifndef RAKNET_BUILD_FOR_CLIENT
708+
reservedSlots = 0;
709+
#endif
701710

702711
// Free any packets the user didn't deallocate
703712
Packet **packet;
@@ -2520,6 +2529,13 @@ RakNetStatisticsStruct * RakPeer::GetStatistics( const PlayerID playerId )
25202529
return 0;
25212530
}
25222531

2532+
#ifndef RAKNET_BUILD_FOR_CLIENT
2533+
void RakPeer::ReserveSlots(unsigned short count)
2534+
{
2535+
reservedSlots = count;
2536+
}
2537+
#endif
2538+
25232539
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
25242540
/*
25252541
void RakPeer::RemoveFromRequestedConnectionsList( const PlayerID playerId )
@@ -2828,8 +2844,14 @@ RakPeer::RemoteSystemStruct * RakPeer::AssignPlayerIDToRemoteSystemList( const P
28282844
RakAssert(playerId!=UNASSIGNED_PLAYER_ID);
28292845

28302846
// Check if maximum number of peers is reached .. without looping them.
2831-
if (activePeersCount == maximumNumberOfPeers)
2847+
#ifndef RAKNET_BUILD_FOR_CLIENT
2848+
if (activePeersCount >= maximumNumberOfPeers - reservedSlots)
2849+
#else
2850+
if (activePeersCount >= maximumNumberOfPeers)
2851+
#endif
2852+
{
28322853
return 0;
2854+
}
28332855

28342856
// remoteSystemList in user thread
28352857
for ( i = 0; i < maximumNumberOfPeers; i++ )
@@ -4653,7 +4675,7 @@ namespace RakNet
46534675
}
46544676

46554677
// Taken from SA-MP 0.3.7 changes
4656-
if ((remoteSystem->connectMode == RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode == RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY) && timeMS - remoteSystem->lastReliableSend > 20000)
4678+
if ((remoteSystem->connectMode == RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode == RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY) && timeMS > remoteSystem->connectionTime && timeMS - remoteSystem->lastReliableSend > 20000)
46574679
{
46584680
packet = AllocPacket(sizeof(char));
46594681
packet->data[0] = ID_CONNECTION_LOST; // DeadConnection
@@ -4663,7 +4685,7 @@ namespace RakNet
46634685

46644686
AddPacketToProducer(packet);
46654687

4666-
CloseConnectionInternal(playerId, false, true, 0);
4688+
CloseConnectionInternal(playerId, false, true, 2);
46674689
continue;
46684690
}
46694691

@@ -5177,19 +5199,19 @@ namespace RakNet
51775199
}
51785200
}
51795201
#endif
5180-
else
5202+
else if (data[0] == (unsigned char)ID_CONNECTION_LOST)
51815203
{
5182-
if (data[0]>=(unsigned char)ID_RPC)
5183-
{
5184-
packet=AllocPacket(byteSize, data);
5185-
packet->bitSize = bitSize;
5186-
packet->playerId = playerId;
5187-
packet->playerIndex = ( PlayerIndex ) remoteSystemIndex;
5188-
AddPacketToProducer(packet);
5189-
}
5190-
//else
5191-
// Some internal type got returned to the user?
5192-
//RakAssert(0);
5204+
// Do nothing - packet is received through network.
5205+
// This shouldn't happen as it's an internal packet.
5206+
delete[] data;
5207+
}
5208+
else if (data[0] >= (unsigned char)ID_RPC)
5209+
{
5210+
packet = AllocPacket(byteSize, data);
5211+
packet->bitSize = bitSize;
5212+
packet->playerId = playerId;
5213+
packet->playerIndex = (PlayerIndex)remoteSystemIndex;
5214+
AddPacketToProducer(packet);
51935215
}
51945216
}
51955217

Source/ReliabilityLayer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ using namespace RakNet;
4545

4646
static const int DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE=512;
4747
static const float PACKETLOSS_TOLERANCE=.02f; // What percentile packetloss we are willing to accept as background noise.
48-
static const double MINIMUM_SEND_BPS=14400.0; // Won't go below this send rate
49-
static const double STARTING_SEND_BPS=28800.0; // What send rate to start at.
5048
static const float PING_MULTIPLIER_TO_RESEND=3.0; // So internet ping variation doesn't cause needless resends
5149
static const RakNetTime MIN_PING_TO_RESEND=30; // So system timer changes and CPU lag don't send needless resends
5250
static const RakNetTimeNS TIME_TO_NEW_SAMPLE=500000; // How many ns to wait before starting a new sample. This way buffers have time to overflow or relax at the new send rate, if they are indeed going to overflow.
@@ -184,14 +182,14 @@ void ReliabilityLayer::InitializeVariables( void )
184182
splitPacketId = 0;
185183
messageNumber = 0;
186184
availableBandwidth=0;
187-
lastUpdateTime= RakNet::GetTimeNS();
188-
currentBandwidth=STARTING_SEND_BPS;
185+
lastUpdateTime = RakNet::GetTimeNS();
186+
currentBandwidth = SAMPRakNet::GetMinimumSendBitsPerSecond() * 2.0f;
189187
// lastPacketSendTime=retransmittedFrames=sentPackets=sentFrames=receivedPacketsCount=bytesSent=bytesReceived=0;
190188

191189
deadConnection = cheater = false;
192190
lastAckTime = 0;
193191

194-
lowBandwidth=STARTING_SEND_BPS;
192+
lowBandwidth = SAMPRakNet::GetMinimumSendBitsPerSecond();
195193
histogramStartTime=lastUpdateTime+TIME_TO_NEW_SAMPLE+ping*2*1000;
196194
histogramEndTime=histogramStartTime+MAX_TIME_TO_SAMPLE;
197195

@@ -1113,6 +1111,8 @@ void ReliabilityLayer::Update( SOCKET s, PlayerID playerId, int MTUSize, RakNetT
11131111
return;
11141112
}
11151113

1114+
const auto minimumSendBPS = SAMPRakNet::GetMinimumSendBitsPerSecond();
1115+
11161116
// Water canister has to have enough room to put more water in :)
11171117
double requiredBuffer=(float)((MTUSize+UDP_HEADER_SIZE)*8);
11181118
if (requiredBuffer > currentBandwidth)
@@ -1168,8 +1168,8 @@ void ReliabilityLayer::Update( SOCKET s, PlayerID playerId, int MTUSize, RakNetT
11681168
lowBandwidth*=.9;
11691169
}
11701170

1171-
if (lowBandwidth < MINIMUM_SEND_BPS)
1172-
lowBandwidth=MINIMUM_SEND_BPS;
1171+
if (lowBandwidth < minimumSendBPS)
1172+
lowBandwidth=minimumSendBPS;
11731173

11741174
delta = (highBandwidth-lowBandwidth)/2;
11751175
currentBandwidth=delta+lowBandwidth;
@@ -1200,12 +1200,12 @@ void ReliabilityLayer::Update( SOCKET s, PlayerID playerId, int MTUSize, RakNetT
12001200
if (packetloss > .2)
12011201
{
12021202
lowBandwidth/=2;
1203-
if (lowBandwidth < MINIMUM_SEND_BPS)
1204-
lowBandwidth=MINIMUM_SEND_BPS;
1203+
if (lowBandwidth < minimumSendBPS)
1204+
lowBandwidth=minimumSendBPS;
12051205
}
12061206

12071207
delta = (highBandwidth-lowBandwidth)/2;
1208-
if (delta < MINIMUM_SEND_BPS/4)
1208+
if (delta < minimumSendBPS/4)
12091209
{
12101210
// If no packetloss and done searching, increase the high range by 50%
12111211
if (packetloss==0.0)
@@ -1221,8 +1221,8 @@ void ReliabilityLayer::Update( SOCKET s, PlayerID playerId, int MTUSize, RakNetT
12211221
{
12221222
// If some packetloss, but not a huge amount and done searching, decrease the low range by 10%
12231223
lowBandwidth*=.9;
1224-
if (lowBandwidth < MINIMUM_SEND_BPS)
1225-
lowBandwidth=MINIMUM_SEND_BPS;
1224+
if (lowBandwidth < minimumSendBPS)
1225+
lowBandwidth=minimumSendBPS;
12261226
}
12271227
delta = (highBandwidth-lowBandwidth)/2;
12281228
}

Source/SocketLayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ SOCKET SocketLayer::CreateBoundSocket( unsigned short port, bool blockingSocket,
200200
setsockopt(listenSocket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) );
201201

202202
// This doesn't make much difference: 10% maybe
203-
sock_opt=1024*16;
203+
sock_opt=1024*128;
204204
setsockopt(listenSocket, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) );
205205

206206
#if defined(_WIN32) && !defined(_COMPATIBILITY_1) && defined(_DEBUG)

Source/rakserver.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Packet* RakServer::Receive( void )
160160

161161
void RakServer::Kick(const PlayerID playerId)
162162
{
163-
RakPeer::NotifyAndFlagForDisconnect(playerId, false, 0);
163+
RakPeer::NotifyAndFlagForDisconnect(playerId, false, 2);
164164
}
165165

166166
void RakServer::DeallocatePacket( Packet *packet )
@@ -460,6 +460,11 @@ RakPeer::RemoteSystemStruct* RakServer::GetRemoteSystemFromPlayerID(const Player
460460
}
461461

462462
#ifndef RAKNET_BUILD_FOR_CLIENT
463+
void RakServer::ReserveSlots(unsigned short count)
464+
{
465+
RakPeer::ReserveSlots(count);
466+
}
467+
463468
SAMPRakNet::RemoteSystemData RakServer::GetSAMPDataFromPlayerID(const PlayerID playerId)
464469
{
465470
RemoteSystemStruct* remoteSystem = RakPeer::GetRemoteSystemFromPlayerID(playerId, false, false);

0 commit comments

Comments
 (0)