Exemplo n.º 1
0
        private LoopCtrl xmitAction_RemainingSeq()
        {
            var isCompleted = true;

            if (packetType == PacketType.SEQ)
            {
                if (seqPacketPayload.hasRemaining())
                {
                    loopByteChannel.write(seqPacketPayload);
                }
                if (!seqPacketPayload.hasRemaining() && seqPadding.hasRemaining())
                {
                    loopByteChannel.write(seqPadding);
                }

                var isDataComplete    = !seqPacketPayload.hasRemaining();
                var isPaddingComplete = !seqPadding.hasRemaining();
                isCompleted = isDataComplete && isPaddingComplete;

                if (isCompleted)
                {
                    HandleSeqCompletion();
                }
                else
                {
                    callXmitRequestListener();
                }
            }
            return(isCompleted ? LoopCtrl.None : LoopCtrl.Break);
        }
Exemplo n.º 2
0
        protected LoopCtrl xmitAction_RemainingXmitBuf()
        {
            if (xmitBuf.hasRemaining())
            {
                loopByteChannel.write(xmitBuf);
            }

            if (!xmitBuf.hasRemaining() && packetType != PacketType.SEQ)
            {
                handleNonSeqCompletion();
                packetType = PacketType.NONE;
            }
            if (xmitBuf.hasRemaining())
            {
                callXmitRequestListener();
            }

            return(xmitBuf.hasRemaining() ? LoopCtrl.Break : LoopCtrl.None);
        }
Exemplo n.º 3
0
        public void recv(ByteChannel channel)
        {
            cancelCurrentReceive = false;
            while (!cancelCurrentReceive)
            {
                if (packetType == PacketType.NONE)
                {
                    Utils.ensure(recvBuf.limit() >= HEADER_RECV_SZ, ChabuErrorCode.UNKNOWN, "unknown header size: {0}", recvBuf);
                    channel.read(recvBuf);
                    if (recvBuf.position() < 8)
                    {
                        break;
                    }

                    packetSize = recvBuf.getInt(0);
                    int packetTypeId = recvBuf.getInt(4) & 0xFF;
                    packetType = PacketTypeExtension.findPacketType(packetTypeId);
                    if (packetType == PacketType.NONE)
                    {
                        throw new ChabuException(string.Format("Packet type 0x{0:X2} unexpected: packetSize {1}", packetTypeId, packetSize));
                    }
                }

                if (packetType != PacketType.SEQ)
                {
                    Utils.ensure(packetSize <= Constants.MAX_RECV_LIMIT_LOW, ChabuErrorCode.UNKNOWN, "unknown header size");
                    if (packetSize > recvBuf.position())
                    {
                        recvBuf.limit(packetSize);
                        channel.read(recvBuf);
                        if (recvBuf.hasRemaining())
                        {
                            // not fully read, try next time
                            break;
                        }
                    }
                }
                else
                {
                    if (HEADER_RECV_SZ > recvBuf.position())
                    {
                        recvBuf.limit(HEADER_RECV_SZ);
                        channel.read(recvBuf);
                        if (recvBuf.hasRemaining())
                        {
                            // not fully read, try next time
                            break;
                        }
                    }
                }

                recvBuf.flip();
                recvBuf.position(8);

                if (packetType != PacketType.SEQ)
                {
                    Utils.ensure(packetSize <= Constants.MAX_RECV_LIMIT_LOW, ChabuErrorCode.UNKNOWN, "unknown header size");
                    switch (packetType)
                    {
                    case PacketType.SETUP: processRecvSetup();    break;

                    case PacketType.ACCEPT: processRecvAccept();   break;

                    case PacketType.ABORT: processRecvAbort();    break;

                    case PacketType.ARM: processRecvArm();      break;

                    case PacketType.DAVAIL: processRecvDavail();   break;

                    case PacketType.NOP: processRecvNop();      break;

                    case PacketType.RST_REQ: processRecvResetReq(); break;

                    case PacketType.RST_ACK: processRecvResetAck(); break;

                    default: break;
                    }
                    Utils.ensure(recvBuf.remaining() < HEADER_RECV_SZ, ChabuErrorCode.ASSERT, "After normal command, the remaining bytes must be below the HEADER_RECV_SZ limit, but is {0}", recvBuf.limit());
                    Utils.ensure(recvBuf.position() == packetSize, ChabuErrorCode.ASSERT, "After normal command, the remaining bytes must be below the HEADER_RECV_SZ limit, but is {0}", recvBuf.limit());
                    recvBuf.compact();
                    recvBuf.limit(HEADER_RECV_SZ);
                    packetType = PacketType.NONE;
                    continue;
                }
                else
                {
                    processSeq(channel);

                    bool isContinuingSeq = packetType == PacketType.SEQ;
                    if (isContinuingSeq)
                    {
                        break;
                    }

                    continue;
                }
            }
        }