public override void Decode(Action <byte[]> packet) { Session session = (Session)UserToken; EofStream eofhead = session.BufferHead; EofStream eofbody = session.BufferBody; loop: if (EnsureHeadComplete()) { if (session.ReceiveCapacity == 0) { session.ReceiveCapacity = Kit.BitToInt16(eofhead.ToArray()); } if (EnsureBodyComplete()) { packet(eofbody.ToArray()); session.ReceiveCapacity = 0; eofhead.Clear(); eofbody.Clear(); } } if (session.ReceiveOffset < BytesTransferred) { goto loop; } session.ReceiveOffset = 0; }
/// <summary> /// 加载包头 /// </summary> /// <param name="tmpStream"></param> /// <returns></returns> internal int LoadHead(Span <byte> stream) { Span <byte> tmpStream = stream; int Offset = 0; //如果包头不完整 if (!HasHeader && tmpStream.Length > 0) { //如果大于-1 说明包头未加载完成 if (HeadTargetLength > -1) { //可以读完包头 if (tmpStream.Length >= (HeadTargetLength - HeadCurrentLength)) { tmpStream.Slice(0, HeadTargetLength - HeadCurrentLength).CopyTo(HeadCache.Slice(HeadCurrentLength).Span); HeadCurrentLength = HeadTargetLength; Span <byte> lengthBit = HeadCache.Slice(1).Span; Offset += lengthBit.Length; switch (lengthBit.Length) { case 1: BodyTargetLength = lengthBit[0]; break; case 2: BodyTargetLength = Kit.BitToInt16(lengthBit.ToArray()); break; case 3: BodyTargetLength = Kit.BitToInt32(lengthBit.ToArray()); break; } BodyCurrentLength = 0; } else { tmpStream.Slice(0, tmpStream.Length).CopyTo(HeadCache.Slice(HeadCurrentLength).Span); HeadCurrentLength += tmpStream.Length; Offset += tmpStream.Length; } } //包头未加载过首先获取包头类型 else { //创建包头缓存 var packetType = tmpStream[0]; switch (packetType) { case 1: //byte 1字节 case 255: //disconnect 1字节 case 254: //reconnect 1字节 HeadTargetLength = 2; break; case 2: //short 2字节 HeadTargetLength = 3; break; case 3: //int 4字节 HeadTargetLength = 5; break; } HeadCache = new byte[HeadTargetLength]; Mode = (MessageMode)packetType; //优先加载封包类型 HeadCache.Span[0] = packetType; HeadCurrentLength = 1; Offset += 1; //递归执行未加载完成的问题 if (tmpStream.Length > 1) { Offset += LoadHead(tmpStream.Slice(1)); } } } return(Offset); }
public override void Decode(Action <byte[]> packet) { Session session = (Session)UserToken; EofStream eof = session.BufferHead; loop: if (session.ReceiveType == 0) { session.ReceiveType = MemoryBuffer.Span[session.ReceiveOffset++]; if (session.ReceiveOffset == MemoryBuffer.Length) { session.ReceiveOffset = 0; return; } } if (session.ReceiveCapacity == 0 && session.ReceiveOffset < MemoryBuffer.Length) { int pos; switch (session.ReceiveType) { case (int)MessageMode.MessageShort: if (!EnsureOffSet(2)) { return; } pos = 2 - eof.Count; eof.AddRange(MemoryBuffer.Slice(session.ReceiveOffset, pos).ToArray()); session.ReceiveOffset += pos; session.ReceiveCapacity = Kit.BitToInt16(eof.ToArray()); break; case (int)MessageMode.MessageInt: if (!EnsureOffSet(4)) { return; } pos = 4 - eof.Count; eof.AddRange(MemoryBuffer.Slice(session.ReceiveOffset, pos).ToArray()); session.ReceiveOffset += pos; session.ReceiveCapacity = Kit.BitToInt32(eof.ToArray()); break; case 255: session.Client?.Shutdown(SocketShutdown.Both); session.ReceiveType = 0; return; } int remaining = BytesTransferred - session.ReceiveOffset; byte[] bytes = MemoryBuffer.Slice(session.ReceiveOffset, session.ReceiveCapacity > remaining ? remaining : session.ReceiveCapacity).ToArray(); session.BufferBody.AddRange(bytes); session.ReceiveOffset += bytes.Length; } else if (session.ReceiveCapacity > session.BufferBody.Count) { int due = session.ReceiveCapacity - session.BufferBody.Count; int remaining = BytesTransferred - session.ReceiveOffset; byte[] bytes = MemoryBuffer.Slice(session.ReceiveOffset, due > remaining ? remaining : due).ToArray(); session.BufferBody.AddRange(bytes); session.ReceiveOffset += bytes.Length; } if (session.ReceiveCapacity == session.BufferBody.Count) { packet.Invoke(session.BufferBody.ToArray()); session.ReceiveType = 0; session.ReceiveCapacity = 0; session.BufferBody.Clear(); if (eof.Count > 0) { eof.Clear(); } } if (session.ReceiveOffset < BytesTransferred) { goto loop; } session.ReceiveOffset = 0; }