Skip to content

Commit

Permalink
increase header size of prefixlengthprotocol to 4 bytes when maxMessa…
Browse files Browse the repository at this point in the history
…geSize < ushort.Max
  • Loading branch information
job79 committed Feb 18, 2021
1 parent 0c7fba6 commit 25470c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
22 changes: 9 additions & 13 deletions EasyTcp4/EasyTcp4.Encryption/Ssl/PrefixLengthSslProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace EasyTcp4.Encryption.Ssl
{
/// <summary>
/// Protocol that determines the length of a message based on a small header
/// Header is an ushort or int as byte[] with the length of the message.
/// Header is an int as byte[] with the length of the message.
/// </summary>
public class PrefixLengthSslProtocol : SslProtocol
{
Expand All @@ -28,8 +28,7 @@ public class PrefixLengthSslProtocol : SslProtocol
public override int BufferCount
{
get => ReceivedHeader ?
Math.Min(BufferSize - BufferOffset, 10240) : // Do not receive more than 10240 bytes at once
MaxMessageLength > ushort.MaxValue ? 4 : 2;
Math.Min(BufferSize - BufferOffset, 10240) : 4; // Do not receive more than 10240 bytes at once
protected set { }
}

Expand All @@ -40,8 +39,6 @@ protected set { }

/// <summary>
/// Maximimum amount of bytes for one message
/// 2 bytes will be used for the header when MaxMessageLength is smaller then 65535,
/// for bigger messages 4 bytes are used for the header
/// </summary>
protected readonly int MaxMessageLength;

Expand All @@ -51,7 +48,7 @@ protected set { }
public PrefixLengthSslProtocol(X509Certificate certificate, int maxMessageLength = ushort.MaxValue) : base(certificate)
{
MaxMessageLength = maxMessageLength;
BufferSize = maxMessageLength > ushort.MaxValue ? 4 : 2;
BufferSize = 4;
BufferCount = BufferSize;
}

Expand All @@ -63,7 +60,7 @@ public PrefixLengthSslProtocol(string serverName, bool acceptInvalidCertificates
int maxMessageLength = ushort.MaxValue) : base(serverName, acceptInvalidCertificates)
{
MaxMessageLength = maxMessageLength;
BufferSize = maxMessageLength > ushort.MaxValue ? 4 : 2;
BufferSize = 4;
BufferCount = BufferSize;
}

Expand All @@ -84,10 +81,9 @@ public override void SendMessage(EasyTcpClient client, params byte[][] messageDa
throw new ArgumentException("Could not send message: message is too big, increase maxMessageLength or send message with SendArray/SendStream");

// Add header to message
int offset = MaxMessageLength > ushort.MaxValue ? 4 : 2;
int offset = 4;
byte[] message = new byte[offset + messageLength];
if (offset == 4) Buffer.BlockCopy(BitConverter.GetBytes((int)messageLength), 0, message, 0, offset);
else Buffer.BlockCopy(BitConverter.GetBytes((ushort)messageLength), 0, message, 0, offset);
Buffer.BlockCopy(BitConverter.GetBytes((int)messageLength), 0, message, 0, offset);

// Add data to message
foreach (var d in messageData)
Expand All @@ -100,7 +96,7 @@ public override void SendMessage(EasyTcpClient client, params byte[][] messageDa

// Send data
// Remove prefix in OnDataSend with an offset
client.FireOnDataSend(message, MaxMessageLength > ushort.MaxValue ? 4 : 2);
client.FireOnDataSend(message, 4);
SslStream.Write(message, 0, message.Length);
}

Expand All @@ -115,15 +111,15 @@ public override async Task OnDataReceive(byte[] data, int receivedBytes, EasyTcp
if (!ReceivedHeader)
{
ReceivedHeader = true;
BufferSize = MaxMessageLength > ushort.MaxValue ? BitConverter.ToInt32(data, 0) : BitConverter.ToUInt16(data, 0);
BufferSize = BitConverter.ToInt32(data, 0);
if (BufferSize == 0) client.Dispose();
}
else
{
if (BufferOffset + receivedBytes == BufferSize)
{
ReceivedHeader = false;
BufferSize = MaxMessageLength > ushort.MaxValue ? 4 : 2;
BufferSize = 4;
BufferOffset = 0;
await client.DataReceiveHandler(new Message(data, client));
}
Expand Down
20 changes: 8 additions & 12 deletions EasyTcp4/EasyTcp4/Protocols/Tcp/PrefixLengthProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace EasyTcp4.Protocols.Tcp
{
/// <summary>
/// Protocol that determines the length of a message based on a small header
/// Header is an ushort or int as byte[] with the length of the message.
/// Header is an int as byte[] with the length of the message.
/// </summary>
public class PrefixLengthProtocol : TcpProtocol
{
Expand All @@ -26,8 +26,7 @@ public class PrefixLengthProtocol : TcpProtocol
public override int BufferCount
{
get => ReceivedHeader ?
Math.Min(BufferSize - BufferOffset, 10240) : // Do not receive more than 10240 bytes at once
MaxMessageLength > ushort.MaxValue ? 4 : 2;
Math.Min(BufferSize - BufferOffset, 10240) : 4; // Do not receive more than 10240 bytes at once
protected set { }
}

Expand All @@ -38,8 +37,6 @@ protected set { }

/// <summary>
/// Maximimum amount of bytes for one message
/// 2 bytes will be used for the header when MaxMessageLength is smaller then 65535,
/// for bigger messages 4 bytes are used for the header
/// </summary>
protected readonly int MaxMessageLength;

Expand All @@ -48,7 +45,7 @@ protected set { }
public PrefixLengthProtocol(int maxMessageLength = ushort.MaxValue)
{
MaxMessageLength = maxMessageLength;
BufferSize = maxMessageLength > ushort.MaxValue ? 4 : 2;
BufferSize = 4;
BufferCount = BufferSize;
}

Expand All @@ -68,10 +65,9 @@ public override void SendMessage(EasyTcpClient client, params byte[][] messageDa
throw new ArgumentException("Could not send message: message is too big, increase maxMessageLength or send message with SendArray/SendStream");

// Add header to message
int offset = MaxMessageLength > ushort.MaxValue ? 4 : 2;
int offset = 4;
byte[] message = new byte[offset + messageLength];
if (offset == 4) Buffer.BlockCopy(BitConverter.GetBytes((int)messageLength), 0, message, 0, offset);
else Buffer.BlockCopy(BitConverter.GetBytes((ushort)messageLength), 0, message, 0, offset);
Buffer.BlockCopy(BitConverter.GetBytes((int)messageLength), 0, message, 0, offset);

// Add data to message
foreach (var d in messageData)
Expand All @@ -83,7 +79,7 @@ public override void SendMessage(EasyTcpClient client, params byte[][] messageDa

// Send data
// Remove prefix in OnDataSend with an offset
client.FireOnDataSend(message, MaxMessageLength > ushort.MaxValue ? 4 : 2);
client.FireOnDataSend(message, 4);
client.BaseSocket.Send(message);
}

Expand All @@ -98,15 +94,15 @@ public override async Task OnDataReceive(byte[] data, int receivedBytes, EasyTcp
if (!ReceivedHeader)
{
ReceivedHeader = true;
BufferSize = MaxMessageLength > ushort.MaxValue ? BitConverter.ToInt32(data, 0) : BitConverter.ToUInt16(data, 0);
BufferSize = BitConverter.ToInt32(data, 0);
if (BufferSize == 0) client.Dispose();
}
else
{
if (BufferOffset + receivedBytes == BufferSize)
{
ReceivedHeader = false;
BufferSize = MaxMessageLength > ushort.MaxValue ? 4 : 2;
BufferSize = 4;
BufferOffset = 0;
await client.DataReceiveHandler(new Message(data, client));
}
Expand Down

0 comments on commit 25470c3

Please sign in to comment.