123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using DotNetty.Buffers;
- using DotNetty.Handlers.Timeout;
- using DotNetty.Transport.Bootstrapping;
- using DotNetty.Transport.Channels;
- using DotNetty.Transport.Channels.Sockets;
- using DotNettyFrom.common;
-
- namespace DotNettyFrom.netty
- {
- public class NettyClient
- {
- public static IChannelHandlerContext Context;
- private MultithreadEventLoopGroup _group;
- private NettyClientHandler _clientHandler;
-
- public void StartNetty(string host, int port)
- {
- IPAddress hostIp = IPAddress.Parse(host);
- RunClientAsync(hostIp, port);
- }
-
- private async void RunClientAsync(IPAddress HostIP, int Port)
- {
- _group = new MultithreadEventLoopGroup();
- var bootstrap = new Bootstrap();
- bootstrap
- .Group(_group)
- .Channel<TcpSocketChannel>()
- .Option(ChannelOption.TcpNodelay, true)
- .Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
- {
- IChannelPipeline pipeline = channel.Pipeline;
- // pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 10, 2, 1, 0));
- pipeline.AddLast("idle", new IdleStateHandler(10, 20, 15));
- _clientHandler = new NettyClientHandler();
- pipeline.AddLast("echo", _clientHandler);
- }));
- try
- {
- await bootstrap.ConnectAsync(new IPEndPoint(HostIP, Port));
- }
- catch (Exception e)
- {
- //连接出错,服务器没开启....
- Console.WriteLine(e);
- await _group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
- }
-
- }
-
-
- public void SendData(string hex)
- {
- if (Context.Channel.Active)
- {
- IByteBuffer msg = Unpooled.Buffer(128);
- byte[] messageBytes = Encoding.UTF8.GetBytes(DateTime.Now.ToLongDateString());
- msg.WriteBytes(messageBytes);
- Context.Channel.WriteAndFlushAsync(msg);
- }
- else
- {
- DataModel.ReceCollection.Add(new UIInfoModel("连接已断开", ""));
- }
- }
-
-
- public void StopNetty()
- {
- Context?.Channel.CloseAsync();
- _group?.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
- _group = null;
- Context = null;
- _clientHandler = null;
- }
-
-
- class NettyClientHandler : ChannelHandlerAdapter
- {
- public override void ChannelActive(IChannelHandlerContext context)
- {
- Context = context;
-
-
- IByteBuffer msg = Unpooled.Buffer(128);
- byte[] messageBytes = Encoding.UTF8.GetBytes(DateTime.Now.ToLongDateString());
- msg.WriteBytes(messageBytes);
- context.WriteAndFlushAsync(msg);
-
- DataModel.ReceCollection.Add(new UIInfoModel("", DateTime.Now.ToLongDateString() + "channeiActive"));
- }
-
-
- public override void ChannelRead(IChannelHandlerContext context, object message)
- {
- if (message is IByteBuffer buffer)
- {
- string abc = buffer.ToString(Encoding.UTF8);
-
- DataModel.ReceCollection.Add(new UIInfoModel("", "收到:-----------" + abc));
- }
-
- // IByteBuffer msg = Unpooled.Buffer(128);
- // byte[] messageBytes = Encoding.UTF8.GetBytes(DateTime.Now.ToLongDateString());
- // msg.WriteBytes(messageBytes);
- // context.WriteAndFlushAsync(msg);
- }
-
- public override void ChannelReadComplete(IChannelHandlerContext context)
- {
- base.ChannelReadComplete(context);
- }
-
- public override void UserEventTriggered(IChannelHandlerContext context, object evt)
- {
- base.UserEventTriggered(context, evt);
- }
-
- public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
- {
- base.ExceptionCaught(context, exception);
- Console.WriteLine(exception.Message);
- DataModel.ReceCollection.Add(new UIInfoModel("", "ExceptionCaught:" + exception.Message));
- }
-
- public override void ChannelInactive(IChannelHandlerContext context)
- {
- base.ChannelInactive(context);
- DataModel.ReceCollection.Add(new UIInfoModel("", "ChannelInactive"));
- }
- }
- }
- }
|