-
Notifications
You must be signed in to change notification settings - Fork 982
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Motivation: Ported/Re-implemented DatagramChannel/UDP into DotNetty Modifications: - Added DatagramChannel/Config into transport project. - Added DatagramPacket/DefaultAddressedEnvelop/IAddressedEnvelop into transport project. - Added DatagramPacketEncoder/DatagramPacketDecoder into Codec project. - Extra: Ported QuoteOfTheMoment UDP example project. Result: DatagramChannel/UDP transport is supported by DotNetty.
- Loading branch information
Showing
39 changed files
with
2,543 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace QuoteOfTheMoment.Client | ||
{ | ||
using System; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DotNetty.Buffers; | ||
using DotNetty.Transport.Bootstrapping; | ||
using DotNetty.Transport.Channels; | ||
using DotNetty.Transport.Channels.Sockets; | ||
using Examples.Common; | ||
|
||
class Program | ||
{ | ||
static async Task RunClientAsync() | ||
{ | ||
ExampleHelper.SetConsoleLogger(); | ||
|
||
var group = new MultithreadEventLoopGroup(); | ||
|
||
try | ||
{ | ||
var bootstrap = new Bootstrap(); | ||
bootstrap | ||
.Group(group) | ||
.Channel<SocketDatagramChannel>() | ||
.Option(ChannelOption.SoBroadcast, true) | ||
.Handler(new ActionChannelInitializer<ISocketChannel>(channel => | ||
{ | ||
channel.Pipeline.AddLast("Quote", new QuoteOfTheMomentClientHandler()); | ||
})); | ||
|
||
IChannel clientChannel = await bootstrap.BindAsync(IPEndPoint.MinPort); | ||
|
||
Console.WriteLine("Sending broadcast QOTM"); | ||
|
||
// Broadcast the QOTM request to port. | ||
byte[] bytes = Encoding.UTF8.GetBytes("QOTM?"); | ||
IByteBuffer buffer = Unpooled.WrappedBuffer(bytes); | ||
await clientChannel.WriteAndFlushAsync( | ||
new DatagramPacket( | ||
buffer, | ||
new IPEndPoint(IPAddress.Broadcast, ClientSettings.Port))); | ||
|
||
Console.WriteLine("Waiting for response."); | ||
|
||
await Task.Delay(5000); | ||
Console.WriteLine("Waiting for response time 5000 completed. Closing client channel."); | ||
|
||
await clientChannel.CloseAsync(); | ||
} | ||
finally | ||
{ | ||
await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)); | ||
} | ||
} | ||
|
||
static void Main() => RunClientAsync().Wait(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/QuoteOfTheMoment.Client/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("QuoteOfTheMoment.Client")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("81aa23b3-e975-403c-8c90-0aa0e572b539")] |
21 changes: 21 additions & 0 deletions
21
examples/QuoteOfTheMoment.Client/QuoteOfTheMoment.Client.xproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>81aa23b3-e975-403c-8c90-0aa0e572b539</ProjectGuid> | ||
<RootNamespace>QuoteOfTheMoment.Client</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
38 changes: 38 additions & 0 deletions
38
examples/QuoteOfTheMoment.Client/QuoteOfTheMomentClientHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace QuoteOfTheMoment.Client | ||
{ | ||
using System; | ||
using System.Text; | ||
using DotNetty.Transport.Channels; | ||
using DotNetty.Transport.Channels.Sockets; | ||
|
||
public class QuoteOfTheMomentClientHandler : SimpleChannelInboundHandler<DatagramPacket> | ||
{ | ||
protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket packet) | ||
{ | ||
Console.WriteLine($"Client Received => {packet}"); | ||
|
||
if (!packet.Content.IsReadable()) | ||
{ | ||
return; | ||
} | ||
|
||
string message = packet.Content.ToString(Encoding.UTF8); | ||
if (!message.StartsWith("QOTM: ")) | ||
{ | ||
return; | ||
} | ||
|
||
Console.WriteLine($"Quote of the Moment: {message.Substring(6)}"); | ||
ctx.CloseAsync(); | ||
} | ||
|
||
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) | ||
{ | ||
Console.WriteLine("Exception: " + exception); | ||
context.CloseAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"port": "7686" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "QuoteOfTheMoment.Client", | ||
"description": "Example of QuoteOfTheMoment Client", | ||
"buildOptions": { | ||
"emitEntryPoint": true, | ||
"copyToOutput": { | ||
"include": [ "appsettings.json", "..\\..\\shared\\dotnetty.com.pfx" ] | ||
} | ||
}, | ||
"dependencies": { | ||
"DotNetty.Common": { | ||
"target": "project" | ||
}, | ||
"DotNetty.Buffers": { | ||
"target": "project" | ||
}, | ||
"DotNetty.Transport": { | ||
"target": "project" | ||
}, | ||
"DotNetty.Handlers": { | ||
"target": "project" | ||
}, | ||
"DotNetty.Codecs": { | ||
"target": "project" | ||
}, | ||
"Examples.Common": { | ||
"target": "project" | ||
} | ||
}, | ||
"frameworks": { | ||
"netcoreapp1.0": { | ||
"dependencies": { | ||
"Microsoft.NETCore.App": { | ||
"version": "1.0.0-*", | ||
"type": "platform" | ||
} | ||
}, | ||
"imports": "dnxcore50" | ||
}, | ||
"net451": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace QuoteOfTheMoment.Server | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using DotNetty.Handlers.Logging; | ||
using DotNetty.Transport.Bootstrapping; | ||
using DotNetty.Transport.Channels; | ||
using DotNetty.Transport.Channels.Sockets; | ||
using Examples.Common; | ||
|
||
class Program | ||
{ | ||
static async Task RunServerAsync() | ||
{ | ||
ExampleHelper.SetConsoleLogger(); | ||
|
||
var group = new MultithreadEventLoopGroup(); | ||
try | ||
{ | ||
var bootstrap = new Bootstrap(); | ||
bootstrap | ||
.Group(group) | ||
.Channel<SocketDatagramChannel>() | ||
.Option(ChannelOption.SoBroadcast, true) | ||
.Handler(new LoggingHandler("SRV-LSTN")) | ||
.Handler(new ActionChannelInitializer<ISocketChannel>(channel => | ||
{ | ||
channel.Pipeline.AddLast("Quote", new QuoteOfTheMomentServerHandler()); | ||
})); | ||
|
||
IChannel boundChannel = await bootstrap.BindAsync(ServerSettings.Port); | ||
Console.WriteLine("Press any key to terminate the server."); | ||
Console.ReadLine(); | ||
|
||
await boundChannel.CloseAsync(); | ||
} | ||
finally | ||
{ | ||
await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)); | ||
} | ||
} | ||
|
||
static void Main() => RunServerAsync().Wait(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/QuoteOfTheMoment.Server/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("QuoteOfTheMoment.Server")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("5e6f211f-a215-409c-8a6d-5ac0251f66b5")] |
21 changes: 21 additions & 0 deletions
21
examples/QuoteOfTheMoment.Server/QuoteOfTheMoment.Server.xproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>5e6f211f-a215-409c-8a6d-5ac0251f66b5</ProjectGuid> | ||
<RootNamespace>QuoteOfTheMoment.Server</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
59 changes: 59 additions & 0 deletions
59
examples/QuoteOfTheMoment.Server/QuoteOfTheMomentServerHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace QuoteOfTheMoment.Server | ||
{ | ||
using System; | ||
using System.Text; | ||
using DotNetty.Buffers; | ||
using DotNetty.Transport.Channels; | ||
using DotNetty.Transport.Channels.Sockets; | ||
|
||
public class QuoteOfTheMomentServerHandler : SimpleChannelInboundHandler<DatagramPacket> | ||
{ | ||
static readonly Random Random = new Random(); | ||
|
||
// Quotes from Mohandas K. Gandhi: | ||
static readonly string[] Quotes = | ||
{ | ||
"Where there is love there is life.", | ||
"First they ignore you, then they laugh at you, then they fight you, then you win.", | ||
"Be the change you want to see in the world.", | ||
"The weak can never forgive. Forgiveness is the attribute of the strong.", | ||
}; | ||
|
||
static string NextQuote() | ||
{ | ||
int quoteId = Random.Next(Quotes.Length); | ||
return Quotes[quoteId]; | ||
} | ||
|
||
protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket packet) | ||
{ | ||
Console.WriteLine($"Server Received => {packet}"); | ||
|
||
if (!packet.Content.IsReadable()) | ||
{ | ||
return; | ||
} | ||
|
||
string message = packet.Content.ToString(Encoding.UTF8); | ||
if (message != "QOTM?") | ||
{ | ||
return; | ||
} | ||
|
||
byte[] bytes = Encoding.UTF8.GetBytes("QOTM: " + NextQuote()); | ||
IByteBuffer buffer = Unpooled.WrappedBuffer(bytes); | ||
ctx.WriteAsync(new DatagramPacket(buffer, packet.Sender)); | ||
} | ||
|
||
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush(); | ||
|
||
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) | ||
{ | ||
Console.WriteLine("Exception: " + exception); | ||
context.CloseAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"port": "7686" | ||
} |
Oops, something went wrong.