Files
Johnny Z 44885964bd Socket-based UDP channel transport (#79). (#171)
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.
2016-11-23 15:45:02 -08:00

38 lines
1.2 KiB
C#

// 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();
}
}
}