public JT808TcpClient(JT808DeviceConfig deviceConfig, IServiceProvider serviceProvider) { DeviceConfig = deviceConfig; LoggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>(); JT808SendAtomicCounterService jT808SendAtomicCounterService = serviceProvider.GetRequiredService <JT808SendAtomicCounterService>(); JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService = serviceProvider.GetRequiredService <JT808ReceiveAtomicCounterService>(); IJT808Config jT808Config = serviceProvider.GetRequiredService <IJT808Config>(); group = new MultithreadEventLoopGroup(1); Bootstrap bootstrap = new Bootstrap(); bootstrap.Group(group); bootstrap.Channel <TcpSocketChannel>(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { bootstrap.Option(ChannelOption.SoReuseport, true); } bootstrap .Option(ChannelOption.SoBacklog, 8192) .Handler(new ActionChannelInitializer <IChannel>(channel => { channel.Pipeline.AddLast("jt808TcpBuffer", new DelimiterBasedFrameDecoder(int.MaxValue, Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); channel.Pipeline.AddLast("systemIdleState", new IdleStateHandler(60, deviceConfig.Heartbeat, 3600)); channel.Pipeline.AddLast("jt808TcpDecode", new JT808ClientTcpDecoder()); channel.Pipeline.AddLast("jt808TcpEncode", new JT808ClientTcpEncoder(jT808Config, jT808SendAtomicCounterService, LoggerFactory)); channel.Pipeline.AddLast("jt808TcpClientConnection", new JT808TcpClientConnectionHandler(this)); channel.Pipeline.AddLast("jt808TcpService", new JT808TcpClientHandler(jT808ReceiveAtomicCounterService, this)); })); clientChannel = bootstrap.ConnectAsync(IPAddress.Parse(DeviceConfig.TcpHost), DeviceConfig.TcpPort).Result; }
public JT808TcpClient(JT808DeviceConfig deviceConfig, IServiceProvider serviceProvider) { DeviceConfig = deviceConfig; LoggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>(); JT808SendAtomicCounterService jT808SendAtomicCounterService = serviceProvider.GetRequiredService <JT808SendAtomicCounterService>(); JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService = serviceProvider.GetRequiredService <JT808ReceiveAtomicCounterService>(); IJT808Config jT808Config = serviceProvider.GetRequiredService <IJT808Config>(); group = new MultithreadEventLoopGroup(1); Bootstrap bootstrap = new Bootstrap(); bootstrap.Group(group); bootstrap.Channel <TcpSocketChannel>(); bootstrap .Option(ChannelOption.Allocator, new PooledByteBufferAllocator()) .Handler(new ActionChannelInitializer <IChannel>(channel => { channel.Pipeline.AddLast("jt808TcpBuffer", new DelimiterBasedFrameDecoder(65535, Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); channel.Pipeline.AddLast("systemIdleState", new IdleStateHandler(60, deviceConfig.Heartbeat, 3600)); channel.Pipeline.AddLast("jt808TcpDecode", new JT808ClientTcpDecoder()); channel.Pipeline.AddLast("jt808TcpEncode", new JT808ClientTcpEncoder(jT808Config, jT808SendAtomicCounterService, LoggerFactory)); channel.Pipeline.AddLast("jt808TcpClientConnection", new JT808TcpClientConnectionHandler(this)); channel.Pipeline.AddLast("jt808TcpService", new JT808TcpClientHandler(jT808ReceiveAtomicCounterService, this)); })); Task.Run(async() => { clientChannel = await bootstrap.ConnectAsync(IPAddress.Parse(DeviceConfig.TcpHost), DeviceConfig.TcpPort); }); }
public JT808TcpClient Create(JT808DeviceConfig deviceConfig) { if (dict.TryGetValue(deviceConfig.TerminalPhoneNo, out var client)) { return(client); } else { JT808TcpClient jT808TcpClient = new JT808TcpClient(deviceConfig, serviceProvider); dict.TryAdd(deviceConfig.TerminalPhoneNo, jT808TcpClient); return(jT808TcpClient); } }