/// <summary> /// Try to parse a bundle from a string using a supplied format provider /// </summary> /// <param name="str">the bundle as a string</param> /// <param name="provider">the format provider to use</param> /// <param name="bundle">the parsed bundle</param> /// <returns>true if the bundle could be parsed else false</returns> public static bool TryParse(string str, IFormatProvider provider, out OscBundle bundle) { try { bundle = Parse(str, provider); return(true); } catch { bundle = null; return(false); } }
/// <summary> /// Try to parse a bundle from a string using the InvariantCulture /// </summary> /// <param name="str">the bundle as a string</param> /// <param name="bundle">the parsed bundle</param> /// <returns>true if the bundle could be parsed else false</returns> public static bool TryParse(string str, out OscBundle bundle) { try { bundle = Parse(str, CultureInfo.InvariantCulture); return(true); } catch { bundle = null; return(false); } }
/// <summary> /// Invoke all the messages within a bundle /// </summary> /// <param name="bundle">an osc bundle of messages</param> /// <returns>true if there was a listener to invoke for any message in the otherwise false</returns> public bool Invoke(OscBundle bundle) { bool result = false; foreach (OscMessage message in bundle) { if (message.Error != OscPacketError.None) { continue; } result |= Invoke(message); } return(result); }
/// <summary> /// Are 2 bundles equivalent /// </summary> /// <param name="bundle1">A bundle</param> /// <param name="bundle2">A bundle</param> /// <returns>true if the objects are equivalent</returns> private bool BundlesAreEqual(OscBundle bundle1, OscBundle bundle2) { // ensure the error codes are the same if (bundle1.Error != bundle2.Error) { return(false); } // ensure the error messages are the same if (bundle1.ErrorMessage != bundle2.ErrorMessage) { return(false); } // ensure the timestamps are the same if (bundle1.Timestamp.Value != bundle2.Timestamp.Value) { return(false); } // ensure the packet arrays are equivalent return(PacketArraysAreEqual(bundle1.ToArray(), bundle2.ToArray())); }
/// <summary> /// Determin if the packet should be invoked /// </summary> /// <param name="packet">A packet</param> /// <returns>The appropriate action that should be taken with the packet</returns> public OscPacketInvokeAction ShouldInvoke(OscPacket packet) { if (packet.Error != OscPacketError.None) { return(OscPacketInvokeAction.HasError); } if (packet is OscMessage) { return(OscPacketInvokeAction.Invoke); } if (packet is OscBundle) { OscBundle bundle = packet as OscBundle; if (BundleInvokeMode == OscBundleInvokeMode.NeverInvoke) { return(OscPacketInvokeAction.DontInvoke); } else if (BundleInvokeMode != OscBundleInvokeMode.InvokeAllBundlesImmediately) { double delay; IOscTimeProvider provider = TimeProvider; if (TimeProvider == null) { provider = DefaultTimeProvider.Instance; } delay = provider.DifferenceInSeconds(bundle.Timestamp); if ((BundleInvokeMode & OscBundleInvokeMode.InvokeEarlyBundlesImmediately) != OscBundleInvokeMode.InvokeEarlyBundlesImmediately) { if (delay > 0 && provider.IsWithinTimeFrame(bundle.Timestamp) == false) { if ((BundleInvokeMode & OscBundleInvokeMode.PosponeEarlyBundles) != OscBundleInvokeMode.PosponeEarlyBundles) { return(OscPacketInvokeAction.Pospone); } else { return(OscPacketInvokeAction.DontInvoke); } } } if ((BundleInvokeMode & OscBundleInvokeMode.InvokeLateBundlesImmediately) != OscBundleInvokeMode.InvokeLateBundlesImmediately) { if (delay < 0 && provider.IsWithinTimeFrame(bundle.Timestamp) == false) { return(OscPacketInvokeAction.DontInvoke); } } if ((BundleInvokeMode & OscBundleInvokeMode.InvokeOnTimeBundles) != OscBundleInvokeMode.InvokeOnTimeBundles) { if (provider.IsWithinTimeFrame(bundle.Timestamp) == true) { return(OscPacketInvokeAction.DontInvoke); } } } return(OscPacketInvokeAction.Invoke); } else { return(OscPacketInvokeAction.DontInvoke); } }
/// <summary> /// Read a OscBundle from a array of bytes /// </summary> /// <param name="bytes">the array that countains the bundle</param> /// <param name="index">the offset within the array where reading should begin</param> /// <param name="count">the number of bytes in the bundle</param> /// <param name="origin">the origin that is the origin of this bundle</param> /// <returns>the bundle</returns> public static new OscBundle Read(byte[] bytes, int index, int count, IPEndPoint origin) { OscBundle bundle = new OscBundle(); List <OscPacket> messages = new List <OscPacket>(); using (MemoryStream stream = new MemoryStream(bytes, index, count)) using (BinaryReader reader = new BinaryReader(stream)) { bundle.m_Origin = origin; if (stream.Length < BundleHeaderSizeInBytes) { // this is an error bundle.m_Error = OscPacketError.MissingBundleIdent; bundle.m_ErrorMessage = Strings.Bundle_MissingIdent; bundle.m_Messages = new OscPacket[0]; return(bundle); } string ident = Encoding.UTF8.GetString(bytes, index, BundleIdent.Length); if (BundleIdent.Equals(ident, System.StringComparison.InvariantCulture) == false) { // this is an error bundle.m_Error = OscPacketError.InvalidBundleIdent; bundle.m_ErrorMessage = String.Format(Strings.Bundle_InvalidIdent, ident); bundle.m_Messages = new OscPacket[0]; return(bundle); } stream.Position = BundleIdent.Length + 1; bundle.m_Timestamp = Helper.ReadOscTimeTag(reader); while (stream.Position < stream.Length) { if (stream.Position + 4 > stream.Length) { // this is an error bundle.m_Error = OscPacketError.InvalidBundleMessageHeader; bundle.m_ErrorMessage = Strings.Bundle_InvalidMessageHeader; bundle.m_Messages = new OscPacket[0]; return(bundle); } int messageLength = Helper.ReadInt32(reader); if (stream.Position + messageLength > stream.Length) { // this is an error bundle.m_Error = OscPacketError.InvalidBundleMessageLength; bundle.m_ErrorMessage = string.Format(Strings.Bundle_InvalidBundleMessageLength); bundle.m_Messages = new OscPacket[0]; return(bundle); } messages.Add(OscPacket.Read(bytes, index + (int)stream.Position, messageLength, origin)); stream.Position += messageLength; } bundle.m_Messages = messages.ToArray(); } return(bundle); }