12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using DotNetty.Transport.Channels;
- using System;
- using System.Collections.Concurrent;
- using System.Windows.Forms;
- using Message = NettyClient.proto.Message;
- namespace NettyClient
- {
- public class SimpleMessageHandler : ChannelHandlerAdapter
- {
- public SimpleMessageHandler()
- {
- //var protobufEncoder = new ProtobufEncoder();
- }
- public static ConcurrentQueue<Message> messageQueue = new ConcurrentQueue<Message>();
- public IChannelHandlerContext context;
- public override void ChannelActive(IChannelHandlerContext context)
- {
- //MessageBox.Show("connected");
- this.context = context;
- }
- public override void ChannelRead(IChannelHandlerContext context, object message)
- {
- if (message is Message msg)
- {
- toString(msg);
- messageQueue.Enqueue(msg);
- }
- }
- public override void ChannelReadComplete(IChannelHandlerContext context)
- {
- context.Flush();
- }
- public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
- {
- Console.WriteLine("Exception: " + exception);
- context.CloseAsync();
- }
- public override void ChannelInactive(IChannelHandlerContext context)
- {
- base.ChannelInactive(context);
- Console.WriteLine("1");
- }
- public override void ChannelUnregistered(IChannelHandlerContext context)
- {
- base.ChannelUnregistered(context);
- Console.WriteLine("2");
- }
- public override void HandlerRemoved(IChannelHandlerContext context)
- {
- base.HandlerRemoved(context);
- }
- public void toString(Message msg)
- {
- Console.WriteLine("Received from server: cmd : " + msg.Cmd + ", taskId : " + msg.TaskId + ", content : " + msg.Content);
- }
- public bool WriteMessage(Message message)
- {
- if (context == null || !context.Channel.Active)
- {
- return false;
- }
- try
- {
- context.WriteAndFlushAsync(message);
- }
- catch
- {
- return false;
- }
- return true;
- }
- }
- }
|