SharpGPS /SharpGps/SharpGps/NTRIP/NTRIP.cs

Language C# Lines 256
MD5 Hash 132156d94d546ed52206af6cd4906288 Estimated Cost $3,712 (why?)
Repository https://SharpGPS.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2007 - Morten Nielsen
//
// This file is part of SharpGps.
// SharpGps is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// SharpGps is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with SharpGps; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SharpGis.SharpGps.NTRIP
{
	/// <summary>
	/// Networked Transport of RTCM via Internet Protocol (Ntrip) is an application-level protocol that
	/// supports streaming Global Navigation Satellite System (GNSS) data over the Internet. Ntrip is a
	/// generic, stateless protocol based on the Hypertext Transfer Protocol HTTP/1.1. The HTTP
	/// objects are extended to GNSS data streams.
	/// </summary>
	/// <remarks>
	/// <para>Ntrip is designed to disseminate differential correction data or other kinds of GNSS streaming
	/// data to stationary or mobile users over the Internet, allowing simultaneous PC, Laptop, PDA, or
	/// receiver connections to a broadcasting host. Ntrip supports wireless Internet access through
	/// Mobile IP Networks like GSM, GPRS, EDGE, or UMTS.</para>
	/// <para>Ntrip is meant to be an open non-proprietary protocol. Major characteristics of Ntrip’s
	/// dissemination technique are the following:</para>
	/// <para>• It is based on the popular HTTP streaming standard; it is comparatively easy to implement
	/// when limited client and server platform resources are available.<br/>
	/// • Its application is not limited to one particular plain or coded stream content; it has the ability
	/// to distribute any kind of GNSS data.<br/>
	/// • It has the potential to support mass usage; it can disseminate hundreds of streams
	/// simultaneously for up to a thousand users when applying modified Internet Radio
	/// broadcasting software.<br/>
	/// • Regarding security needs, stream providers and users are not necessarily in direct contact,
	/// and streams are usually not blocked by firewalls or proxy servers protecting Local Area
	/// Networks.<br/>
	/// • It enables streaming over any mobile IP network because it uses TCP/IP.</para>
	/// </remarks>
	
	public class NTRIPClient : IDisposable
	{
		private Socket sckt;
		private string _username;
		private string _password;
		private IPEndPoint _broadcaster;
		byte[] rtcmDataBuffer = new byte[128];
		//IAsyncResult m_asynResult;
		public AsyncCallback pfnCallBack;

		/// <summary>
		/// NTRIP server Username
		/// </summary>
		public string UserName
		{
			get { return _username; }
			set { _username = value; }
		}

		/// <summary>
		/// NTRIP server password
		/// </summary>
		public string Password
		{
			get { return _password; }
			set { _password = value; }
		}

		/// <summary>
		/// NTRIP Server
		/// </summary>
		public IPEndPoint BroadCaster
		{
			get { return _broadcaster; }
			set { _broadcaster = value; }
		}

		private GPSHandler gps;

		public NTRIPClient(IPEndPoint Server, GPSHandler gpsHandler)
		{
			//Initialization...
			gps = gpsHandler;
			BroadCaster = Server;
			//InitializeSocket();
		}

		public NTRIPClient(IPEndPoint Server, string strUserName, string strPassword, GPSHandler gpsHandler) : this(Server, gpsHandler)
		{
			_username = strUserName;
			_password = strPassword;
			//InitializeSocket();
		}

		private void InitializeSocket()
		{
			sckt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		}

		

		/// <summary>
		/// Creates request to NTRIP server
		/// </summary>
		/// <param name="strRequest">Resource to request. Leave blank to get NTRIP service data</param>
		private byte[] CreateRequest(string strRequest)
		{
			//Build request message
			string msg = "GET /" + strRequest + " HTTP/1.1\r\n";
			msg += "User-Agent: SharpGps iter.dk\r\n";
			//If password/username is specified, send login details
			if (_username != null && _username != "")
			{
				string auth = ToBase64(_username + ":" + _password);
				msg += "Authorization: Basic " + auth + "\r\n";
			}
			msg += "Accept: */*\r\nConnection: close\r\n";
			msg += "\r\n";

			return System.Text.Encoding.ASCII.GetBytes(msg);	
		}

		private void Connect()
		{
			//Connect to server	   
			if (!sckt.Connected)
				sckt.Connect(BroadCaster);
		}

		private void Close()
		{
			sckt.Shutdown(SocketShutdown.Both);
			sckt.Close();
		}

		public SourceTable GetSourceTable()
		{
			this.InitializeSocket();
			sckt.Blocking = true;
			this.Connect();
			sckt.Send(CreateRequest(""));
			string responseData = "";
			System.Threading.Thread.Sleep(1000); //Wait for response
			while (sckt.Available>0)
			{
				byte[] returndata = new byte[sckt.Available];
				sckt.Receive(returndata); //Get response
				responseData += System.Text.Encoding.ASCII.GetString(returndata, 0, returndata.Length);
				System.Threading.Thread.Sleep(100); //Wait for response
			}
			this.Close();

			if (!responseData.StartsWith("SOURCETABLE 200 OK"))
				return null;
			
			SourceTable result = new SourceTable();
			foreach (string line in responseData.Split('\n'))
			{
				if(line.StartsWith("STR"))
					result.DataStreams.Add(NTRIP.SourceTable.NTRIPDataStream.ParseFromString(line));
				else if (line.StartsWith("CAS"))
					result.Casters.Add(NTRIP.SourceTable.NTRIPCaster.ParseFromString(line));
				else if (line.StartsWith("NET"))
					result.Networks.Add(NTRIP.SourceTable.NTRIPNetwork.ParseFromString(line));
			}
			return result;
		}

		/// <summary>
		/// Opens the connection to the NTRIP server and starts receiving
		/// </summary>
		/// <param name="MountPoint">ID of Stream</param>
		public void StartNTRIP(string MountPoint)
		{
			this.InitializeSocket();
			sckt.Blocking = true;
			this.Connect();
			sckt.Send(CreateRequest(MountPoint));
			sckt.Blocking = false;
			WaitForData(sckt);
		}

		public void WaitForData(Socket sock)
		{
			//if (pfnCallBack == null)
			//	pfnCallBack = new AsyncCallback(OnDataReceived);
			AsyncCallback recieveData = new AsyncCallback(OnDataReceived);
			sock.BeginReceive(rtcmDataBuffer, 0, rtcmDataBuffer.Length, SocketFlags.None,
				recieveData, sock);
			//m_asynResult = sckt.BeginReceive(rtcmDataBuffer, 0, rtcmDataBuffer.Length, SocketFlags.None, pfnCallBack, null);
		}

		private void OnDataReceived(IAsyncResult ar)
		{
			Socket sock = (Socket)ar.AsyncState;
			int iRx = sock.EndReceive(ar);
			if (iRx > 0) //if we received at least one byte
			{
				try
				{
					if (gps.GpsPort.IsPortOpen)
						//Send RTCM data to GPS. We assume the data is valid RTCM
						gps.GpsPort.Write(rtcmDataBuffer);
				}
				catch (System.Exception ex)
				{
					this.Close();
					throw (new System.Exception("Error sending RTCM data to device:" + ex.Message));
				}
			}
			WaitForData(sock);
		}

		/// <summary>
		/// Stops receiving data from the NTRIP server
		/// </summary>
		private void StopNTRIP()
		{
			this.Close();
		}

		/// <summary>
		/// Apply AsciiEncoding to user name and password to obtain it as an array of bytes
		/// </summary>
		/// <param name="str">username:password</param>
		/// <returns>Base64 encoded username/password</returns>
		private string ToBase64(string str)
		{
			Encoding asciiEncoding = Encoding.ASCII;
			byte[] byteArray = new byte[asciiEncoding.GetByteCount(str)];
			byteArray = asciiEncoding.GetBytes(str);
			return Convert.ToBase64String(byteArray, 0, byteArray.Length);
		}

		#region IDisposable Members

		public void Dispose()
		{
			this.Close();
		}

		#endregion
}
}
Back to Top