Exemplo n.º 1
0
        public void TestCalculate()
        {
            var value1 = _provider.Calculate(Bytes1);
            var value2 = _provider.Calculate(Bytes2);
            var value3 = _provider.Calculate(Bytes1);

            Assert.AreNotEqual(value1, value2);
            Assert.AreEqual(value1, value3);
        }
Exemplo n.º 2
0
        public static Parser <T> WithChecksum <T>(this Parser <T> parser, IChecksum checksum)
        {
            if (parser == null)
            {
                throw new ArgumentNullException("parser");
            }

            return(i =>
            {
                var start = i.Position;
                var a = parser(i);
                var end = a.Remainder.Position;

                return a.IfSuccess(
                    b =>
                {
                    return Parse.Bytes(checksum.Length)(a.Remainder)
                    .IfSuccess(
                        c =>
                    {
                        var expected = checksum.Calculate(a.Remainder.Source.Skip(start).Take(end - start).ToArray());
                        var actual = c.Value.ToArray();
                        return expected.SequenceEqual(actual)
                                        ? Result.Success(b.Value, c.Remainder)
                                        : Result.Failure <T>(b.Remainder, "invalid checksum", new[] { string.Format("expected checksum {0} but was {1}", BitConverter.ToString(expected), BitConverter.ToString(actual)) });
                    }
                        );
                });
            });
        }
Exemplo n.º 3
0
        internal bool Write(BlazeCommand command, params byte[] data)
        {
            ByteList byteList = new ByteList();
            ushort   length   = 0;

            if (data != null)
            {
                length = (ushort)data.Length;
            }

            // Add a '$' sync character
            byteList.Add(36);
            byteList.Add((byte)command);
            byteList.AddUInt16(length);

            if (length > 0)
            {
                byteList.AddRange(data);
            }

            ushort csum = 0;

            if (_checksum != null)
            {
                // Don't count the
                csum = _checksum.Calculate(byteList, 0, byteList.Count);
            }

            byteList.Add((byte)(csum >> 8));
            byteList.Add((byte)csum);

            byteList.TrimExcess();

            _replyReceived.Reset();

            _startTime = Environment.TickCount;

            if (_dataBuffer != null)
            {
                _dataBuffer.Clear();
            }

            _provider.Write(byteList);

            bool flag = _replyReceived.WaitOne(_provider.Timeout);

            _endTime = Environment.TickCount;

            return(flag);
        }