我是DotNetty的新手,我试图从套接字接收一个简单的请求(二进制格式)
我要反序列化的课程是
public class PKKeepAlive
{
public uint PacketType => 12003;
}
我尝试使用的处理程序是
public class KeepAliveResponseHandler : SimpleChannelInboundHandler<PKKeepAlive>
{
public override void ChannelRead(IChannelHandlerContext ctx, object msg)
{
var obj = msg as IByteBuffer;
base.ChannelRead(ctx, msg);
}
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
{
Console.WriteLine("Exception: " + exception);
context.CloseAsync();
}
protected override void ChannelRead0(IChannelHandlerContext ctx, PKKeepAlive packet)
{
string message = packet.ToString();
Console.WriteLine($"Quote of the Moment: {message.Substring(6)}");
ctx.CloseAsync();
}
}
这是在WPF示例应用程序中注册并执行
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var group = new MultithreadEventLoopGroup();
var bootstrap = new Bootstrap();
bootstrap
.Group(group)
.Channel<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
//channel.Pipeline.AddLast("LoginResponse", new Temp());
channel.Pipeline.AddLast("KeepAliveResponse", new KeepAliveResponseHandler());
}));
IPAddress ip = IPAddress.Parse("192.168.0.116");
IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(ip, 20000));
var login = new LoginRequest { UserName = "if" };
var bytes = login.Serialize();
IByteBuffer buffer = Unpooled.WrappedBuffer(bytes);
await bootstrapChannel.WriteAndFlushAsync(buffer);
}
我遇到的问题是我可以在公共覆盖void ChannelRead(IChannelHandlerContext ctx,object msg)方法上有一个断点,我看到我有8个字节可读,但它没有停止on protected override void ChannelRead0(IChannelHandlerContext ctx,PKKeepAlive packet)我认为我应该让包刚刚反序列化
在我的真实场景中考虑我必须根据uint PacketType处理不同的数据包(实际上在超级插件中我读取该值并执行切换)
对此有何建议? 感谢