Browse Source

调整协议粘包

develop
H Vs 8 months ago
parent
commit
7486b73b69
1 changed files with 96 additions and 1 deletions
  1. +96
    -1
      NearCardAttendance.Service/TcpServer/Handler/ProtocolHandler.cs

+ 96
- 1
NearCardAttendance.Service/TcpServer/Handler/ProtocolHandler.cs View File

@@ -43,7 +43,6 @@ namespace NearCardAttendance.Service.TcpServer.Handler
_logger.LogInformation($"{nameof(ProtocolHandler)} --- {nameof(ChannelRead0)} 处理出错\n{ex.Message}\n{ex.StackTrace}");
}
}

private void ProcessMessage(IChannelHandlerContext context, string content)
{
buffer ??= Unpooled.Buffer();
@@ -51,6 +50,102 @@ namespace NearCardAttendance.Service.TcpServer.Handler
byte[] bytes = Encoding.ASCII.GetBytes(content);
buffer.WriteBytes(Unpooled.WrappedBuffer(bytes));

if (int.TryParse(buffer.ToString(Encoding.ASCII).Substring(0, 4), out int messageLength))
{
Console.WriteLine(buffer.ToString(Encoding.ASCII));
if (buffer.ToString(Encoding.ASCII).Length == messageLength)
{
//var parser = buffer.ToString(Encoding.ASCII);
ProtocolParser parser = new(buffer.ToString(Encoding.ASCII));
context.FireChannelRead(parser);
Console.WriteLine($"发送正常:{parser}");
//ProtocolWrapper wrapper = new ProtocolWrapper("82", parser.SeqNo, DateTime.Now.ToString("yyyyMMddHHmmss"));
//Console.WriteLine(wrapper.GenerateProtocolString());
Console.WriteLine($"length:{parser.MessageLength},func_no:{parser.FuncNo},seq_no:{parser.SeqNo},data:{parser.Data}");
ReleaseBuffer();
}
else if (buffer.ToString(Encoding.ASCII).Length > messageLength)
{
string inputString = buffer.ToString(Encoding.ASCII);
var nextMessageLength = messageLength;
var startIndex = 0;
var messagesStringLength = 0;

List<string> messages = new List<string>();

while (startIndex < inputString.Length)
{
try
{
string message = inputString.Substring(startIndex, nextMessageLength);
messages.Add(message);

startIndex += nextMessageLength;

if (startIndex + 4 <= inputString.Length)
{
if (!int.TryParse(inputString.Substring(startIndex, 4), out nextMessageLength))
{
break;
}
}
else
{
break;
}
}
catch (Exception)
{
break;
}
}

foreach (var message in messages)
{
ProtocolParser parser = new(message);
context.FireChannelRead(parser);
messagesStringLength += message.Length;
// Console.WriteLine(message);
}
// 过长且不完整
if (inputString.Length > messageLength)
{
var overLongbuffer = Unpooled.Buffer();
overLongbuffer.WriteBytes(Unpooled.WrappedBuffer(Encoding.ASCII.GetBytes(inputString.Substring(messageLength, inputString.Length- messageLength))));
ReleaseBuffer();
buffer = overLongbuffer;
}
// 数据完整
if (inputString.Length ==messageLength)
{
ReleaseBuffer();
}
//Console.WriteLine($"{stringLength},{inputString.Length}");


// var parser = buffer.ToString(Encoding.ASCII).Substring(0, messageLength);
//ProtocolParser parser = new(buffer.ToString(Encoding.ASCII).Substring(0, messageLength));
//context.FireChannelRead(parser);
////ReleaseBuffer();

//var overLongbuffer = Unpooled.Buffer();
//Console.WriteLine($"过长消息:{buffer.ToString(Encoding.ASCII).Substring(messageLength)}");
//overLongbuffer.WriteBytes(Unpooled.WrappedBuffer(Encoding.ASCII.GetBytes(buffer.ToString(Encoding.ASCII).Substring(messageLength))));
//ReleaseBuffer();
//buffer = overLongbuffer;
//Console.WriteLine($"剩余消息{buffer.ToString(Encoding.ASCII)}");
}
}
}

private void ProcessMessage2(IChannelHandlerContext context, string content)
{
buffer ??= Unpooled.Buffer();

byte[] bytes = Encoding.ASCII.GetBytes(content);
buffer.WriteBytes(Unpooled.WrappedBuffer(bytes));

if (int.TryParse(buffer.ToString(Encoding.ASCII).Substring(0, 4), out int messageLength))
{
Console.WriteLine(buffer.ToString(Encoding.ASCII));


Loading…
Cancel
Save