Пример #1
0
        public override IResponse receiveAnswer()
        {
            byte[] header = new byte[BinaryResponse.HEADER_LENTGH];
            log.Info("recieving an answer...");
            int n = port.Read(header, 0, BinaryResponse.HEADER_LENTGH);

            if (n != BinaryResponse.HEADER_LENTGH)
            {
                // maybe wait for a while?
                log.Info("error reading header from device!");
                throw new InvalidDataException();
            }

            BinaryResponse.OP op      = (BinaryResponse.OP)BitConverter.ToInt32(header, 0);
            UInt32            dataLen = BitConverter.ToUInt32(header, 4);
            BinaryResponse    answer  = BinaryResponse.newDeviceAnswer(op);

            log.Info("got answer of type: " + op.ToString());
            if (dataLen != 0)
            {
                byte[] data = new byte[dataLen];
                n = port.Read(data, 0, (int)dataLen);
                if (n != dataLen)
                {
                    log.Info("could not read all the data, maybe add a loop?!");
                    throw new NotImplementedException();
                }
                answer.fromByteArray(header, data);
            }
            return(answer);
        }
Пример #2
0
        public override IResponse receiveAnswer()
        {
            try
            {
                byte[] header = new byte[BinaryResponse.HEADER_LENTGH];
                log.Info("recieving an answer...");
                int n = socket.Receive(header, BinaryResponse.HEADER_LENTGH, System.Net.Sockets.SocketFlags.None);
                if (n != BinaryResponse.HEADER_LENTGH)
                {
                    // maybe wait for a while?
                    log.Error("error reading header from device!");
                    throw new InvalidDataException();
                }

                BinaryResponse.OP op      = (BinaryResponse.OP)BitConverter.ToInt32(header, 0);
                UInt32            dataLen = BitConverter.ToUInt32(header, 4);
                BinaryResponse    answer  = BinaryResponse.newDeviceAnswer(op);

                if (answer == null)
                {
                    throw new ArgumentNullException("error in response parsing, is the OPCODE missing? {" + op.ToString() + "}");
                }

                log.Info("got answer of type: " + op.ToString());
                n = 0;
                byte[] data = new byte[dataLen];
                log.Info("data len: " + dataLen.ToString());
                while (dataLen - n > 0)
                {
                    n += socket.Receive(data, n, (int)(dataLen - n), System.Net.Sockets.SocketFlags.Partial);
                    log.Info("read " + n.ToString());
                }
                answer.fromByteArray(header, data);
                return(answer);
            } catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #3
0
        public override IResponse receiveAnswer(CancellationToken ct)
        {
            Queue dataQ = new Queue();

            Byte [] b = new byte[1];
            log.Info("recieving an answer...");
            int  n           = 0;
            bool fullMessage = false;

            port.ReadTimeout = 1000;

            while (!fullMessage)
            {
                /* check the cancellation token */
                ct.ThrowIfCancellationRequested();

                try
                {
                    n = port.Read(b, 0, 1);
                } catch (TimeoutException)
                {
                    /* this is ok, continue to next iteration */
                    continue;
                }

                if (n > 0)
                {
                    if (b[0] == CHAR_FLAG)
                    {
                        if (dataQ.Count > 1) /* op + crc */
                        {
                            fullMessage = true;
                        }
                        else
                        {
                            dataQ.Clear();
                        }
                    }
                    else
                    {
                        dataQ.Enqueue(b[0]);
                    }
                }
            }

            byte[] data = new byte[dataQ.Count];
            Array.Copy(dataQ.ToArray(), data, dataQ.Count);
            log.Info("got: " + BitConverter.ToString(data).Replace("-", " "));

            byte[] strippedData = restore_received_data(data);
            if (strippedData == null)
            {
                log.Error("error in recieved bitstream!");
                throw new RFC1662Exception();
            }

            BinaryResponse.OP op     = (BinaryResponse.OP)strippedData[0];
            BinaryResponse    answer = BinaryResponse.newDeviceAnswer(op);

            if (answer == null)
            {
                throw new UnknownOPException();
            }
            log.Info("got answer of type: " + op.ToString("X"));

            answer.fromByteArray(strippedData);

            return(answer);
        }