/* SocketOutPC - Send XYZ Point Cloud data out via UDP V1.01 - Lorne - Changed to Autoevaluate V1.02 - Lorne - Added SendTo return value check, returns total bytes sent */ #region usings using System; using System.ComponentModel.Composition; using System.Net; using System.Net.Sockets; using System.Text; using VVVV.PluginInterfaces.V1; using VVVV.PluginInterfaces.V2; using VVVV.Utils.VColor; using VVVV.Utils.VMath; using VVVV.Core.Logging; #endregion usings namespace VVVV.Nodes { #region PluginInfo [PluginInfo( Name = "SocketOutPC", Category = "PC", Help = "Send Point Cloud via UDP", Author = "Lorne", Tags = "", AutoEvaluate = true)] #endregion PluginInfo public class PCSocketOutPCNode : IPluginEvaluate { #region fields & pins [Input("Host", IsSingle = true)] IDiffSpread FHost; [Input("Port", IsSingle = true, DefaultValue = 3141)] IDiffSpread FPort; [Input("Input")] ISpread FInput; [Input("TimeStamp", IsSingle = true)] ISpread FTime; [Input("Send", IsBang = true, IsSingle = true)] ISpread FSend; [Input("Enable", IsSingle = true)] ISpread FEnable; [Output("HostOK")] ISpread FHostOK; [Output("Sent")] ISpread FOutput; [Output("Bytes Sent")] ISpread FBytesSent; [Output("Frame")] ISpread FFrame; [Import()] ILogger FLogger; #endregion fields & pins bool HostWasBad = false; UInt32 framecnt = 0; private int SocketSend(Socket sending_socket, IPEndPoint sending_end_point) { int iResult; int bytessent = 0; const int MAXV3D = 240; // gives < 1500 byte packet size const int MAXDOUBLES = MAXV3D * 3; UInt32 cnt; bool exception_thrown = false; Int32 seqcnt = 0; framecnt++; Int32 timestamp = (Int32) FTime[0]; int place = 0; Int16 tempMM; for ( int i = 0; i < FInput.SliceCount / MAXDOUBLES + 1; i++ ) { int basecnt = i * MAXDOUBLES; int lastseq = (i == FInput.SliceCount / MAXDOUBLES ? -1 : 1); cnt = (uint)Math.Min((FInput.SliceCount - basecnt), MAXDOUBLES); //FLogger.Log(LogType.Debug, "basecnt = {0} cnt = {1}",basecnt,cnt); UInt32 datasize = 4 * 4 + cnt * 2; byte[] data = new byte[datasize]; seqcnt++; place = 0; Buffer.BlockCopy(BitConverter.GetBytes(framecnt), 0, data, place, 4); place += 4; Buffer.BlockCopy(BitConverter.GetBytes(timestamp), 0, data, place, 4); place += 4; Buffer.BlockCopy(BitConverter.GetBytes(seqcnt * lastseq), 0, data, place, 4); place += 4; Buffer.BlockCopy(BitConverter.GetBytes(cnt), 0, data, place, 4); place += 4; for ( int j = 0; j < cnt; j++ ) { tempMM = (Int16)(FInput[basecnt+j] * 1000); // Meters to MM try { Buffer.BlockCopy(BitConverter.GetBytes(tempMM), 0, data, place, 2); place += 2; } catch { FLogger.Log(LogType.Debug, "SocketOutPC: Out of bounds: j = {0} place = {1}", j, place); } } try { iResult = sending_socket.SendTo(data, sending_end_point); if ( iResult < 0 ) { FLogger.Log(LogType.Debug, "SocketOutPC: SendTo returned error {0}",iResult); return(iResult); } bytessent += iResult; } catch ( Exception send_exception ) { exception_thrown = true; FLogger.Log(LogType.Debug, "SocketOutPC: Exception sending data: {0}", send_exception.Message); break; } } if ( exception_thrown == false ) { //FLogger.Log(LogType.Debug, "SocketOutPC: Message has been sent to the broadcast address"); //bytessent = 0; } return(bytessent); } Socket sending_socket; IPHostEntry ipHostInfo; IPAddress send_to_address; IPEndPoint sending_end_point; bool socket_open = false; //called when data for any output pin is requested public void Evaluate(int SpreadMax) { FOutput.SliceCount = 1; FOutput[0] = false; FBytesSent.SliceCount = 1; FBytesSent[0] = 0; FHostOK[0] = false; if ( ! FEnable[0] ) { if ( socket_open ) { sending_socket.Close(); socket_open = false; return; } HostWasBad = false; return; } if ( HostWasBad && !FHost.IsChanged ) { return; } if ( !socket_open || FHost.IsChanged || FPort.IsChanged ) { sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); try { ipHostInfo = Dns.Resolve(FHost[0]); /*Dns.GetHostEntry(FHost[0]); <- DOES NOT WORK*/ } catch ( Exception e ) { FLogger.Log(LogType.Debug, "SocketOutPC: Exception resolving '{0}': {1}", FHost[0], e.Message); HostWasBad = true; return; } send_to_address = ipHostInfo.AddressList[0]; //FLogger.Log(LogType.Debug, "SocketOutPC: Sending to {0} port {1}", send_to_address, FPort[0]); sending_end_point = new IPEndPoint(send_to_address, FPort[0]); FLogger.Log(LogType.Debug, "SocketOutPC: Sending to {0} - {1}", FHost[0], sending_end_point); HostWasBad = false; FHostOK[0] = true; socket_open = true; framecnt = 0; } if ( FSend.SliceCount > 0 && FSend[0] ) { FBytesSent[0] = SocketSend(sending_socket, sending_end_point); FOutput[0] = FBytesSent[0] > 0; } FHostOK[0] = ! HostWasBad; FFrame.SliceCount = 1; FFrame[0] = (int)framecnt; } } }