Exemplo n.º 1
0
        public static OscBundle Read(OscReader reader, int count, Uri origin = null)
        {
            OscBundle bundle = new OscBundle
            {
                Origin = origin
            };

            int start     = reader.Position;
            int bundleEnd = start + count;

            reader.BeginBundle(count);

            string ident = reader.ReadAddress();

            if (BundleIdent.Equals(ident, StringComparison.Ordinal) == false)
            {
                // this is an error
                throw new OscException(OscError.InvalidBundleIdent, $"Invalid bundle ident '{ident}'");
            }

            bundle.Timestamp = reader.ReadBundleTimeTag();

            List <OscPacket> messages = new List <OscPacket>();

            while (reader.Position < bundleEnd)
            {
                if (reader.Position + 4 > bundleEnd)
                {
                    // this is an error
                    throw new OscException(OscError.InvalidBundleMessageHeader, "Invalid bundle message header");
                }

                int messageLength = reader.ReadBundleMessageLength(start, count);

                if (reader.Position + messageLength > bundleEnd ||
                    messageLength < 0 ||
                    messageLength % 4 != 0)
                {
                    // this is an error
                    throw new OscException(OscError.InvalidBundleMessageLength, "Invalid bundle message length");
                }

                messages.Add(Read(reader, messageLength, origin, bundle.Timestamp));
            }

            bundle.packets = messages.ToArray();

            return(bundle);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Read the osc packet from a byte array
        /// </summary>
        /// <param name="bytes">array to read from</param>
        /// <param name="index">the offset within the array where reading should begin</param>
        /// <param name="count">the number of bytes in the packet</param>
        /// <param name="origin">the origin that is the origin of this packet</param>
        /// <param name="timeTag">the time tag asociated with the parent</param>
        /// <returns>the packet</returns>
        public static OscPacket Read(
            byte[] bytes,
            int index,
            int count,
            Uri origin         = null,
            OscTimeTag?timeTag = null)
        {
            //if (OscBundle.IsBundle(bytes, index, count) == true)
            if (bytes[index] == (byte)'#')
            {
                return(OscBundle.Read(bytes, index, count, origin));
            }

            return(OscMessage.Read(bytes, index, count, origin, timeTag));
        }
Exemplo n.º 3
0
        /// <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);
            }
        }
Exemplo n.º 4
0
        /// <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);
            }
        }