mirror of
https://github.com/Azure/DotNetty.git
synced 2026-10-27 03:26:51 +08:00
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.
38 lines
1.2 KiB
C#
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();
|
|
}
|
|
}
|
|
} |