Skip to content

Commit

Permalink
Merge pull request #99 from asimarslan/feature/master/buffer-resize-fix
Browse files Browse the repository at this point in the history
client message internal buffer created with correct size for decode
  • Loading branch information
asimarslan committed Feb 14, 2017
2 parents 92173d9 + 68c39b8 commit 95cb6d7
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions Hazelcast.Net/Hazelcast.Client.Protocol/ClientMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.IO;
using System.Text;
using Hazelcast.Client.Protocol.Util;
using Hazelcast.IO;
Expand Down Expand Up @@ -148,14 +149,24 @@ public virtual bool IsRetryable()

public virtual bool ReadFrom(ByteBuffer source)
{
if (Index() == 0)
var frameLength = 0;
if (Buffer == null)
{
InitFrameSize(source);
}
while (Index() >= Bits.IntSizeInBytes && source.HasRemaining() && !IsComplete())
{
Accumulate(source, GetFrameLength() - Index());
//init internal buffer
var remaining = source.Remaining();
if (remaining < Bits.IntSizeInBytes) {
//we don't have even the frame length ready
return false;
}
frameLength = Bits.ReadIntL(source.Array(), source.Position);
if (frameLength < HeaderSize)
{
throw new InvalidDataException("Client message frame length cannot be smaller than header size.");
}
Wrap(new SafeBuffer(new byte[frameLength]), 0);
}
frameLength = frameLength > 0 ? frameLength : GetFrameLength();
Accumulate(source, frameLength - Index());
return IsComplete();
}

Expand Down Expand Up @@ -198,9 +209,7 @@ public virtual bool IsUrgent()

public static ClientMessage Create()
{
var clientMessage = new ClientMessage();
clientMessage.Wrap(new SafeBuffer(new byte[InitialBufferSize]), 0);
return clientMessage;
return new ClientMessage();
}

public static ClientMessage CreateForDecode(IClientProtocolBuffer buffer, int offset)
Expand Down Expand Up @@ -347,8 +356,6 @@ private int Accumulate(ByteBuffer byteBuffer, int length)
var readLength = remaining < length ? remaining : length;
if (readLength > 0)
{
var requiredCapacity = Index() + readLength;
EnsureCapacity(requiredCapacity);
Buffer.PutBytes(Index(), byteBuffer.Array(), byteBuffer.Position, readLength);
byteBuffer.Position = byteBuffer.Position + readLength;
Index(Index() + readLength);
Expand All @@ -357,17 +364,6 @@ private int Accumulate(ByteBuffer byteBuffer, int length)
return 0;
}

private void EnsureCapacity(int requiredCapacity)
{
var capacity = Buffer.Capacity() > 0 ? Buffer.Capacity() : 1;
if (requiredCapacity > capacity)
{
var newCapacity = QuickMath.NextPowerOfTwo(requiredCapacity);
var newBuffer = new byte[newCapacity];
Array.Copy(Buffer.ByteArray(), 0, newBuffer, 0, capacity);
Buffer.Wrap(newBuffer);
}
}

private void EnsureHeaderSize(int offset, int length)
{
Expand All @@ -377,14 +373,5 @@ private void EnsureHeaderSize(int offset, int length)
" bytes! length: " + length + ", offset: " + offset);
}
}

private int InitFrameSize(ByteBuffer byteBuffer)
{
if (byteBuffer.Remaining() < Bits.IntSizeInBytes)
{
return 0;
}
return Accumulate(byteBuffer, Bits.IntSizeInBytes);
}
}
}

0 comments on commit 95cb6d7

Please sign in to comment.