SmallChi / JT808Gateway

JT/T808 Gateway,JT808 Gateway, GB808 Gateway(support 2011, 2013, 2019 version) JT/T808网关、JT808网关、GB808网关(支持2011、2013、2019版本)
MIT License
183 stars 117 forks source link

Pipeline版本怎么集成LengthFieldBasedFrameDecoder协议? #11

Closed 491134648 closed 4 years ago

491134648 commented 4 years ago

Pipeline版本怎么集成LengthFieldBasedFrameDecoder协议,调试代码的时候发现不是正确包的时候,抛出了相关异常,下次再发数据包时就进入了Write了,然后就断开了连接,是为什么呢?

SmallChi commented 4 years ago

额,这个可以参考下SuperSocket的LengthFieldBasedFrameDecoder,因为808没有用到固定长度的包,这边也没有打算在808下支持多协议的骚操作。😄

491134648 commented 4 years ago

这地方为什么要移除会话呢?SessionManager.RemoveBySessionId(session.SessionID); public Task StartAsync(CancellationToken cancellationToken) { Logger.LogInformation($"JT808 TCP Server start at {IPAddress.Any}:{Configuration.TcpPort}."); Task.Factory.StartNew(async () => { while (!cancellationToken.IsCancellationRequested) { var socket = await server.AcceptAsync(); JT808TcpSession jT808TcpSession = new JT808TcpSession(socket); SessionManager.TryAdd(jT808TcpSession); await Task.Factory.StartNew(async (state) => { var session = (JT808TcpSession)state; if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[Connected]:{session.Client.RemoteEndPoint}"); } var pipe = new Pipe(); Task writing = FillPipeAsync(session, pipe.Writer); Task reading = ReadPipeAsync(session, pipe.Reader); await Task.WhenAll(reading, writing); SessionManager.RemoveBySessionId(session.SessionID); }, jT808TcpSession); } }, cancellationToken); return Task.CompletedTask; }

SmallChi commented 4 years ago

因为tcp这时候已经断开了,所以要移除掉。

491134648 commented 4 years ago

这个实际是没有断开,只是发的数据包格式不符合数据包规范,你可以发个错误包看下

SmallChi commented 4 years ago

对呀,你都说了发了错误的包,那对于协议来说,肯定是不允许的,那只能是拒绝连接了。

491134648 commented 4 years ago

好吧,这个现实场景也会有网络干扰吧,比如信号强度不好的地方。

SmallChi commented 4 years ago

网络问题,顶多就是断开重连,设备是有补传数据的,这都是正常的。

yedajiang44 commented 3 years ago

@491134648 DotNetty可以通过继承ByteToMessageDecoder,然后实现Decode的方法中根据协议规则去判断是哪种协议,并添加相应的handler即可,多协议归根结底还是handler的增加、删除、切换而已

注意:ChildHandler中只需要添加你实现的ByteToMessageDecoder,否则你需要做额外的工作

最后祝君好运~