示例#1
0
        public void doXfer()
        {
            bool loop = true;

            while (loop)
            {
                loop = false;

                if (inState == InState.RecvHdr)
                {
                    int r = Base.SockRead(sock, inBuf, inPos, inBuf.Length - inPos);

                    if (r > 0)
                    {
                        loop   = true;
                        inPos += r;

                        if (inPos == inBuf.Length)
                        {
                            inMsg = new AnpMsg();

                            UInt32 size = 0;
                            AnpMsg.ParseHdr(inBuf, ref inMsg.Major, ref inMsg.Minor, ref inMsg.Type, ref inMsg.ID, ref size);

                            if (size > AnpMsg.MaxSize)
                            {
                                throw new AnpException("ANP message is too large");
                            }

                            if (size > 0)
                            {
                                inState = InState.RecvPayload;
                                inBuf   = new byte[size];
                                inPos   = 0;
                            }

                            else
                            {
                                inState = InState.Received;
                            }
                        }
                    }
                }

                if (inState == InState.RecvPayload)
                {
                    int r = Base.SockRead(sock, inBuf, inPos, inBuf.Length - inPos);

                    if (r > 0)
                    {
                        loop   = true;
                        inPos += r;

                        if (inPos == inBuf.Length)
                        {
                            inMsg.Elements = AnpMsg.ParsePayload(inBuf);
                            inState        = InState.Received;
                        }
                    }
                }

                if (outState == OutState.Sending)
                {
                    int r = Base.SockWrite(sock, outBuf, outPos, outBuf.Length - outPos);

                    if (r > 0)
                    {
                        loop    = true;
                        outPos += r;

                        if (outPos == outBuf.Length)
                        {
                            outState = OutState.NoPacket;
                            break;
                        }
                    }
                }
            }
        }