Skip to content

Commit

Permalink
Port NetUV.Core Part-B
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteant committed Sep 11, 2020
1 parent 1cc0bc9 commit d5b13f2
Show file tree
Hide file tree
Showing 50 changed files with 1,109 additions and 582 deletions.
4 changes: 2 additions & 2 deletions src/DotNetty.NetUV/Buffers/ReadableBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public static ReadableBuffer Composite(IEnumerable<ReadableBuffer> buffers)

//public string ReadString(Encoding encoding, byte[] separator)
//{
// Contract.Requires(encoding != null);
// Contract.Requires(separator != null && separator.Length > 0);
// Contract.Requires(encoding is object);
// Contract.Requires(separator is object && separator.Length > 0);

// int readableBytes = this.buffer.ReadableBytes;
// if (readableBytes == 0)
Expand Down
56 changes: 30 additions & 26 deletions src/DotNetty.NetUV/Buffers/ReceiveBufferSizeEstimate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,80 +82,84 @@ private static int GetSizeTableIndex(int size)
}
}

private readonly int minIndex;
private readonly int maxIndex;
private int index;
private bool decreaseNow;
private int receiveBufferSize;
private readonly int _minIndex;
private readonly int _maxIndex;
private int _index;
private bool _decreaseNow;
private int _receiveBufferSize;

public ReceiveBufferSizeEstimate(int minimum = DefaultMinimum, int initial = DefaultInitial, int maximum = DefaultMaximum)
{
if (minimum <= 0) { ThrowHelper.ThrowArgumentException_Positive(minimum, ExceptionArgument.minimum); }
if ((uint)(minimum - 1) > SharedConstants.TooBigOrNegative) { ThrowHelper.ThrowArgumentException_Positive(minimum, ExceptionArgument.minimum); }
if (initial < minimum) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.initial); }
if (initial > maximum) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.maximum); }

int min = GetSizeTableIndex(minimum);
if (SizeTable[min] < minimum)
{
this.minIndex = min + 1;
_minIndex = min + 1;
}
else
{
this.minIndex = min;
_minIndex = min;
}

int max = GetSizeTableIndex(maximum);
if (SizeTable[max] > maximum)
{
this.maxIndex = max - 1;
_maxIndex = max - 1;
}
else
{
this.maxIndex = max;
_maxIndex = max;
}

this.index = GetSizeTableIndex(initial);
this.receiveBufferSize = SizeTable[this.index];
_index = GetSizeTableIndex(initial);
_receiveBufferSize = SizeTable[_index];
}

internal IByteBuffer Allocate(PooledByteBufferAllocator allocator)
{
Debug.Assert(allocator != null);
Debug.Assert(allocator is object);

#if DEBUG
if (Log.DebugEnabled)
{
Log.Debug("{0} allocate, estimated size = {1}", nameof(ReceiveBufferSizeEstimate), this.receiveBufferSize);
Log.Debug("{} allocate, estimated size = {}", nameof(ReceiveBufferSizeEstimate), _receiveBufferSize);
}
#endif

return allocator.Buffer(this.receiveBufferSize);
return allocator.Buffer(_receiveBufferSize);
}

internal void Record(int actualReadBytes)
{
if (actualReadBytes <= SizeTable[Math.Max(0, this.index - IndexDecrement - 1)])
if (actualReadBytes <= SizeTable[Math.Max(0, _index - IndexDecrement - 1)])
{
if (this.decreaseNow)
if (_decreaseNow)
{
this.index = Math.Max(this.index - IndexDecrement, this.minIndex);
this.receiveBufferSize = SizeTable[this.index];
this.decreaseNow = false;
_index = Math.Max(_index - IndexDecrement, _minIndex);
_receiveBufferSize = SizeTable[_index];
_decreaseNow = false;
}
else
{
this.decreaseNow = true;
_decreaseNow = true;
}
}
else if (actualReadBytes >= this.receiveBufferSize)
else if (actualReadBytes >= _receiveBufferSize)
{
this.index = Math.Min(this.index + IndexIncrement, this.maxIndex);
this.receiveBufferSize = SizeTable[this.index];
this.decreaseNow = false;
_index = Math.Min(_index + IndexIncrement, _maxIndex);
_receiveBufferSize = SizeTable[_index];
_decreaseNow = false;
}

#if DEBUG
if (Log.DebugEnabled)
{
Log.Debug("{0} record actual size = {1}, next size = {2}", nameof(ReceiveBufferSizeEstimate), actualReadBytes, this.receiveBufferSize);
Log.Debug("{} record actual size = {}, next size = {}", nameof(ReceiveBufferSizeEstimate), actualReadBytes, _receiveBufferSize);
}
#endif
}
}
}
2 changes: 1 addition & 1 deletion src/DotNetty.NetUV/Channels/StreamConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Consume(T stream, IStreamReadCompletion readCompletion)
{
try
{
if (readCompletion.Error != null)
if (readCompletion.Error is object)
{
_onError(stream, readCompletion.Error);
}
Expand Down
10 changes: 6 additions & 4 deletions src/DotNetty.NetUV/Handles/FSEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ public string GetPath()

private void OnFSEventCallback(string fileName, int events, int status)
{
#if DEBUG
if (Log.TraceEnabled)
{
Log.Trace("{0} {1} callback", HandleType, InternalHandle);
Log.Trace("{} {} callback", HandleType, InternalHandle);
}
#endif
try
{
OperationException error = null;
if (status < 0)
if ((uint)status > SharedConstants.TooBigOrNegative)
{
error = NativeMethods.CreateError((uv_err_code)status);
}
Expand All @@ -120,7 +122,7 @@ private void OnFSEventCallback(string fileName, int events, int status)
}
catch (Exception exception)
{
Log.Error($"{HandleType} {InternalHandle} callback error.", exception);
Log.Handle_callback_error(HandleType, InternalHandle, exception);
throw;
}
}
Expand All @@ -138,7 +140,7 @@ private static void OnFSEventCallback(IntPtr handle, string fileName, int events
public void CloseHandle(Action<FSEvent> onClosed = null)
{
Action<ScheduleHandle> handler = null;
if (onClosed != null)
if (onClosed is object)
{
handler = state => onClosed((FSEvent)state);
}
Expand Down
17 changes: 9 additions & 8 deletions src/DotNetty.NetUV/Handles/FSPoll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace DotNetty.NetUV.Handles
{
using System;
using System.Diagnostics.Contracts;
using DotNetty.NetUV.Native;

public readonly struct FSPollStatus
Expand Down Expand Up @@ -45,9 +44,9 @@ internal FSPoll(LoopContext loop)

public FSPoll Start(string path, int interval, Action<FSPoll, FSPollStatus> callback)
{
Contract.Requires(!string.IsNullOrEmpty(path));
Contract.Requires(interval > 0);
Contract.Requires(callback != null);
if (string.IsNullOrEmpty(path)) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.path); }
if ((uint)(interval - 1) > SharedConstants.TooBigOrNegative) { ThrowHelper.ThrowArgumentException_Positive(interval, ExceptionArgument.interval); }
if (callback is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.callback); }

Validate();
_pollCallback = callback;
Expand All @@ -64,16 +63,18 @@ public string GetPath()

private void OnFSPollCallback(int status, ref uv_stat_t prev, ref uv_stat_t curr)
{
#if DEBUG
if (Log.TraceEnabled)
{
Log.Trace("{0} {1} callback", HandleType, InternalHandle);
Log.Trace("{} {} callback", HandleType, InternalHandle);
}
#endif
try
{
FileStatus previous = null;
FileStatus current = null;
OperationException error = null;
if (status < 0)
if ((uint)status > SharedConstants.TooBigOrNegative) // < 0
{
error = NativeMethods.CreateError((uv_err_code)status);
}
Expand All @@ -87,7 +88,7 @@ private void OnFSPollCallback(int status, ref uv_stat_t prev, ref uv_stat_t curr
}
catch (Exception exception)
{
Log.Error($"{HandleType} {InternalHandle} callback error.", exception);
Log.Handle_callback_error(HandleType, InternalHandle, exception);
throw;
}
}
Expand All @@ -105,7 +106,7 @@ private static void OnFSPollCallback(IntPtr handle, int status, ref uv_stat_t pr
public void CloseHandle(Action<FSPoll> onClosed = null)
{
Action<ScheduleHandle> handler = null;
if (onClosed != null)
if (onClosed is object)
{
handler = state => onClosed((FSPoll)state);
}
Expand Down
32 changes: 12 additions & 20 deletions src/DotNetty.NetUV/Handles/HandleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ internal HandleContext(
params object[] args)
{
Debug.Assert(loopHandle != IntPtr.Zero);
Debug.Assert(initializer != null);
Debug.Assert(target != null);
Debug.Assert(initializer is object);
Debug.Assert(target is object);

int size = NativeMethods.GetSize(handleType);
IntPtr handle = NativeMethods.Allocate(size);
Expand All @@ -56,10 +56,7 @@ internal HandleContext(
Handle = handle;
_handleType = handleType;

if (Log.InfoEnabled)
{
Log.Info("{0} {1} allocated.", handleType, handle);
}
if (Log.InfoEnabled) { Log.HandleAllocated(handleType, handle); }
}

internal bool IsActive => IsValid
Expand Down Expand Up @@ -89,16 +86,10 @@ internal bool HasReference()
protected override void CloseHandle()
{
IntPtr handle = Handle;
if (handle == IntPtr.Zero)
{
return;
}
if (handle == IntPtr.Zero) { return; }

NativeMethods.CloseHandle(handle, CloseCallback);
if (Log.InfoEnabled)
{
Log.Info("{0} {1} closed, releasing resources pending.", _handleType, handle);
}
if (Log.InfoEnabled) { Log.HandleClosedReleasingResourcesPending(_handleType, handle); }
}

internal static T GetTarget<T>(IntPtr handle)
Expand All @@ -120,10 +111,7 @@ internal static T GetTarget<T>(IntPtr handle)

private static void OnCloseHandle(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
return;
}
if (handle == IntPtr.Zero) { return; }

ScheduleHandle scheduleHandle = null;

Expand All @@ -138,20 +126,24 @@ private static void OnCloseHandle(IntPtr handle)
nativeHandle.Free();

((uv_handle_t*)handle)->data = IntPtr.Zero;
#if DEBUG
if (Log.TraceEnabled)
{
Log.Trace("{0} {1} GCHandle released.", scheduleHandle?.HandleType, handle);
Log.Trace("{} {} GCHandle released.", scheduleHandle?.HandleType, handle);
}
#endif
}
}

// Release memory
NativeMethods.FreeMemory(handle);
scheduleHandle?.OnHandleClosed();
#if DEBUG
if (Log.TraceEnabled)
{
Log.Info("{0} {1} memory and GCHandle released.", scheduleHandle?.HandleType, handle);
Log.Trace("{} {} memory and GCHandle released.", scheduleHandle?.HandleType, handle);
}
#endif
}
}
}
Loading

0 comments on commit d5b13f2

Please sign in to comment.