address review comments

pull/18865/head
Jan Tattermusch 6 years ago
parent 76e3489216
commit 094c47e7a2
  1. 2
      src/csharp/Grpc.Core/Internal/AsyncCallBase.cs
  2. 25
      src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs
  3. 16
      src/csharp/Grpc.Core/Internal/ReusableSliceBuffer.cs

@ -334,7 +334,7 @@ namespace Grpc.Core.Internal
/// </summary> /// </summary>
protected void HandleReadFinished(bool success, IBufferReader receivedMessageReader) protected void HandleReadFinished(bool success, IBufferReader receivedMessageReader)
{ {
// if success == false, received message will be null. It that case we will // if success == false, the message reader will report null payload. It that case we will
// treat this completion as the last read an rely on C core to handle the failed // treat this completion as the last read an rely on C core to handle the failed
// read (e.g. deliver approriate statusCode on the clientside). // read (e.g. deliver approriate statusCode on the clientside).

@ -113,20 +113,6 @@ namespace Grpc.Core.Internal
return data; return data;
} }
public bool GetReceivedMessageNextSlicePeek(out Slice slice)
{
UIntPtr sliceLen;
IntPtr sliceDataPtr;
if (0 == Native.grpcsharp_batch_context_recv_message_next_slice_peek(this, out sliceLen, out sliceDataPtr))
{
slice = default(Slice);
return false;
}
slice = new Slice(sliceDataPtr, (int) sliceLen);
return true;
}
public IBufferReader GetReceivedMessageReader() public IBufferReader GetReceivedMessageReader()
{ {
return this; return this;
@ -190,7 +176,16 @@ namespace Grpc.Core.Internal
bool IBufferReader.TryGetNextSlice(out Slice slice) bool IBufferReader.TryGetNextSlice(out Slice slice)
{ {
return GetReceivedMessageNextSlicePeek(out slice); UIntPtr sliceLen;
IntPtr sliceDataPtr;
if (0 == Native.grpcsharp_batch_context_recv_message_next_slice_peek(this, out sliceLen, out sliceDataPtr))
{
slice = default(Slice);
return false;
}
slice = new Slice(sliceDataPtr, (int) sliceLen);
return true;
} }
struct CompletionCallbackData struct CompletionCallbackData

@ -31,36 +31,35 @@ namespace Grpc.Core.Internal
public const int MaxCachedSegments = 1024; // ~4MB payload for 4K slices public const int MaxCachedSegments = 1024; // ~4MB payload for 4K slices
readonly SliceSegment[] cachedSegments = new SliceSegment[MaxCachedSegments]; readonly SliceSegment[] cachedSegments = new SliceSegment[MaxCachedSegments];
int populatedSegmentCount = 0; int populatedSegmentCount;
public ReadOnlySequence<byte> PopulateFrom(IBufferReader bufferReader) public ReadOnlySequence<byte> PopulateFrom(IBufferReader bufferReader)
{ {
populatedSegmentCount = 0;
long offset = 0; long offset = 0;
int index = 0;
SliceSegment prevSegment = null; SliceSegment prevSegment = null;
while (bufferReader.TryGetNextSlice(out Slice slice)) while (bufferReader.TryGetNextSlice(out Slice slice))
{ {
// Initialize cached segment if still null or just allocate a new segment if we already reached MaxCachedSegments // Initialize cached segment if still null or just allocate a new segment if we already reached MaxCachedSegments
var current = index < cachedSegments.Length ? cachedSegments[index] : new SliceSegment(); var current = populatedSegmentCount < cachedSegments.Length ? cachedSegments[populatedSegmentCount] : new SliceSegment();
if (current == null) if (current == null)
{ {
current = cachedSegments[index] = new SliceSegment(); current = cachedSegments[populatedSegmentCount] = new SliceSegment();
} }
current.Reset(slice, offset); current.Reset(slice, offset);
prevSegment?.SetNext(current); prevSegment?.SetNext(current);
index ++; populatedSegmentCount ++;
offset += slice.Length; offset += slice.Length;
prevSegment = current; prevSegment = current;
} }
populatedSegmentCount = index;
// Not necessary for ending the ReadOnlySequence, but for making sure we // Not necessary for ending the ReadOnlySequence, but for making sure we
// don't keep more than MaxCachedSegments alive. // don't keep more than MaxCachedSegments alive.
prevSegment?.SetNext(null); prevSegment?.SetNext(null);
if (index == 0) if (populatedSegmentCount == 0)
{ {
return ReadOnlySequence<byte>.Empty; return ReadOnlySequence<byte>.Empty;
} }
@ -80,8 +79,9 @@ namespace Grpc.Core.Internal
while (segment != null) while (segment != null)
{ {
segment.Reset(new Slice(IntPtr.Zero, 0), 0); segment.Reset(new Slice(IntPtr.Zero, 0), 0);
var nextSegment = (SliceSegment) segment.Next;
segment.SetNext(null); segment.SetNext(null);
segment = (SliceSegment) segment.Next; segment = nextSegment;
} }
populatedSegmentCount = 0; populatedSegmentCount = 0;
} }

Loading…
Cancel
Save