Пример #1
0
        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;
        }
Пример #2
0
        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;
        }