コード例 #1
0
        /// <summary>
        /// Return the next element if it has the expected type. Throw an
        /// exception otherwise.
        /// </summary>
        private K3pElement GetNext(K3pElement.K3pType expected)
        {
            K3pElement el = Reader();

            CheckType(expected, el.Type);
            return(el);
        }
コード例 #2
0
 /// <summary>
 /// Throw an exception if the expected type does not match the actual
 /// type.
 /// </summary>
 private void CheckType(K3pElement.K3pType expected, K3pElement.K3pType actual)
 {
     if (expected != actual)
     {
         throw new K3pException("expected K3P " + K3pElement.GetTypeName(expected) +
                                ", received K3P " + K3pElement.GetTypeName(actual));
     }
 }
コード例 #3
0
        public K3pElement getRecv()
        {
            Debug.Assert(doneReceiving);
            K3pElement m = inMsg;

            flushRecv();
            return(m);
        }
コード例 #4
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 K3pElement();

                            inMsg.ParseType(inBuf);

                            switch (inMsg.Type)
                            {
                            case K3pElement.K3pType.INS:
                                inState = InState.RecvIns;
                                inPos   = 0;
                                inBuf   = new byte[8];
                                break;

                            case K3pElement.K3pType.INT:
                                inState = InState.RecvInt;
                                inPos   = 0;
                                inBuf   = new byte[20];
                                break;

                            case K3pElement.K3pType.STR:
                                inState = InState.RecvStrSize;
                                inBuf   = new byte[20];
                                inPos   = 0;
                                break;
                            }
                        }
                    }
                }

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

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

                        if (inPos == inBuf.Length)
                        {
                            inMsg.ParseIns(inBuf);
                            inState = InState.Received;
                        }
                    }
                }

                if (inState == InState.RecvInt || inState == InState.RecvStrSize)
                {
                    int r = Base.SockRead(sock, inBuf, inPos, 1);

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

                        if ((char)inBuf[inPos - 1] == '>')
                        {
                            byte[] tmpInBuf = new byte[inPos];
                            for (int i = 0; i < inPos; i++)
                            {
                                tmpInBuf[i] = inBuf[i];
                            }
                            inMsg.ParseInt(tmpInBuf);
                            if (inState == InState.RecvStrSize)
                            {
                                inState = InState.RecvStr;
                                inBuf   = new byte[inMsg.Int];
                                inPos   = 0;

                                if (inMsg.Int == 0)
                                {
                                    inMsg.ParseStr(inBuf);
                                    inState = InState.Received;
                                }
                            }
                            else
                            {
                                inState = InState.Received;
                            }
                        }
                        else if (inPos == inBuf.Length)
                        {
                            throw new K3pException("Expecting a '>' to end an INT in k3p message");
                        }
                    }
                }

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

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

                        if (inPos == inBuf.Length)
                        {
                            inMsg.ParseStr(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;
                        }
                    }
                }
            }
        }