Files
DotNetty/examples/Echo.Client/EchoClientHandler.cs
T
hide1202 d7cbe06feb Implemented ToString methods in ByteBuffers.
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.
2016-01-25 08:40:04 +09:00

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