Skip to content

Commit c0d9ed2

Browse files
committed
Merge pull request #109 from nayato/lang2
Fixing minor issues and code style throughout
2 parents afb136b + 8114b7f commit c0d9ed2

121 files changed

Lines changed: 1082 additions & 3095 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/Echo.Client/EchoClientHandler.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,15 @@ namespace Echo.Client
1111
public class EchoClientHandler : ChannelHandlerAdapter
1212
{
1313
readonly IByteBuffer initialMessage;
14-
readonly byte[] buffer;
1514

1615
public EchoClientHandler()
1716
{
18-
this.buffer = new byte[EchoClientSettings.Size];
1917
this.initialMessage = Unpooled.Buffer(EchoClientSettings.Size);
2018
byte[] messageBytes = Encoding.UTF8.GetBytes("Hello world");
2119
this.initialMessage.WriteBytes(messageBytes);
2220
}
2321

24-
public override void ChannelActive(IChannelHandlerContext context)
25-
{
26-
context.WriteAndFlushAsync(this.initialMessage);
27-
}
22+
public override void ChannelActive(IChannelHandlerContext context) => context.WriteAndFlushAsync(this.initialMessage);
2823

2924
public override void ChannelRead(IChannelHandlerContext context, object message)
3025
{
@@ -36,14 +31,8 @@ public override void ChannelRead(IChannelHandlerContext context, object message)
3631
context.WriteAsync(message);
3732
}
3833

39-
public override void ChannelReadComplete(IChannelHandlerContext context)
40-
{
41-
context.Flush();
42-
}
34+
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
4335

44-
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
45-
{
46-
context.CloseAsync();
47-
}
36+
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) => context.CloseAsync();
4837
}
4938
}

examples/Echo.Client/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Echo.Client
1818

1919
class Program
2020
{
21-
static async Task RunClient()
21+
static async Task RunClientAsync()
2222
{
2323
var eventListener = new ObservableEventListener();
2424
eventListener.LogToConsole();
@@ -62,9 +62,6 @@ static async Task RunClient()
6262
}
6363
}
6464

65-
static void Main(string[] args)
66-
{
67-
Task.Run(() => RunClient()).Wait();
68-
}
65+
static void Main() => RunClientAsync().Wait();
6966
}
7067
}

examples/Echo.Server/EchoServerHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public override void ChannelRead(IChannelHandlerContext context, object message)
2020
context.WriteAsync(message);
2121
}
2222

23-
public override void ChannelReadComplete(IChannelHandlerContext context)
24-
{
25-
context.Flush();
26-
}
23+
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
2724

2825
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
2926
{

examples/Echo.Server/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Echo.Server
1818

1919
class Program
2020
{
21-
static async Task RunServer()
21+
static async Task RunServerAsync()
2222
{
2323
var eventListener = new ObservableEventListener();
2424
eventListener.LogToConsole();
@@ -61,9 +61,6 @@ static async Task RunServer()
6161
}
6262
}
6363

64-
static void Main(string[] args)
65-
{
66-
Task.Run(() => RunServer()).Wait();
67-
}
64+
static void Main() => RunServerAsync().Wait();
6865
}
6966
}

src/DotNetty.Buffers/AbstractByteBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ int ForEachByteAsc0(int index, int length, ByteProcessor processor)
941941
{
942942
if (processor == null)
943943
{
944-
throw new ArgumentNullException("processor");
944+
throw new ArgumentNullException(nameof(processor));
945945
}
946946

947947
if (length == 0)

src/DotNetty.Buffers/AbstractByteBufferAllocator.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,9 @@ protected AbstractByteBufferAllocator()
4949
this.emptyBuffer = new EmptyByteBuffer(this);
5050
}
5151

52-
public IByteBuffer Buffer()
53-
{
54-
return this.Buffer(DefaultInitialCapacity, int.MaxValue);
55-
}
52+
public IByteBuffer Buffer() => this.Buffer(DefaultInitialCapacity, int.MaxValue);
5653

57-
public IByteBuffer Buffer(int initialCapacity)
58-
{
59-
return this.Buffer(initialCapacity, int.MaxValue);
60-
}
54+
public IByteBuffer Buffer(int initialCapacity) => this.Buffer(initialCapacity, int.MaxValue);
6155

6256
public IByteBuffer Buffer(int initialCapacity, int maxCapacity)
6357
{

src/DotNetty.Buffers/ByteBufferUtil.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ public static int HashCode(IByteBuffer buffer)
222222
/// identical to each other for {@code length} bytes starting at {@code aStartIndex}
223223
/// index for the {@code a} buffer and {@code bStartIndex} index for the {@code b} buffer.
224224
/// A more compact way to express this is:
225-
/// <p>
226-
/// {@code a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]}
225+
/// <p />
226+
/// {@code a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]}
227227
/// </summary>
228228
public static bool Equals(IByteBuffer a, int aStartIndex, IByteBuffer b, int bStartIndex, int length)
229229
{
@@ -404,19 +404,44 @@ public static IByteBuffer ReadBytes(IByteBufferAllocator alloc, IByteBuffer buff
404404
}
405405
}
406406

407-
//todo: port
408-
//static int firstIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value)
409-
//{
410-
// fromIndex = Math.max(fromIndex, 0);
411-
// if (fromIndex >= toIndex || buffer.capacity() == 0)
412-
// {
413-
// return -1;
414-
// }
407+
/// <summary>
408+
/// The default implementation of <see cref="IByteBuffer.IndexOf(int, int, byte)"/>.
409+
/// This method is useful when implementing a new buffer type.
410+
/// </summary>
411+
public static int IndexOf(IByteBuffer buffer, int fromIndex, int toIndex, byte value)
412+
{
413+
if (fromIndex <= toIndex)
414+
{
415+
return FirstIndexOf(buffer, fromIndex, toIndex, value);
416+
}
417+
else
418+
{
419+
return LastIndexOf(buffer, fromIndex, toIndex, value);
420+
}
421+
}
422+
423+
static int FirstIndexOf(IByteBuffer buffer, int fromIndex, int toIndex, byte value)
424+
{
425+
fromIndex = Math.Max(fromIndex, 0);
426+
if (fromIndex >= toIndex || buffer.Capacity == 0)
427+
{
428+
return -1;
429+
}
430+
431+
return buffer.ForEachByte(fromIndex, toIndex - fromIndex, new ByteProcessor.IndexOfProcessor(value));
432+
}
415433

416-
// return buffer.ForEachByte(fromIndex, toIndex - fromIndex, new ByteProcessor.IndexOfProcessor(value));
417-
//}
434+
static int LastIndexOf(IByteBuffer buffer, int fromIndex, int toIndex, byte value)
435+
{
436+
fromIndex = Math.Min(fromIndex, buffer.Capacity);
437+
if (fromIndex < 0 || buffer.Capacity == 0)
438+
{
439+
return -1;
440+
}
441+
442+
return buffer.ForEachByteDesc(toIndex, fromIndex - toIndex, new ByteProcessor.IndexOfProcessor(value));
443+
}
418444

419-
/// todo: port
420445
/// <summary>
421446
/// Returns a multi-line hexadecimal dump of the specified {@link ByteBuf} that is easy to read by humans.
422447
/// </summary>
@@ -524,7 +549,7 @@ public static void AppendPrettyHexDump(StringBuilder dump, IByteBuffer buf, int
524549
}
525550

526551
/// <summary>
527-
/// Appends the prefix of each hex dump row. Uses the look-up table for the buffer <= 64 KiB.
552+
/// Appends the prefix of each hex dump row. Uses the look-up table for the buffer &lt;= 64 KiB.
528553
/// </summary>
529554
static void AppendHexDumpRowPrefix(StringBuilder dump, int row, int rowStartIndex)
530555
{

src/DotNetty.Buffers/CompositeByteBuffer.cs

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ public ComponentEntry(IByteBuffer buffer)
2828
this.Length = buffer.ReadableBytes;
2929
}
3030

31-
public void FreeIfNecessary()
32-
{
33-
// Unwrap so that we can free slices, too.
34-
this.Buffer.Release(); // We should not get a NPE here. If so, it must be a bug.
35-
}
31+
public void FreeIfNecessary() => this.Buffer.Release();
3632
}
3733

3834
static readonly ArraySegment<byte> EmptyNioBuffer = Unpooled.Empty.GetIoBuffer();
@@ -665,10 +661,7 @@ public int ToByteIndex(int cIndex)
665661
return this.components[cIndex].Offset;
666662
}
667663

668-
public override byte GetByte(int index)
669-
{
670-
return this._GetByte(index);
671-
}
664+
public override byte GetByte(int index) => this._GetByte(index);
672665

673666
protected override byte _GetByte(int index)
674667
{
@@ -805,10 +798,7 @@ public override IByteBuffer SetByte(int index, int value)
805798
return this;
806799
}
807800

808-
protected override void _SetByte(int index, int value)
809-
{
810-
this.SetByte(index, value);
811-
}
801+
protected override void _SetByte(int index, int value) => this.SetByte(index, value);
812802

813803
protected override void _SetShort(int index, int value)
814804
{
@@ -1009,10 +999,7 @@ void CopyTo(int index, int length, int componentId, IByteBuffer dst)
1009999
/// @param offset the offset for which the {@link IByteBuffer} should be returned
10101000
/// @return the {@link IByteBuffer} on the specified index
10111001
/// </summary>
1012-
public IByteBuffer ComponentAtOffset(int offset)
1013-
{
1014-
return this.InternalComponentAtOffset(offset).Duplicate();
1015-
}
1002+
public IByteBuffer ComponentAtOffset(int offset) => this.InternalComponentAtOffset(offset).Duplicate();
10161003

10171004
/// <summary>
10181005
/// Return the internal {@link IByteBuffer} on the specified index. Note that updating the indexes of the returned
@@ -1030,10 +1017,7 @@ public IByteBuffer InternalComponent(int cIndex)
10301017
/// buffer will lead to an undefined behavior of this buffer.
10311018
/// @param offset the offset for which the {@link IByteBuffer} should be returned
10321019
/// </summary>
1033-
public IByteBuffer InternalComponentAtOffset(int offset)
1034-
{
1035-
return this.FindComponent(offset).Buffer;
1036-
}
1020+
public IByteBuffer InternalComponentAtOffset(int offset) => this.FindComponent(offset).Buffer;
10371021

10381022
ComponentEntry FindComponent(int offset)
10391023
{
@@ -1218,10 +1202,7 @@ public override IByteBuffer DiscardReadBytes()
12181202
return this;
12191203
}
12201204

1221-
IByteBuffer AllocateBuffer(int capacity)
1222-
{
1223-
return this.Allocator.Buffer(capacity);
1224-
}
1205+
IByteBuffer AllocateBuffer(int capacity) => this.Allocator.Buffer(capacity);
12251206

12261207
public override string ToString()
12271208
{
@@ -1232,26 +1213,17 @@ public override string ToString()
12321213

12331214
public override IReferenceCounted Touch()
12341215
{
1235-
if (this.leak != null)
1236-
{
1237-
this.leak.Record();
1238-
}
1216+
this.leak?.Record();
12391217
return this;
12401218
}
12411219

12421220
public override IReferenceCounted Touch(object hint)
12431221
{
1244-
if (this.leak != null)
1245-
{
1246-
this.leak.Record(hint);
1247-
}
1222+
this.leak?.Record(hint);
12481223
return this;
12491224
}
12501225

1251-
public override IByteBuffer DiscardSomeReadBytes()
1252-
{
1253-
return this.DiscardReadComponents();
1254-
}
1226+
public override IByteBuffer DiscardSomeReadBytes() => this.DiscardReadComponents();
12551227

12561228
protected override void Deallocate()
12571229
{
@@ -1269,15 +1241,9 @@ protected override void Deallocate()
12691241
this.components[i].FreeIfNecessary();
12701242
}
12711243

1272-
if (this.leak != null)
1273-
{
1274-
this.leak.Close();
1275-
}
1244+
this.leak?.Close();
12761245
}
12771246

1278-
public override IByteBuffer Unwrap()
1279-
{
1280-
return null;
1281-
}
1247+
public override IByteBuffer Unwrap() => null;
12821248
}
12831249
}

0 commit comments

Comments
 (0)