コード例 #1
0
ファイル: OscBundleRaw.cs プロジェクト: tilde-love/osc-core
        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;
                }
            }
        }
コード例 #2
0
ファイル: OscBundle.cs プロジェクト: tilde-love/osc-core
        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);
        }