/// <summary>
        /// Returns the number of bytes to read from the stream.
        /// </summary>
        /// <param name="state">Instance of <see cref="T:NavtelecomProtocol.SessionState" />.</param>
        /// <param name="reader"><see cref="T:SharpStructures.BinaryListReader" /> linked to a FLEX message body.</param>
        /// <param name="writer"><see cref="T:SharpStructures.MemoryBufferWriter" /> with data to be sent to the client.</param>
        /// <param name="token">The token to monitor for cancellation requests.</param>
        /// <returns>A task that represents the asynchronous operation. Contains the total number of bytes to read from the socket. Zero bytes to stop reading and send the response.</returns>
        public async Task <int> GetPendingBytesAsync(SessionState state, BinaryListReader reader, MemoryBufferWriter writer, CancellationToken token)
        {
            switch (reader.ByteList.Count)
            {
            case 2:
                return(13);

            case 15:
                reader.SetPosition(13);

                return(reader.ReadUInt16() + 1);

            default:
                if (reader.ByteList.Last() != BinaryUtilities.GetCrc8(reader.ByteList.Take(reader.ByteList.Count - 1)))
                {
                    throw new ArgumentException("Invalid FLEX message CRC.");
                }

                reader.SetPosition(4);

                var result    = reader.ReadByte();
                var crashTime = reader.ReadUInt32();
                var offset    = reader.ReadUInt32();
                var sizeRead  = reader.ReadUInt16();

                if (state.CrashInfo != null && result == 0x00 && state.CrashInfo.Timestamp == crashTime)
                {
                    for (var i = 0; i < sizeRead; ++i)
                    {
                        state.CrashInfo.Data[offset + i] = reader.ByteList[15 + i];
                    }

                    state.CrashInfo.Offset = offset + sizeRead;

                    var bytesLeft = state.CrashInfo.Data.Length - state.CrashInfo.Offset;

                    writer.Write(CrashInformation.CrashDataQuery);
                    writer.Write(crashTime);
                    writer.Write(state.CrashInfo.Offset);
                    writer.Write(bytesLeft > ushort.MaxValue ? ushort.MaxValue : (ushort)bytesLeft);
                    writer.Write(BinaryUtilities.GetCrc8(writer.Buffer));

                    if (bytesLeft == 0)
                    {
                        if (_onReadyCrash != null)
                        {
                            await _onReadyCrash(state, token).ConfigureAwait(false);
                        }

                        state.CrashInfo = null;
                    }
                }

                return(0);
            }
        }
Exemple #2
0
        /// <summary>
        /// Returns the number of bytes to read from the stream.
        /// </summary>
        /// <param name="state">Instance of <see cref="T:NavtelecomProtocol.SessionState" />.</param>
        /// <param name="reader"><see cref="T:SharpStructures.BinaryListReader" /> linked to a FLEX message body.</param>
        /// <param name="writer"><see cref="T:SharpStructures.MemoryBufferWriter" /> with data to be sent to the client.</param>
        /// <param name="token">The token to monitor for cancellation requests.</param>
        /// <returns>A task that represents the asynchronous operation. Contains the total number of bytes to read from the socket. Zero bytes to stop reading and send the response.</returns>
        public Task <int> GetPendingBytesAsync(SessionState state, BinaryListReader reader, MemoryBufferWriter writer,
                                               CancellationToken token)
        {
            switch (reader.ByteList.Count)
            {
            case 2:
                return(Task.FromResult(12));

            case 14:
                return(Task.FromResult(reader.ByteList[13] + 1));

            default:
                if (reader.ByteList.Last() != BinaryUtilities.GetCrc8(reader.ByteList.Take(reader.ByteList.Count - 1)))
                {
                    throw new ArgumentException("Invalid FLEX message CRC.");
                }

                reader.SetPosition(2);

                reader.ReadByte();
                reader.ReadByte();

                var crashTime   = reader.ReadUInt32();
                var crashLength = reader.ReadUInt32();

                var flags = reader.ReadByte();

                var nameLength = reader.ReadByte();
                var crashName  = new string(reader.ReadBytes(nameLength).Select(x => (char)x).ToArray());

                if (crashTime > 0 && crashLength > 0 && flags == 0xFF)
                {
                    state.CrashInfo = new CrashInformation
                    {
                        Data      = new byte[crashLength],
                        Name      = crashName,
                        Timestamp = crashTime
                    };

                    writer.Write(CrashInformation.CrashDataQuery);
                    writer.Write(crashTime);
                    writer.Write(0U);
                    writer.Write(crashLength > ushort.MaxValue ? ushort.MaxValue : (ushort)crashLength);
                    writer.Write(BinaryUtilities.GetCrc8(writer.Buffer));
                }

                return(Task.FromResult(0));
            }
        }
Exemple #3
0
        /// <summary>
        /// Returns the number of bytes to read from the stream.
        /// </summary>
        /// <param name="state">Instance of <see cref="T:NavtelecomProtocol.SessionState" />.</param>
        /// <param name="reader"><see cref="T:SharpStructures.BinaryListReader" /> linked to a FLEX message body.</param>
        /// <param name="writer"><see cref="T:SharpStructures.MemoryBufferWriter" /> with data to be sent to the client.</param>
        /// <param name="token">The token to monitor for cancellation requests.</param>
        /// <returns>A task that represents the asynchronous operation. Contains the total number of bytes to read from the socket. Zero bytes to stop reading and send the response.</returns>
        public Task <int> GetPendingBytesAsync(SessionState state, BinaryListReader reader, MemoryBufferWriter writer,
                                               CancellationToken token)
        {
            switch (reader.ByteList.Count)
            {
            case 2:
                var messageSize = FlexMessageTable.GetFlexMessageSize(state);

                return(Task.FromResult(messageSize + 5));

            default:
                if (reader.ByteList.Last() != BinaryUtilities.GetCrc8(reader.ByteList.Take(reader.ByteList.Count - 1)))
                {
                    throw new ArgumentException("Invalid FLEX message CRC.");
                }

                reader.SetPosition(2);

                var eventIndex = reader.ReadUInt32();

                writer.Write(Response);
                writer.Write(eventIndex);
                writer.Write(BinaryUtilities.GetCrc8(writer.Buffer));

                return(Task.FromResult(0));
            }
        }
        /// <summary>
        /// Returns the number of bytes to read from the stream.
        /// </summary>
        /// <param name="sessionState">Instance of <see cref="T:NavtelecomProtocol.SessionState" />.</param>
        /// <param name="receiveBuffer">A read-only collection of bytes received from a stream.</param>
        /// <param name="sendBuffer"><see cref="T:SharpStructures.MemoryBuffer" /> with data to be sent to the client.</param>
        /// <param name="token">The token to monitor for cancellation requests.</param>
        /// <returns>A task that represents the asynchronous operation. Contains the total number of bytes to read from the socket. Zero bytes to stop reading and send the response.</returns>
        public Task <int> GetPendingBytesAsync(SessionState sessionState, IReadOnlyList <byte> receiveBuffer, MemoryBuffer sendBuffer, CancellationToken token)
        {
            switch (receiveBuffer.Count)
            {
            case 1:
                return(Task.FromResult(HeaderLength - 1));

            case HeaderLength:
                var reader = new BinaryListReader(receiveBuffer, true);

                if (!reader.ReadBytes(HeaderPreamble.Length).Select(x => (char)x).SequenceEqual(HeaderPreamble))
                {
                    throw new ArgumentException("NTCB header preamble does not match.");
                }

                sessionState.ReceiverId = reader.ReadUInt32();
                sessionState.SenderId   = reader.ReadUInt32();

                var payloadLength = reader.ReadUInt16();

                reader.ReadByte();

                var headerChecksum = reader.ReadByte();

                if (BinaryUtilities.GetXorSum(receiveBuffer.Take(HeaderLength - 1)) != headerChecksum)
                {
                    throw new ArgumentException("NTCB header checksum does not match.");
                }

                return(Task.FromResult((int)payloadLength));

            default:
                if (BinaryUtilities.GetXorSum(receiveBuffer.Skip(HeaderLength)) != receiveBuffer[14])
                {
                    throw new ArgumentException("NTCB body checksum does not match.");
                }

                var bodyProcessor =
                    _bodyProcessors.GetValueOrDefault(receiveBuffer.Skip(HeaderLength).Select(x => (char)x));

                if (bodyProcessor == null)
                {
                    throw new ArgumentException("Unknown NTCB message type.");
                }

                var bodyReader = new BinaryListReader(receiveBuffer, true);

                bodyReader.SetPosition(HeaderLength);

                sendBuffer.AllocateSpace(HeaderLength);

                var writer = new MemoryBufferWriter(sendBuffer, true);

                bodyProcessor.ProcessBody(sessionState, bodyReader, writer);

                var responseLength = sendBuffer.Position - HeaderLength;

                sendBuffer.SetPosition(0);

                writer.Write(HeaderPreamble.Select(x => (byte)x).ToArray());
                writer.Write(sessionState.SenderId);
                writer.Write(sessionState.ReceiverId);
                writer.Write((ushort)responseLength);
                writer.Write(BinaryUtilities.GetXorSum(sendBuffer.Array.Skip(HeaderLength).Take(responseLength)));
                writer.Write(BinaryUtilities.GetXorSum(sendBuffer.Array.Take(HeaderLength - 1)));

                sendBuffer.SetPosition(responseLength + HeaderLength);

                return(Task.FromResult(0));
            }
        }