Esempio n. 1
0
        public new static OscMessage Read(
            OscReader reader,
            int count,
            Uri origin         = null,
            OscTimeTag?timeTag = null)
        {
            reader.BeginMessage(count);

            OscMessage msg = new OscMessage
            {
                Origin    = origin,
                Timestamp = timeTag
            };

            msg.Address = reader.ReadAddress();

            if (reader.PeekToken() == OscToken.End)
            {
                msg.arguments = new object[0];

                return(msg);
            }

            OscTypeTag typeTag = reader.ReadTypeTag();

            msg.arguments = new object[reader.GetArgumentCount(ref typeTag, out OscToken argumentsType)];
Esempio n. 2
0
        /// <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 static OscBundle Read(
            byte[] bytes,
            int index,
            int count,
            Uri origin = null)
        {
            ArraySegment <byte> arraySegment = new ArraySegment <byte>(bytes, index, count);

            OscReader reader = new OscReader(arraySegment);

            return(Read(reader, count, origin));
        }
Esempio n. 3
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;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        ///     Read a OscMessage from a array of bytes
        /// </summary>
        /// <param name="bytes">the array that contains the message</param>
        /// <param name="index">the offset within the array where reading should begin</param>
        /// <param name="count">the number of bytes in the message</param>
        /// <param name="origin">the origin of the packet</param>
        /// <param name="timeTag">time-tag of parent bundle</param>
        /// <exception cref="OscException"></exception>
        /// <returns>the parsed OSC message or an empty message if their was an error while parsing</returns>
        public new static OscMessage Read(
            byte[] bytes,
            int index,
            int count,
            Uri origin         = null,
            OscTimeTag?timeTag = null)
        {
            ArraySegment <byte> arraySegment = new ArraySegment <byte>(bytes, index, count);

            OscReader reader = new OscReader(arraySegment);

            return(Read(reader, count, origin, timeTag));
        }
Esempio n. 5
0
        public static OscPacket Read(
            OscReader reader,
            int count,
            Uri origin         = null,
            OscTimeTag?timeTag = null)
        {
            if (reader.PeekByte() == (byte)'#')
            {
                return(OscBundle.Read(reader, count, origin));
            }

            return(OscMessage.Read(reader, count, origin, timeTag));
        }
Esempio n. 6
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();
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public void ReadTest_MultiLine()
        {
            using (MemoryStream stream = new MemoryStream())
                using (StreamWriter writer = new StreamWriter(stream))
                    using (OscReader target = new OscReader(stream, OscPacketFormat.String))
                    {
                        writer.WriteLine(UnitTestHelper.BundleString_MultiLineString);
                        writer.Flush();
                        stream.Position = 0;

                        OscPacket expected = UnitTestHelper.Bundle_MultiLineString();
                        OscPacket actual;

                        actual = target.Read();

                        UnitTestHelper.AreEqual(expected, actual);
                    }
        }
Esempio n. 9
0
        private void Go_Click(object sender, EventArgs e)
        {
            try
            {
                using (FileStream stream = new FileStream(m_FilePath.Text, FileMode.Open, FileAccess.Read))
                    using (OscReader reader = new OscReader(stream, (OscPacketFormat)m_Format.SelectedIndex))
                    {
                        while (reader.EndOfStream == false)
                        {
                            AppendLine(reader.Read().ToString());
                        }

                        AppendLine("End of file");
                    }
            }
            catch (Exception ex)
            {
                AppendLine("Exception while reading");
                AppendLine(ex.Message);
            }
        }
Esempio n. 10
0
        public OscMessageRaw(ArraySegment <byte> buffer, Uri origin = null, OscTimeTag?timestamp = null)
        {
            Origin    = origin;
            Timestamp = timestamp;

            reader = new OscReader(buffer);

            reader.BeginMessage(buffer.Count);

            Address = reader.ReadAddress();

            if (reader.PeekToken() == OscToken.End)
            {
                arguments = new OscArgument[0];

                return;
            }

            OscTypeTag typeTag = reader.ReadTypeTag();

            arguments = new OscArgument[reader.GetArgumentCount(ref typeTag, out OscToken argumentsType)];