mirror of
https://github.com/Azure/DotNetty.git
synced 2026-10-24 01:28:34 +08:00
d7cbe06feb
Motivation: If users want to change the ByteBuffer of the string, it is complicated. Modifications: Implement ToString methods in IByteBuffer and derived classes. Result: Users can easily use to change IByteBuffer to the string.
51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
using DotNetty.Buffers;
|
|
using DotNetty.Transport.Channels;
|
|
|
|
namespace Echo.Client
|
|
{
|
|
using System;
|
|
using System.Text;
|
|
|
|
public class EchoClientHandler : ChannelHandlerAdapter
|
|
{
|
|
readonly IByteBuffer initialMessage;
|
|
readonly byte[] buffer;
|
|
|
|
public EchoClientHandler()
|
|
{
|
|
this.buffer = new byte[EchoClientSettings.Size];
|
|
this.initialMessage = Unpooled.Buffer(EchoClientSettings.Size);
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes("Hello world");
|
|
this.initialMessage.WriteBytes(messageBytes);
|
|
}
|
|
|
|
public override void ChannelActive(IChannelHandlerContext context)
|
|
{
|
|
context.WriteAndFlushAsync(this.initialMessage);
|
|
}
|
|
|
|
public override void ChannelRead(IChannelHandlerContext context, object message)
|
|
{
|
|
var byteBuffer = message as IByteBuffer;
|
|
if (byteBuffer != null)
|
|
{
|
|
Console.WriteLine("Received from server: " + byteBuffer.ToString(Encoding.UTF8));
|
|
}
|
|
context.WriteAsync(message);
|
|
}
|
|
|
|
public override void ChannelReadComplete(IChannelHandlerContext context)
|
|
{
|
|
context.Flush();
|
|
}
|
|
|
|
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
|
|
{
|
|
context.CloseAsync();
|
|
}
|
|
}
|
|
}
|