Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Robert Hugo

Member Since 11 May 2014
Offline Last Active Sep 29 2015 12:27 AM
-----

Topics I've Started

Micro.Udp.Cast

25 May 2015 - 02:29 AM

Here is a Micro.NET class file (Micro.Udp.Cast.cs) that uses Multicast functionality. I seem to be having problems with the receiving buffer. (From tablet TO N3:) If I send 5 characters initially, I get 0 received, if I then send 15, I get 5, and if I then send 25, I get 15, and so on. It's like the buffer size is (re)set from the previous received size.

 

Anyways, this works on the N3 and N2+.

namespace Micro.Udp
{
	public class Cast
	{
		#region "Event Handling"
		public class DataReceivedEventArgs
		{
			public byte[] Bytes { get; private set; }
			public System.Net.EndPoint Remote { get; private set; }

			public DataReceivedEventArgs(byte[] bytes, System.Net.EndPoint remote)
			{
				Bytes = bytes;
				Remote = remote;
			}
		}

		private DataReceivedEventHandler DataReceivedExternal;

		private System.Threading.Thread DataReceivedThread;

		public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);

		protected virtual void RaiseDataReceivedEvent(DataReceivedEventArgs e)
		{
			DataReceivedEventHandler eventHandler = DataReceivedExternal;

			if (eventHandler != null)
				eventHandler(this, e);
		}
		#endregion

		public System.Net.IPEndPoint Remote { get; private set; }

		public System.Net.Sockets.Socket Socket { get; private set; }

		public Cast(System.Net.IPEndPoint remote, DataReceivedEventHandler dataReceivedEventHandler)
		{
			Remote = remote;

			Socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
			Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.DontFragment, true);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.IpTimeToLive, 127);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReuseAddress, true);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveBuffer, 1024);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendBuffer, 1024);
			Socket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, Remote.Port));
			if (new System.Text.RegularExpressions.Regex(@"2(?:2[4-9]|3\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?|0)){3}").IsMatch(Remote.Address.ToString()))
			{
				//byte[] multicastOpt = new byte[] { 224, 5, 6, 7, 0, 0, 0, 0 };
				byte[] multicastOpt = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
				System.Array.Copy(Remote.Address.GetAddressBytes(), multicastOpt, 0);
				System.Array.Copy(Remote.Address.GetAddressBytes(), multicastOpt, 4);
				Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.MulticastTimeToLive, 127);
				Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.AddMembership, multicastOpt);
			}

			DataReceivedExternal = dataReceivedEventHandler;

			DataReceivedThread = new System.Threading.Thread(DataReceivedInternal);
			DataReceivedThread.Start();
		}

		private void DataReceivedInternal()
		{
			System.Net.EndPoint remoteEP = (System.Net.EndPoint)new System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), Remote.Port);

			while (true)
			{
				if (Socket.Available > 0)
				{
					byte[] buffer = new byte[Socket.Available];

					Socket.ReceiveFrom(buffer, ref remoteEP);

					RaiseDataReceivedEvent(new DataReceivedEventArgs(buffer, remoteEP));
				}
			}
		}

		public void SendBytes(byte[] bytes)
		{
			Socket.SendTo(bytes, Remote);
		}
		public void SendString(string text)
		{
			SendBytes(System.Text.Encoding.UTF8.GetBytes("Netduino: " + text));
		}
	}
}

Recommended HTTP API for the future?

19 May 2015 - 04:31 AM

What is the official API to use for HTTP communication "from now on"? I now in the past certain libraries have been the focus, but with Web API in ASP.NET and things being pushed to various likings, I'd like to know what is recommended for the future of IoT and Netduino (3 and 2+, in my case).

 

I hope my question make sense.

 

Thank you,

Rob

 


home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.