Skip to content

Commit

Permalink
ThrowHelper & LoggingExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Seabiscuit committed Aug 14, 2018
1 parent 65a61c0 commit 50ae0dc
Show file tree
Hide file tree
Showing 187 changed files with 5,027 additions and 1,377 deletions.
72 changes: 36 additions & 36 deletions DotNetty.sln
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpServer", "examples\Http
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetty.Transport.Libuv.Tests", "test\DotNetty.Transport.Libuv.Tests\DotNetty.Transport.Libuv.Tests.csproj", "{5938CE7C-61CD-4C70-82B8-B236AC881A14}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSockets.Client", "examples\WebSockets.Client\WebSockets.Client.csproj", "{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSockets.Server", "examples\WebSockets.Server\WebSockets.Server.csproj", "{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSockets.Server", "examples\WebSockets.Server\WebSockets.Server.csproj", "{AA8F9D2A-1055-4018-84A3-D623172787CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSockets.Client", "examples\WebSockets.Client\WebSockets.Client.csproj", "{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Expand Down Expand Up @@ -710,38 +710,38 @@ Global
{5938CE7C-61CD-4C70-82B8-B236AC881A14}.Release|x64.Build.0 = Release|Any CPU
{5938CE7C-61CD-4C70-82B8-B236AC881A14}.Release|x86.ActiveCfg = Release|Any CPU
{5938CE7C-61CD-4C70-82B8-B236AC881A14}.Release|x86.Build.0 = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|ARM.ActiveCfg = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|ARM.Build.0 = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|x64.ActiveCfg = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|x64.Build.0 = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|x86.ActiveCfg = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Debug|x86.Build.0 = Debug|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|Any CPU.Build.0 = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|ARM.ActiveCfg = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|ARM.Build.0 = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|x64.ActiveCfg = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|x64.Build.0 = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|x86.ActiveCfg = Release|Any CPU
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649}.Release|x86.Build.0 = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|ARM.ActiveCfg = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|ARM.Build.0 = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|x64.ActiveCfg = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|x64.Build.0 = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|x86.ActiveCfg = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Debug|x86.Build.0 = Debug|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|Any CPU.Build.0 = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|ARM.ActiveCfg = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|ARM.Build.0 = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|x64.ActiveCfg = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|x64.Build.0 = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|x86.ActiveCfg = Release|Any CPU
{AA8F9D2A-1055-4018-84A3-D623172787CD}.Release|x86.Build.0 = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|ARM.ActiveCfg = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|ARM.Build.0 = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|x64.ActiveCfg = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|x64.Build.0 = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|x86.ActiveCfg = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Debug|x86.Build.0 = Debug|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|Any CPU.Build.0 = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|ARM.ActiveCfg = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|ARM.Build.0 = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|x64.ActiveCfg = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|x64.Build.0 = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|x86.ActiveCfg = Release|Any CPU
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385}.Release|x86.Build.0 = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|ARM.ActiveCfg = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|ARM.Build.0 = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|x64.ActiveCfg = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|x64.Build.0 = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|x86.ActiveCfg = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Debug|x86.Build.0 = Debug|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|Any CPU.Build.0 = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|ARM.ActiveCfg = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|ARM.Build.0 = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|x64.ActiveCfg = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|x64.Build.0 = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|x86.ActiveCfg = Release|Any CPU
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -786,8 +786,8 @@ Global
{26A8779A-44B1-46D4-A43E-FA2616EBFD02} = {01F3CC7E-F996-411E-AFD6-72673A826549}
{BDDF75B4-4BAB-49DE-97A3-CA191C523636} = {3511E2D1-F581-420E-8DC4-8F0386947621}
{5938CE7C-61CD-4C70-82B8-B236AC881A14} = {01F3CC7E-F996-411E-AFD6-72673A826549}
{91F3EB7C-FB5C-4ADB-A3F2-D95A3165E649} = {3511E2D1-F581-420E-8DC4-8F0386947621}
{AA8F9D2A-1055-4018-84A3-D623172787CD} = {3511E2D1-F581-420E-8DC4-8F0386947621}
{F3AA2B9E-0AA7-4F13-B5F5-5BFF34FF8385} = {3511E2D1-F581-420E-8DC4-8F0386947621}
{DA148037-5219-49C2-B76F-9B0F2BFC5FA9} = {3511E2D1-F581-420E-8DC4-8F0386947621}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A659CEFB-DDB3-49BE-AEDD-FF2F1B3297DB}
Expand Down
30 changes: 16 additions & 14 deletions src/DotNetty.Buffers/AbstractUnpooledSlicedByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ protected AbstractUnpooledSlicedByteBuffer(IByteBuffer buffer, int index, int le
{
CheckSliceOutOfBounds(index, length, buffer);

if (buffer is AbstractUnpooledSlicedByteBuffer byteBuffer)
switch (buffer)
{
this.buffer = byteBuffer.buffer;
this.adjustment = byteBuffer.adjustment + index;
}
else if (buffer is UnpooledDuplicatedByteBuffer)
{
this.buffer = buffer.Unwrap();
this.adjustment = index;
}
else
{
this.buffer = buffer;
this.adjustment = index;
case AbstractUnpooledSlicedByteBuffer byteBuffer:
this.buffer = byteBuffer.buffer;
this.adjustment = byteBuffer.adjustment + index;
break;

case UnpooledDuplicatedByteBuffer _:
this.buffer = buffer.Unwrap();
this.adjustment = index;
break;

default:
this.buffer = buffer;
this.adjustment = index;
break;
}

this.SetWriterIndex0(length);
Expand Down Expand Up @@ -329,7 +331,7 @@ internal static void CheckSliceOutOfBounds(int index, int length, IByteBuffer bu
{
if (MathUtil.IsOutOfBounds(index, length, buffer.Capacity))
{
throw new IndexOutOfRangeException($"{buffer}.Slice({index}, {length})");
ThrowHelper.ThrowIndexOutOfRangeException_CheckSliceOutOfBounds(index, length, buffer);
}
}
}
Expand Down
138 changes: 69 additions & 69 deletions src/DotNetty.Buffers/ByteBufferUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,25 +317,25 @@ public static IByteBuffer WriteUtf8(IByteBufferAllocator alloc, ICharSequence se

public static int ReserveAndWriteUtf8(IByteBuffer buf, ICharSequence seq, int reserveBytes)
{
for (;;)
while(true)
{
if (buf is AbstractByteBuffer byteBuf)
switch (buf)
{
byteBuf.EnsureWritable0(reserveBytes);
int written = WriteUtf8(byteBuf, byteBuf.WriterIndex, seq, seq.Count);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;
}
else if (buf is WrappedByteBuffer)
{
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(seq.ToString());
buf.WriteBytes(bytes);
return bytes.Length;
case AbstractByteBuffer byteBuf:
byteBuf.EnsureWritable0(reserveBytes);
int written = WriteUtf8(byteBuf, byteBuf.WriterIndex, seq, seq.Count);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;

case WrappedByteBuffer _:
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
break;

default:
byte[] bytes = Encoding.UTF8.GetBytes(seq.ToString());
buf.WriteBytes(bytes);
return bytes.Length;
}
}
}
Expand Down Expand Up @@ -422,25 +422,25 @@ public static IByteBuffer WriteUtf8(IByteBufferAllocator alloc, string value)
/// <returns> This method returns the actual number of bytes written.</returns>
public static int ReserveAndWriteUtf8(IByteBuffer buf, string value, int reserveBytes)
{
for (;;)
while(true)
{
if (buf is AbstractByteBuffer byteBuf)
switch (buf)
{
byteBuf.EnsureWritable0(reserveBytes);
int written = WriteUtf8(byteBuf, byteBuf.WriterIndex, value, value.Length);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;
}
else if (buf is WrappedByteBuffer)
{
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(value);
buf.WriteBytes(bytes);
return bytes.Length;
case AbstractByteBuffer byteBuf:
byteBuf.EnsureWritable0(reserveBytes);
int written = WriteUtf8(byteBuf, byteBuf.WriterIndex, value, value.Length);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;

case WrappedByteBuffer _:
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
break;

default:
byte[] bytes = Encoding.UTF8.GetBytes(value);
buf.WriteBytes(bytes);
return bytes.Length;
}
}
}
Expand Down Expand Up @@ -595,25 +595,25 @@ public static int WriteAscii(IByteBuffer buf, ICharSequence seq)
}
else
{
for (;;)
while(true)
{
if (buf is AbstractByteBuffer byteBuf)
switch (buf)
{
byteBuf.EnsureWritable0(len);
int written = WriteAscii(byteBuf, byteBuf.WriterIndex, seq, len);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;
}
else if (buf is WrappedByteBuffer)
{
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
}
else
{
byte[] bytes = Encoding.ASCII.GetBytes(seq.ToString());
buf.WriteBytes(bytes);
return bytes.Length;
case AbstractByteBuffer byteBuf:
byteBuf.EnsureWritable0(len);
int written = WriteAscii(byteBuf, byteBuf.WriterIndex, seq, len);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;

case WrappedByteBuffer _:
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
break;

default:
byte[] bytes = Encoding.ASCII.GetBytes(seq.ToString());
buf.WriteBytes(bytes);
return bytes.Length;
}
}
}
Expand Down Expand Up @@ -644,25 +644,25 @@ public static int WriteAscii(IByteBuffer buf, string value)
{
// ASCII uses 1 byte per char
int len = value.Length;
for (;;)
while(true)
{
if (buf is AbstractByteBuffer byteBuf)
{
byteBuf.EnsureWritable0(len);
int written = WriteAscii(byteBuf, byteBuf.WriterIndex, value, len);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;
}
else if (buf is WrappedByteBuffer)
switch (buf)
{
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
}
else
{
byte[] bytes = Encoding.ASCII.GetBytes(value);
buf.WriteBytes(bytes);
return bytes.Length;
case AbstractByteBuffer byteBuf:
byteBuf.EnsureWritable0(len);
int written = WriteAscii(byteBuf, byteBuf.WriterIndex, value, len);
byteBuf.SetWriterIndex(byteBuf.WriterIndex + written);
return written;

case WrappedByteBuffer _:
// Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
buf = buf.Unwrap();
break;

default:
byte[] bytes = Encoding.ASCII.GetBytes(value);
buf.WriteBytes(bytes);
return bytes.Length;
}
}
}
Expand Down Expand Up @@ -1037,7 +1037,7 @@ public static bool IsText(IByteBuffer buf, int index, int length, Encoding encod
int maxIndex = buf.ReaderIndex + buf.ReadableBytes;
if (index < 0 || length < 0 || index > maxIndex - length)
{
throw new IndexOutOfRangeException($"index: {index}length: {length}");
ThrowHelper.ThrowIndexOutOfRangeException_IsText(index, length);
}
if (ReferenceEquals(Encoding.UTF8, encoding))
{
Expand Down
4 changes: 2 additions & 2 deletions src/DotNetty.Buffers/CompositeByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ public virtual int ToComponentIndex(int offset)
}
}

throw new Exception("should not reach here");
return ThrowHelper.ThrowGetException_ShouldNotReachHere<int>();
}

public virtual int ToByteIndex(int cIndex)
Expand Down Expand Up @@ -1304,7 +1304,7 @@ ComponentEntry FindComponent(int offset)
}
}

throw new Exception("should not reach here");
return ThrowHelper.ThrowGetException_ShouldNotReachHere<ComponentEntry>();
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/DotNetty.Buffers/EmptyByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ IByteBuffer CheckIndex(int index)
{
if (index != 0)
{
throw new IndexOutOfRangeException();
ThrowHelper.ThrowIndexOutOfRangeException();
}
return this;
}
Expand All @@ -515,11 +515,11 @@ IByteBuffer CheckIndex(int index, int length)
{
if (length < 0)
{
throw new ArgumentException("length: " + length);
ThrowHelper.ThrowArgumentException_CheckIndex(length);
}
if (index != 0 || length != 0)
{
throw new IndexOutOfRangeException();
ThrowHelper.ThrowIndexOutOfRangeException();
}

return this;
Expand All @@ -530,11 +530,11 @@ IByteBuffer CheckLength(int length)
{
if (length < 0)
{
throw new ArgumentException("length: " + length + " (expected: >= 0)");
ThrowHelper.ThrowArgumentException_CheckLength(length);
}
if (length != 0)
{
throw new IndexOutOfRangeException();
ThrowHelper.ThrowIndexOutOfRangeException();
}
return this;
}
Expand Down
19 changes: 19 additions & 0 deletions src/DotNetty.Buffers/Internal/LoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Runtime.CompilerServices;
using DotNetty.Common.Concurrency;
using DotNetty.Common.Internal.Logging;

namespace DotNetty.Buffers
{
internal static class BuffersLoggingExtensions
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void FreedThreadLocalBufferFromThread(this IInternalLogger logger, int numFreed, XThread deathWatchThread)
{
logger.Debug("Freed {} thread-local buffer(s) from thread: {}", numFreed, deathWatchThread.Name);
}
}
}
Loading

0 comments on commit 50ae0dc

Please sign in to comment.