1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-16 15:16:33 +09:00

Robustified and optimized GetBestChunkSize()

This commit is contained in:
lidgren
2012-10-24 13:06:46 +00:00
parent 1801432a28
commit e1a66ae0cd
2 changed files with 5 additions and 4 deletions

View File

@@ -152,8 +152,9 @@ namespace Lidgren.Network
internal static int GetBestChunkSize(int group, int totalBytes, int mtu) internal static int GetBestChunkSize(int group, int totalBytes, int mtu)
{ {
int tryNumChunks = (totalBytes / (mtu - 8)) + 1; int tryChunkSize = mtu - NetConstants.HeaderByteSize - 4; // naive approximation
int tryChunkSize = (totalBytes / tryNumChunks) + 1; // +1 since we immediately decrement it in the loop int est = GetFragmentationHeaderSize(group, totalBytes, tryChunkSize, totalBytes / tryChunkSize);
tryChunkSize = mtu - NetConstants.HeaderByteSize - est; // slightly less naive approximation
int headerSize = 0; int headerSize = 0;
do do
@@ -164,9 +165,9 @@ namespace Lidgren.Network
if (numChunks * tryChunkSize < totalBytes) if (numChunks * tryChunkSize < totalBytes)
numChunks++; numChunks++;
headerSize = GetFragmentationHeaderSize(group, totalBytes, tryChunkSize, numChunks); headerSize = GetFragmentationHeaderSize(group, totalBytes, tryChunkSize, numChunks); // 4+ bytes
} while (tryChunkSize + headerSize + 5 + 1 >= mtu); } while (tryChunkSize + headerSize + NetConstants.HeaderByteSize + 1 >= mtu);
return tryChunkSize; return tryChunkSize;
} }