Ejemplo n.º 1
0
        public ArraySegment <byte> ReadBlob(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Blob);

            CheckForPacketEnd(OscError.ErrorParsingBlob, 4);

            uint length = unchecked ((uint)ReadIntDirect()); // unchecked ((uint) IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer.Array, buffer.Offset + Position)));


            // this shouldn't happen and means we're decoding rubbish
            if (length > 0 && Position + length > maxPosition)
            {
                throw new OscException(OscError.ErrorParsingBlob, $"Unexpected end of message while parsing argument '{typeTag.Index}'");
            }

            ArraySegment <byte> segment = new ArraySegment <byte>(buffer.Array, buffer.Offset + Position, (int)length);

            Position += (int)length;

            // Advance pass the padding
            if (SkipPadding() == false)
            {
                throw new OscException(OscError.ErrorParsingBlob, $"Unexpected end of message while parsing argument '{typeTag.Index}'");
            }

            currentToken = typeTag.NextToken();

            return(segment);
        }
Ejemplo n.º 2
0
        public OscImpulse ReadImpulse(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Impulse);

            currentToken = typeTag.NextToken();

            return(OscImpulse.Value);
        }
Ejemplo n.º 3
0
        public object ReadNull(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Null);

            currentToken = typeTag.NextToken();

            return(OscNull.Value);
        }
Ejemplo n.º 4
0
        public int StartArray(ref OscTypeTag typeTag, out OscToken arrayType)
        {
            CheckToken(OscToken.ArrayStart);

            int length = typeTag.GetArrayElementCount(out arrayType);

            currentToken = typeTag.NextToken();

            return(length);
        }
Ejemplo n.º 5
0
        public bool ReadBool(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Bool);

            bool result = currentToken == OscToken.True;

            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 6
0
        public long ReadLong(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Long);

            CheckForPacketEnd(OscError.ErrorParsingInt64, 8);

            long result = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(buffer.Array, buffer.Offset + Position));

            Position    += 8;
            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 7
0
        public int ReadInt(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Int);

            CheckForPacketEnd(OscError.ErrorParsingInt32, 4);

            int result = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer.Array, buffer.Offset + Position));

            Position    += 4;
            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 8
0
        public OscTimeTag ReadTimeTag(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.TimeTag);

            CheckForPacketEnd(OscError.ErrorParsingOscTimeTag, 8);

            ulong result = unchecked ((ulong)IPAddress.NetworkToHostOrder(BitConverter.ToInt64(buffer.Array, buffer.Offset + Position)));

            Position    += 8;
            currentToken = typeTag.NextToken();

            return(new OscTimeTag(result));
        }
Ejemplo n.º 9
0
        public byte ReadChar(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Char);

            CheckForPacketEnd(OscError.ErrorParsingChar, 4);

            byte result = buffer.Array[buffer.Offset + Position];

            Position    += 4;
            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 10
0
        public OscReader(ArraySegment <byte> buffer)
        {
            if (buffer.Count % 4 != 0)
            {
                throw new OscException(OscError.InvalidSegmentLength, "The packet length is not the correct size");
            }

            this.buffer  = buffer;
            currentToken = OscToken.OscAddress;
            flipper32    = new BitFlipper32();
            Position     = 0;
            maxPosition  = buffer.Count;
        }
Ejemplo n.º 11
0
        public OscMidiMessage ReadMidi(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Midi);

            CheckForPacketEnd(OscError.ErrorParsingInt32, 4);

            OscMidiMessage result = new OscMidiMessage(unchecked ((uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer.Array, buffer.Offset + Position))));

            Position    += 4;
            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 12
0
        public double ReadDouble(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Double);

            CheckForPacketEnd(OscError.ErrorParsingDouble, 8);

            long value = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(buffer.Array, buffer.Offset + Position));

            double result = BitConverter.Int64BitsToDouble(value);

            Position    += 8;
            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 13
0
        public float ReadFloat(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Float);

            CheckForPacketEnd(OscError.ErrorParsingSingle, 4);

            int value = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer.Array, buffer.Offset + Position));

            flipper32.ValueInt32 = value;

            float result = flipper32.ValueFloat;

            Position    += 4;
            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 14
0
        public string ReadAddress()
        {
            CheckToken(OscToken.OscAddress);

            int  start  = Position;
            bool failed = true;

            // scan forward and look for the end of the address string
            while (Position < maxPosition)
            {
                if (buffer.Array[buffer.Offset + Position++] != 0)
                {
                    continue;
                }

                failed = false;

                break;
            }

            if (failed)
            {
                // this shouldn't happen and means we're decoding rubbish
                throw new OscException(OscError.MissingAddress, "Address terminator could not be found");
            }

            // check for an empty string
            if (Position - start - 1 == 0)
            {
                throw new OscException(OscError.MissingAddress, "Address was empty");
            }

            // read the string
            string result = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + start, Position - start - 1);

            // Advance to the typetag
            if (SkipPadding() == false)
            {
                throw new OscException(OscError.InvalidSegmentLength, "Unexpected end of message");
            }

            currentToken = Position == buffer.Count ? OscToken.End : OscToken.TypeTag;

            return(result);
        }
Ejemplo n.º 15
0
        public OscColor ReadColor(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.Color);

            CheckForPacketEnd(OscError.ErrorParsingColor, 4);

            uint value = unchecked ((uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer.Array, buffer.Offset + Position)));

            byte a, r, g, b;

            r = (byte)((value & 0xFF000000) >> 24);
            g = (byte)((value & 0x00FF0000) >> 16);
            b = (byte)((value & 0x0000FF00) >> 8);
            a = (byte)(value & 0x000000FF);

            Position    += 4;
            currentToken = typeTag.NextToken();

            return(OscColor.FromArgb(a, r, g, b));
        }
Ejemplo n.º 16
0
        //[Conditional("MOOP")]
        private void CheckToken(OscToken expected)
        {
            if (currentToken == expected)
            {
                return;
            }

            if (expected == OscToken.Bool)
            {
                if (currentToken == OscToken.True || currentToken == OscToken.False)
                {
                    return;
                }
            }

            if (expected == OscToken.BundleMessageLength && currentToken == OscToken.End)
            {
                return;
            }

            throw new OscException(OscError.UnexpectedToken, $"Unexpected token {currentToken} expected {expected}");
        }
Ejemplo n.º 17
0
        private string ReadStringInner(ref OscTypeTag typeTag, OscError error)
        {
            int  stringStart = Position;
            bool failed      = true;

            // scan forward and look for the end of the string
            while (Position < maxPosition)
            {
                if (buffer.Array[buffer.Offset + Position++] != 0)
                {
                    continue;
                }

                failed = false;


                break;
            }

            if (failed)
            {
                throw new OscException(error, $@"Terminator could not be found while parsing argument '{typeTag.Index}'");
            }

            string result = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + stringStart, Position - stringStart - 1);

            // Advance pass the padding
            if (SkipPadding() == false)
            {
                throw new OscException(error, $"Unexpected end of message while parsing argument '{typeTag.Index}'");
            }

            currentToken = typeTag.NextToken();

            return(result);
        }
Ejemplo n.º 18
0
        public void WriteTypeTag(OscToken token)
        {
            CheckWriterState(WriterState.TypeTag);

            FlushConditionally();

            switch (token)
            {
            case OscToken.Char:
                Write((byte)'c');
                break;

            case OscToken.True:
                Write((byte)'T');
                break;

            case OscToken.False:
                Write((byte)'F');
                break;

            case OscToken.String:
                Write((byte)'s');
                break;

            case OscToken.Symbol:
                Write((byte)'S');
                break;

            case OscToken.Impulse:
                Write((byte)'I');
                break;

            case OscToken.Null:
                Write((byte)'N');
                break;

            case OscToken.Int:
                Write((byte)'i');
                break;

            case OscToken.Long:
                Write((byte)'h');
                break;

            case OscToken.Float:
                Write((byte)'f');
                break;

            case OscToken.Double:
                Write((byte)'d');
                break;

            case OscToken.TimeTag:
                Write((byte)'t');
                break;

            case OscToken.Blob:
                Write((byte)'b');
                break;

            case OscToken.Color:
                Write((byte)'r');
                break;

            case OscToken.Midi:
                Write((byte)'m');
                break;

            case OscToken.ArrayStart:
                Write((byte)'[');
                break;

            case OscToken.ArrayEnd:
                Write((byte)']');
                break;

            default:
                throw new OscException(OscError.UnexpectedToken, $"Unexpected token {token}");
            }
        }
Ejemplo n.º 19
0
 public int GetArrayElementCount(out OscToken arrayType)
 {
     return(GetArrayLength(Index + 1, out arrayType));
 }
Ejemplo n.º 20
0
 public int GetArgumentCount(out OscToken arrayType)
 {
     return(GetArrayLength(0, out arrayType));
 }
Ejemplo n.º 21
0
        private int GetArrayLength(int index, out OscToken arrayType)
        {
            arrayType = OscToken.None;

            if (index == typeTag.Length)
            {
                return(0);
            }

            if (index < 0 || index > typeTag.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), index, "Index is not a valid part of the type tag");
            }

            int count = 0;
            int inset = 0;

            while (true)
            {
                OscToken token = GetTokenFromTypeTag(index++);

                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (token)
                {
                case OscToken.None:
                case OscToken.OscAddress:
                case OscToken.TypeTag:
                    throw new OscException(OscError.UnexpectedToken, $"Unexpected token {token}");

                case OscToken.True:
                case OscToken.False:
                    if (arrayType == OscToken.None)
                    {
                        arrayType = OscToken.Bool;
                    }
                    else if (arrayType != OscToken.Bool)
                    {
                        arrayType = OscToken.MixedTypes;
                    }

                    if (inset == 0)
                    {
                        count++;
                    }

                    break;

                case OscToken.Null:
                    if (arrayType != OscToken.String &&
                        arrayType != OscToken.Blob)
                    {
                        arrayType = OscToken.MixedTypes;
                    }

                    if (inset == 0)
                    {
                        count++;
                    }

                    break;

                case OscToken.String:
                case OscToken.Blob:
                case OscToken.Char:
                case OscToken.Symbol:
                case OscToken.Impulse:
                case OscToken.Int:
                case OscToken.Long:
                case OscToken.Float:
                case OscToken.Double:
                case OscToken.TimeTag:
                case OscToken.Color:
                case OscToken.Midi:
                    if (arrayType == OscToken.None)
                    {
                        arrayType = token;
                    }
                    else if (arrayType != token)
                    {
                        arrayType = OscToken.MixedTypes;
                    }

                    if (inset == 0)
                    {
                        count++;
                    }

                    break;

                case OscToken.ArrayStart:
                    if (inset == 0)
                    {
                        count++;
                    }

                    inset++;
                    break;

                case OscToken.ArrayEnd:
                    inset--;

                    if (inset == -1)
                    {
                        return(count);
                    }

                    break;

                case OscToken.End:
                    return(count);

                case OscToken.MixedTypes:
                default:
                    throw new OscException(OscError.UnknownArguemntType, $@"Unknown OSC type '{token}' on argument '{index}'");
                }
            }
        }
Ejemplo n.º 22
0
 public int GetArgumentCount(ref OscTypeTag typeTag, out OscToken arrayType)
 {
     return(typeTag.GetArgumentCount(out arrayType));
 }
Ejemplo n.º 23
0
        public OscTypeTag ReadTypeTag()
        {
            CheckToken(OscToken.TypeTag);

            // check that the next char is a comma
            if ((char)buffer.Array[buffer.Offset + Position++] != ',')
            {
                throw new OscException(OscError.MissingComma, "No comma found");
            }

            // mark the start of the type tag
            int  start  = Position;
            int  count  = 0;
            int  inset  = 0;
            bool failed = true;

            // scan forward and look for the end of the typetag string
            while (Position < maxPosition)
            {
                char @char = (char)buffer.Array[buffer.Offset + Position++];

                if (@char == 0)
                {
                    failed = false;
                    break;
                }

                if (inset == 0)
                {
                    count++;
                }

                if (@char == '[')
                {
                    inset++;
                }
                else if (@char == ']')
                {
                    inset--;
                }

                if (inset < 0)
                {
                    throw new OscException(OscError.MalformedTypeTag, "Malformed type tag");
                }
            }

            if (failed)
            {
                // this shouldn't happen and means we're decoding rubbish
                throw new OscException(OscError.MissingTypeTag, "Type tag terminator could not be found");
            }

            // read the string
            string result = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + start, Position - start - 1);

            // Advance to the arguments
            if (SkipPadding() == false)
            {
                throw new OscException(OscError.InvalidSegmentLength, "Unexpected end of message");
            }

            OscTypeTag typeTag = new OscTypeTag(result);

            currentToken = typeTag.CurrentToken;

            return(typeTag);
        }
Ejemplo n.º 24
0
 public void BeginMessage(int count)
 {
     maxPosition  = Position + count;
     currentToken = OscToken.OscAddress;
 }
Ejemplo n.º 25
0
        public void EndArray(ref OscTypeTag typeTag)
        {
            CheckToken(OscToken.ArrayEnd);

            currentToken = typeTag.NextToken();
        }