public static bool TryReadFrom(byte[] data, ref int index, int byteCount, out OscBundle bundle)
    {
        // Check header size (prefix + timetag).
        if (data.Length - index < OscConst.bundleHeaderSize)
        {
            Debug.LogWarning("[OscParser] OscBundle with invalid header was ignored." + Environment.NewLine);               // TODO make warnings optional
            bundle = null;
            return(false);
        }

        // Check prefix.
        if (!ReadAndValidatePrefix(data, ref index))
        {
            Debug.LogWarning("[OscParser] OscBundle with invalid header was ignored." + Environment.NewLine);                // TODO make warnings optional
            bundle = null;
            return(false);
        }

        // Try get recycled bundle from the pool, otherwise create new.
        bundle = OscPool.GetBundle();

        // Get timetag osc ntp.
        EightByteOscData timeTagDataValue;

        EightByteOscData.TryReadFrom(data, ref index, out timeTagDataValue);
        bundle.timeTag = timeTagDataValue.timeTagValue;

        // Fill Bundle.
        while (index < byteCount)
        {
            FourByteOscData packetSizeDataValue;
            if (!FourByteOscData.TryReadFrom(data, ref index, out packetSizeDataValue))
            {
                break;
            }
            if (index + packetSizeDataValue.intValue > data.Length)
            {
                Debug.LogWarning("[OscParser] Incomplete OscBundle was ignored.\nPerhaps your UDP buffer size is too small.\n");                    // TODO make warnings optional
                bundle = null;
                return(false);
            }

            OscPacket subPacket;
            if (TryReadFrom(data, ref index, byteCount, out subPacket))
            {
                bundle.Add(subPacket);
            }
            else
            {
                Debug.Log("Failed to read packet.");                   // TODO make warnings optional
            }
        }

        // Done.
        return(true);
    }
    /// <summary>
    /// Tries to get argument at index of midi message. Returns success status.
    /// </summary>
    public bool TryGet(int index, out OscMidiMessage value)
    {
        if (!ValidateTryGet(index, OscArgType.Midi))
        {
            value = new OscMidiMessage();
            return(false);
        }

        // Get.
        int             dataStartIndex = GetDataIndex(index);
        FourByteOscData dataValue;

        if (!FourByteOscData.TryReadFrom(_argData, ref dataStartIndex, out dataValue))
        {
            value = new OscMidiMessage();
            Debug.Log(OscDebug.FailedReadingBytesWarning(this));
            return(false);
        }
        value = dataValue.midiMessage;
        return(true);
    }
    /// <summary>
    /// Tries to get argument at index of type char. Returns success status.
    /// </summary>
    public bool TryGet(int index, out char value)
    {
        if (!ValidateTryGet(index, OscArgType.Char))
        {
            value = (char)OscConst.tagUnsupportedByte;
            return(false);
        }

        // Get.
        int             dataStartIndex = GetDataIndex(index);
        FourByteOscData dataValue;

        if (!FourByteOscData.TryReadFrom(_argData, ref dataStartIndex, out dataValue))
        {
            value = ' ';
            Debug.Log(OscDebug.FailedReadingBytesWarning(this));
            return(false);
        }
        value = dataValue.charValue;
        return(true);
    }
    /// <summary>
    /// Tries to get argument at index of type float. Returns success status.
    /// </summary>
    public bool TryGet(int index, out float value)
    {
        if (!ValidateTryGet(index, OscArgType.Float))
        {
            value = 0;
            return(false);
        }

        // Get.
        int             dataStartIndex = GetDataIndex(index);
        FourByteOscData dataValue;

        if (!FourByteOscData.TryReadFrom(_argData, ref dataStartIndex, out dataValue))
        {
            value = 0;
            Debug.Log(OscDebug.FailedReadingBytesWarning(this));
            return(false);
        }
        value = dataValue.floatValue;
        return(true);
    }
Example #5
0
    public static bool TryReadFrom(byte[] data, ref int index, int byteCount, out OscBundle bundle)
    {
        //Debug.Log( "OscBundle.TryReadFrom. index: " + index + ", expected byteCount: " + byteCount );

        // Check header size (prefix + timetag).
        if (data.Length - index < OscConst.bundleHeaderSize)
        {
            if (OscGlobals.logWarnings)
            {
                Debug.LogWarning(logPrepend + "OscBundle with invalid header was ignored." + Environment.NewLine);
            }
            bundle = null;
            return(false);
        }

        // Check prefix.
        if (!ReadAndValidatePrefix(data, ref index))
        {
            if (OscGlobals.logWarnings)
            {
                Debug.LogWarning(logPrepend + "OscBundle with invalid header was ignored." + Environment.NewLine);
            }
            bundle = null;
            return(false);
        }

        // Try get recycled bundle from the pool, otherwise create new.
        bundle = OscPool.GetBundle();

        // Get (optional) timetag osc ntp.
        EightByteOscData timeTagDataValue;

        if (EightByteOscData.TryReadFrom(data, ref index, out timeTagDataValue))
        {
            bundle.timeTag = timeTagDataValue.timeTagValue;
            //Debug.Log( "Time tag: " + bundle.timeTag );
        }

        // Extract packets from buffer data.
        while (index < byteCount)
        {
            //Debug.Log( "Read index: " + index + " out of " + byteCount );

            // Read packet size.
            FourByteOscData packetSizeDataValue;
            if (!FourByteOscData.TryReadFrom(data, ref index, out packetSizeDataValue) || packetSizeDataValue.intValue == 0)
            {
                //Debug.LogError( "No message size provided!" );
                break;
            }
            //Debug.Log( "packetSizeData: " + packetSizeDataValue.intValue );
            int endDataIndex = index + packetSizeDataValue.intValue;
            if (endDataIndex > data.Length)
            {
                if (OscGlobals.logWarnings)
                {
                    //Debug.LogError( "packetSizeDataValue.intValue: " + packetSizeDataValue.intValue );
                    Debug.LogWarning(string.Format(
                                         "{0}Failed to read OscBundle at index {1} because a OscPacket is too large to fit in buffer (byte size {2}.\n" +
                                         "Your buffer may be too small to read the entire bundle. Try increasing the buffer size in OscIn.",
                                         logPrepend, index, packetSizeDataValue.intValue
                                         ));
                }
                bundle = null;
                return(false);
            }

            //if( index % 4 != 0 ) Debug.LogError( "NOT MULTIPLE OF 4" );

            OscPacket subPacket;
            //Debug.Log( ( (char) data[ index ] ) + " " + data[ index ] );
            if (TryReadFrom(data, ref index, endDataIndex, out subPacket))
            {
                //Debug.Log( "Sub packet read: " + index + " == " + endDataIndex );
                bundle.Add(subPacket);
            }
            else
            {
                if (OscGlobals.logWarnings)
                {
                    Debug.LogWarning(logPrepend + "Failed to read packet.\nIndex: " + index + ", Packet size: " + packetSizeDataValue.intValue + ", Byte count: " + byteCount + ", Buffer size: " + data.Length);
                }
                return(false);
            }
        }

        // Done.
        return(true);
    }