Example #1
0
        /// <summary>
        ///     Create a bundle of messages
        /// </summary>
        /// <param name="origin">the origin of the osc bundle</param>
        /// <param name="timestamp">timestamp</param>
        /// <param name="messages">messages to bundle</param>
        public OscBundle(Uri origin, OscTimeTag timestamp, params OscPacket[] messages)
        {
            Origin = origin;

            Timestamp = timestamp;
            packets   = messages;
        }
Example #2
0
        /// <summary>
        ///     Create a bundle of messages
        /// </summary>
        /// <param name="origin">the origin of the osc bundle</param>
        /// <param name="timestamp">timestamp</param>
        /// <param name="messages">messages to bundle</param>
        public OscBundle(Uri origin, DateTime timestamp, params OscPacket[] messages)
        {
            Origin = origin;

            Timestamp = OscTimeTag.FromDataTime(timestamp);
            packets   = messages;
        }
Example #3
0
        /// <summary>
        ///     Create a bundle of messages
        /// </summary>
        /// <param name="timestamp">timestamp</param>
        /// <param name="messages">messages to bundle</param>
        public OscBundle(OscTimeTag timestamp, params OscPacket[] messages)
        {
            Origin = null;

            Timestamp = timestamp;
            packets   = messages;
        }
Example #4
0
        public new static OscBundle Parse(ref OscStringReader reader, IFormatProvider provider = null, OscSerializationToken endToken = OscSerializationToken.End)
        {
            if (provider == null)
            {
                provider = CultureInfo.InvariantCulture;
            }

            string ident = reader.ReadAddress(false)
                           .Trim();

            if (BundleIdent.Equals(ident, StringComparison.Ordinal) == false)
            {
                throw new Exception($"Invalid bundle ident '{ident}'");
            }

            reader.ReadSeparator();

            if (reader.ReadNextToken(out string timeStampStr) != OscSerializationToken.Literal)
            {
                throw new Exception("Invalid bundle timestamp");
            }

            OscTimeTag timeStamp = OscTimeTag.Parse(timeStampStr.Trim(), provider);

            if (reader.ReadSeparatorOrEnd() == endToken)
            {
                return(new OscBundle(timeStamp));
            }

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

            OscSerializationToken token = OscSerializationToken.None;

            token = reader.ReadNextToken(out string _);

            while (token != endToken && token != OscSerializationToken.End)
            {
                if (token != OscSerializationToken.ObjectStart)
                {
                    throw new Exception("Invalid bundle token");
                }

                packets.Add(OscPacket.Parse(ref reader, provider, OscSerializationToken.ObjectEnd));

                token = reader.ReadNextToken(out string _);

                if (token == OscSerializationToken.Separator)
                {
                    token = reader.ReadNextToken(out string _);
                }
            }

            if (token != endToken)
            {
                throw new OscException(OscError.UnexpectedToken, $"Unexpected token {token}");
            }

            return(new OscBundle(timeStamp, packets.ToArray()));
        }
Example #5
0
        private void ReadMessages(
            ArraySegment <byte> buffer,
            OscReader reader,
            int count,
            List <OscMessageRaw> messages,
            out OscTimeTag timestamp)
        {
            int start     = reader.Position;
            int bundleEnd = start + count;

            reader.BeginBundle(count);

            string ident = reader.ReadAddress();

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

            timestamp = reader.ReadBundleTimeTag();

            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");
                }

                if (reader.PeekByte() == (byte)'#')
                {
                    ReadMessages(buffer, reader, messageLength, messages, out OscTimeTag _);
                }
                else
                {
                    messages.Add(new OscMessageRaw(new ArraySegment <byte>(buffer.Array, buffer.Offset + reader.Position, messageLength), Origin, timestamp));

                    reader.Position += messageLength;
                }
            }
        }
Example #6
0
        public override void Write(OscWriter writer)
        {
            OscTimeTag timestamp = Timestamp;

            writer.StartBundle(BundleIdent, ref timestamp);

            foreach (OscPacket message in this)
            {
                writer.WriteBundleMessageLength(message.SizeInBytes);

                message.Write(writer);
            }
        }
Example #7
0
        public OscBundleRaw(ArraySegment <byte> buffer, Uri origin = null)
        {
            Origin = origin;

            OscReader reader = new OscReader(buffer);

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

            ReadMessages(buffer, reader, buffer.Count, messages, out OscTimeTag timestamp);

            Timestamp = timestamp;

            this.messages = messages.ToArray();
        }
Example #8
0
        /// <summary>
        ///     Try to parse a OSC time tag from date-time string.
        /// </summary>
        /// <param name="str">String to parse.</param>
        /// <param name="value">The parsed time tag.</param>
        /// <returns>True if parsed else false.</returns>
        public static bool TryParse(string str, out OscTimeTag value)
        {
            try
            {
                value = Parse(str, CultureInfo.InvariantCulture);

                return(true);
            }
            catch
            {
                value = default(OscTimeTag);

                return(false);
            }
        }
Example #9
0
        /// <summary>
        ///     Try to parse a OSC time tag from date-time string.
        /// </summary>
        /// <param name="str">String to parse.</param>
        /// <param name="provider">Format provider.</param>
        /// <param name="value">The parsed time tag.</param>
        /// <returns>True if parsed else false.</returns>
        public static bool TryParse(string str, IFormatProvider provider, out OscTimeTag value)
        {
            try
            {
                value = Parse(str, provider);

                return(true);
            }
            catch
            {
                value = default(OscTimeTag);

                return(false);
            }
        }