using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using NmeaParser;
using NmeaParser.Messages;

namespace skyscraper5.NmeaSharpWrapper
{
	internal class NmeaFromUdp : NmeaSharpWrapper
	{
		public NmeaFromUdp(string ipAdress, int port)
		{
			this.ipAdress = ipAdress;
			this.port = port;
		}

		private string ipAdress;
		private int port;

		protected override void ProcessorThread()
		{
			UdpClient client = new UdpClient();
			client.ExclusiveAddressUse = false;
			client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
			client.Client.Bind(new IPEndPoint(IPAddress.Parse(this.ipAdress),this.port));

			CancelRequest = false;
			IPEndPoint endPoint = null;
			while (!CancelRequest)
			{
				byte[] receive = client.Receive(ref endPoint);
				string s = Encoding.ASCII.GetString(receive);
				HandleNmeaMessage(s);
			}
			client.Close();
		}
		
	}
}
