2014-11-11 23:42:53 +01:00
|
|
|
|
//
|
|
|
|
|
|
// Copyright (c) 2014 Morten Nielsen
|
|
|
|
|
|
//
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL) (the "License");
|
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
//
|
|
|
|
|
|
// http://opensource.org/licenses/Ms-PL.html
|
|
|
|
|
|
//
|
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NmeaParser
|
|
|
|
|
|
{
|
2014-11-12 02:36:38 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An abstract generic NMEA device that reads a stream at a decreased pace,
|
|
|
|
|
|
/// mostly used to emulate NMEA input from files and strings.
|
|
|
|
|
|
/// </summary>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public abstract class BufferedStreamDevice : NmeaDevice
|
|
|
|
|
|
{
|
|
|
|
|
|
BufferedStream m_stream;
|
|
|
|
|
|
int m_readSpeed;
|
|
|
|
|
|
/// <summary>
|
2014-11-15 02:36:46 +01:00
|
|
|
|
/// Initializes a new instance of the <see cref="BufferedStreamDevice"/> class.
|
|
|
|
|
|
/// </summary>
|
2015-03-21 00:20:02 +01:00
|
|
|
|
protected BufferedStreamDevice() : this(1000)
|
2014-11-15 02:36:46 +01:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="BufferedStreamDevice"/> class.
|
2014-11-11 23:42:53 +01:00
|
|
|
|
/// </summary>
|
2015-03-21 00:20:02 +01:00
|
|
|
|
/// <param name="readSpeed">The time to wait between each group of lines being read in milliseconds</param>
|
2014-11-15 02:36:46 +01:00
|
|
|
|
protected BufferedStreamDevice(int readSpeed)
|
2014-11-11 23:42:53 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_readSpeed = readSpeed;
|
|
|
|
|
|
}
|
2014-11-12 02:36:38 +01:00
|
|
|
|
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the stream to perform buffer reads on.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2014-11-15 02:36:46 +01:00
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
|
2014-11-11 23:42:53 +01:00
|
|
|
|
protected abstract Task<System.IO.Stream> GetStreamAsync();
|
|
|
|
|
|
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Opens the stream asynchronous.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
protected sealed async override Task<System.IO.Stream> OpenStreamAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var stream = await GetStreamAsync();
|
|
|
|
|
|
StreamReader sr = new StreamReader(stream);
|
|
|
|
|
|
m_stream = new BufferedStream(sr, m_readSpeed);
|
|
|
|
|
|
return m_stream;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Closes the stream asynchronous.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
|
|
/// <returns></returns>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
protected override Task CloseStreamAsync(System.IO.Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_stream.Dispose();
|
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-12 02:36:38 +01:00
|
|
|
|
// stream that slowly populates a buffer from a StreamReader to simulate nmea messages coming
|
2015-03-21 00:20:02 +01:00
|
|
|
|
// in lastLineRead by lastLineRead at a steady stream
|
2014-11-11 23:42:53 +01:00
|
|
|
|
private class BufferedStream : Stream
|
|
|
|
|
|
{
|
2015-03-21 00:20:02 +01:00
|
|
|
|
private StreamReader m_sr;
|
|
|
|
|
|
private byte[] m_buffer = new byte[0];
|
|
|
|
|
|
private System.Threading.Timer m_timer;
|
|
|
|
|
|
private object lockObj = new object();
|
|
|
|
|
|
private string groupToken = null;
|
|
|
|
|
|
private string lastLineRead = null;
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="BufferedStream"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="stream">The stream.</param>
|
2015-03-21 00:20:02 +01:00
|
|
|
|
/// <param name="readSpeed">The read speed in milliseconds.</param>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public BufferedStream(StreamReader stream, int readSpeed)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_sr = stream;
|
2015-03-21 00:20:02 +01:00
|
|
|
|
m_timer = new System.Threading.Timer(OnRead, null, 0, readSpeed); //read a group of lines every 'readSpeed' milliseconds
|
2014-11-11 23:42:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
private void OnRead(object state)
|
|
|
|
|
|
{
|
2015-03-21 00:20:02 +01:00
|
|
|
|
if (lastLineRead != null)
|
|
|
|
|
|
AppendToBuffer(lastLineRead);
|
|
|
|
|
|
//Get the group token if we don't have one
|
|
|
|
|
|
while (groupToken == null && (lastLineRead == null || !lastLineRead.StartsWith("$", StringComparison.Ordinal)))
|
|
|
|
|
|
{
|
|
|
|
|
|
lastLineRead = ReadLine(); //seek forward to first nmea token
|
|
|
|
|
|
AppendToBuffer(lastLineRead);
|
|
|
|
|
|
}
|
|
|
|
|
|
if(groupToken == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var values = lastLineRead.Trim().Split(new char[] { ',' });
|
|
|
|
|
|
if (values.Length > 0)
|
|
|
|
|
|
groupToken = values[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
lastLineRead = ReadLine();
|
|
|
|
|
|
while (!lastLineRead.StartsWith(groupToken, StringComparison.Ordinal)) //keep reading until messages start repeating again
|
|
|
|
|
|
{
|
|
|
|
|
|
AppendToBuffer(lastLineRead);
|
|
|
|
|
|
lastLineRead = ReadLine();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void AppendToBuffer(string line)
|
|
|
|
|
|
{
|
2014-11-11 23:42:53 +01:00
|
|
|
|
var bytes = Encoding.UTF8.GetBytes(line);
|
|
|
|
|
|
lock (lockObj)
|
|
|
|
|
|
{
|
2014-11-15 02:36:46 +01:00
|
|
|
|
byte[] newBuffer = new byte[m_buffer.Length + bytes.Length];
|
|
|
|
|
|
m_buffer.CopyTo(newBuffer, 0);
|
|
|
|
|
|
bytes.CopyTo(newBuffer, m_buffer.Length);
|
|
|
|
|
|
m_buffer = newBuffer;
|
2014-11-11 23:42:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-03-21 00:20:02 +01:00
|
|
|
|
private string ReadLine()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_sr.EndOfStream)
|
|
|
|
|
|
m_sr.BaseStream.Seek(0, SeekOrigin.Begin); //start over
|
|
|
|
|
|
return m_sr.ReadLine() + '\n';
|
|
|
|
|
|
}
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a value indicating whether this instance can read.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>
|
|
|
|
|
|
/// <c>true</c> if this instance can read; otherwise, <c>false</c>.
|
|
|
|
|
|
/// </value>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override bool CanRead { get { return true; } }
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a value indicating whether this instance can seek.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>
|
|
|
|
|
|
/// <c>true</c> if this instance can seek; otherwise, <c>false</c>.
|
|
|
|
|
|
/// </value>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override bool CanSeek { get { return false; } }
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a value indicating whether this instance can write.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>
|
|
|
|
|
|
/// <c>true</c> if this instance can write; otherwise, <c>false</c>.
|
|
|
|
|
|
/// </value>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override bool CanWrite { get { return false; } }
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Flushes this instance.
|
|
|
|
|
|
/// </summary>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override void Flush() { }
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the length.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>
|
|
|
|
|
|
/// The length.
|
|
|
|
|
|
/// </value>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override long Length { get { return m_sr.BaseStream.Length; } }
|
|
|
|
|
|
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the position.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>
|
|
|
|
|
|
/// The position.
|
|
|
|
|
|
/// </value>
|
|
|
|
|
|
/// <exception cref="System.NotSupportedException"></exception>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override long Position
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_sr.BaseStream.Position; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Reads the specified buffer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="buffer">The buffer.</param>
|
|
|
|
|
|
/// <param name="offset">The offset.</param>
|
|
|
|
|
|
/// <param name="count">The count.</param>
|
|
|
|
|
|
/// <returns></returns>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (lockObj)
|
|
|
|
|
|
{
|
2014-11-15 02:36:46 +01:00
|
|
|
|
if (this.m_buffer.Length <= count)
|
2014-11-11 23:42:53 +01:00
|
|
|
|
{
|
2014-11-15 02:36:46 +01:00
|
|
|
|
int length = this.m_buffer.Length;
|
|
|
|
|
|
this.m_buffer.CopyTo(buffer, 0);
|
|
|
|
|
|
this.m_buffer = new byte[0];
|
2014-11-11 23:42:53 +01:00
|
|
|
|
return length;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-11-15 02:36:46 +01:00
|
|
|
|
Array.Copy(this.m_buffer, buffer, count);
|
|
|
|
|
|
byte[] newBuffer = new byte[this.m_buffer.Length - count];
|
|
|
|
|
|
Array.Copy(this.m_buffer, count, newBuffer, 0, newBuffer.Length);
|
|
|
|
|
|
this.m_buffer = newBuffer;
|
2014-11-11 23:42:53 +01:00
|
|
|
|
return count;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Seeks the specified offset.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="offset">The offset.</param>
|
|
|
|
|
|
/// <param name="origin">The origin.</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="System.NotSupportedException"></exception>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the length.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">The value.</param>
|
|
|
|
|
|
/// <exception cref="System.NotSupportedException"></exception>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override void SetLength(long value)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// <summary>
|
2014-11-15 02:36:46 +01:00
|
|
|
|
/// Writes the specified buffer to the device.
|
2014-11-12 02:44:02 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="buffer">The buffer.</param>
|
|
|
|
|
|
/// <param name="offset">The offset.</param>
|
|
|
|
|
|
/// <param name="count">The count.</param>
|
|
|
|
|
|
/// <exception cref="System.NotSupportedException"></exception>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
2014-11-12 02:44:02 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
2014-11-11 23:42:53 +01:00
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
|
m_sr.Dispose();
|
2014-11-15 02:36:46 +01:00
|
|
|
|
m_timer.Dispose();
|
2014-11-11 23:42:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|