Пример #1
0
    public static OscMessage GetMessage(int hash)
    {
        //Debug.Log( "Pool lossy hash count: " + _messageStacks.Count );

        Stack <OscMessage> stack;

        if (_messageStacks.TryGetValue(hash, out stack) && stack.Count > 0)
        {
            OscMessage message = stack.Pop();
            message.Clear();
            return(message);
        }

        //Debug.Log( Time.frameCount +  ": OscPool.GetMessage CREATED MESSAGE! for hash " + hash + " when there was " + _messageStacks.Count + " stacks available.\n" );
        //foreach( KeyValuePair<int, Stack<OscMessage>> pair in _messageStacks ) {
        //	Debug.Log( "\tHash: " + pair.Key + ", message count: " + pair.Value.Count + "\n" );
        //}
        return(new OscMessage());
    }
    // Undocumented on purpose.
    public static bool TryReadFrom(byte[] data, ref int index, int size, ref OscMessage message)
    {
        int beginIndex = index;

        // If we are not provided with a message, then read the lossy hash and try reuse from the pool.
        if (message == null)
        {
            int hash = OscStringHash.Pack(data, index);
            message = OscPool.GetMessage(hash);
        }
        else
        {
            if (message._argInfo.Count > 0)
            {
                message.Clear();                                          // Ensure that arguments are cleared.
            }
        }

        // Address.
        string address = message._address;

        if (!StringOscData.TryReadFrom(data, ref index, ref address))
        {
            Debug.Log(OscDebug.FailedReadingBytesWarning(message));
            return(false);
        }
        message._address = address;

        // Tag prefix.
        if (data[index] != OscConst.tagPrefixByte)
        {
            StringBuilder sb = OscDebug.BuildText(message);
            sb.Append("Read failed. Tag prefix missing.\n");
            Debug.LogWarning(sb.ToString());
            return(false);
        }
        index++;

        // Argument tags.
        for (int i = index; i < data.Length && data[i] != 0; i++)
        {
            message._argInfo.Add(new OscArgInfo(data[i], 0));
        }
        index += message._argInfo.Count;

        // Followed by at least one trailing zero, multiple of four bytes.
        index += 4 - (index % 4);

        //Debug.Log( "READ: Args data start index: " + index );

        // Argument data info.
        int argDataByteCount = 0;

        for (int i = 0; i < message._argInfo.Count; i++)
        {
            byte tagByte      = message._argInfo[i].tagByte;
            int  argByteCount = 0;
            switch (tagByte)
            {
            case OscConst.tagNullByte:
            case OscConst.tagImpulseByte:
            case OscConst.tagTrueByte:
            case OscConst.tagFalseByte:
                break;

            case OscConst.tagFloatByte:
            case OscConst.tagIntByte:
            case OscConst.tagCharByte:
            case OscConst.tagColorByte:
            case OscConst.tagMidiByte:
                argByteCount = 4;
                break;

            case OscConst.tagDoubleByte:
            case OscConst.tagLongByte:
            case OscConst.tagTimetagByte:
                argByteCount = 8;
                break;

            case OscConst.tagStringByte:
            case OscConst.tagSymbolByte:
                argByteCount = StringOscData.EvaluateByteCount(data, index + argDataByteCount);
                break;

            case OscConst.tagBlobByte:
                BlobOscData.TryEvaluateByteCount(data, index + argDataByteCount, out argByteCount);
                break;

            default:
                StringBuilder sb = OscDebug.BuildText(message);
                sb.Append("Read failed. Tag '"); sb.Append((char)tagByte); sb.Append("' is not supported\n");
                Debug.LogWarning(sb.ToString());
                return(false);
            }
            message._argInfo[i] = new OscArgInfo(tagByte, argByteCount);
            //Debug.Log( "i; " + i + ", info: " + message._argInfo[i] );
            argDataByteCount += argByteCount;
        }

        // AdaptiveSet data list.
        if (message._argData.Capacity < argDataByteCount)
        {
            message._argData.Capacity = argDataByteCount;
        }

        // Read data.
        for (int i = 0; i < argDataByteCount; i++)
        {
            message._argData.Add(data[index++]);
        }

        // Cache byte count.
        message._cachedSize = index - beginIndex;
        message._dirtySize  = false;

        return(true);
    }