/// <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> /// 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); } }
private void IncrementSendStatistics(OscBundle bundle, OscCommunicationStatistics stats) { foreach (OscPacket packet in bundle) { if (packet is OscMessage) { stats.MessagesSent.Increment(1); } else if (packet is OscBundle) { stats.BundlesSent.Increment(1); IncrementSendStatistics(packet as OscBundle, stats); } } }
/// <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> /// Read a OscBundle from a array of bytes /// </summary> /// <param name="bytes">the array that contains 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 new static 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.Origin = origin; if (stream.Length < BundleHeaderSizeInBytes) { // this is an error bundle.error = OscPacketError.MissingBundleIdent; bundle.errorMessage = "Missing bundle ident"; bundle.packets = 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.error = OscPacketError.InvalidBundleIdent; bundle.errorMessage = $"Invalid bundle ident '{ident}'"; bundle.packets = new OscPacket[0]; return(bundle); } stream.Position = BundleIdent.Length + 1; bundle.Timestamp = Helper.ReadOscTimeTag(reader); while (stream.Position < stream.Length) { if (stream.Position + 4 > stream.Length) { // this is an error bundle.error = OscPacketError.InvalidBundleMessageHeader; bundle.errorMessage = "Invalid bundle message header"; bundle.packets = new OscPacket[0]; return(bundle); } int messageLength = Helper.ReadInt32(reader); if (stream.Position + messageLength > stream.Length || messageLength < 0 || messageLength % 4 != 0) { // this is an error bundle.error = OscPacketError.InvalidBundleMessageLength; bundle.errorMessage = "Invalid bundle message length"; bundle.packets = new OscPacket[0]; return(bundle); } messages.Add(OscPacket.Read(bytes, index + (int)stream.Position, messageLength, origin, bundle.Timestamp)); stream.Position += messageLength; } bundle.packets = messages.ToArray(); } return(bundle); }