Example #1
0
        void Start()
        {
            // Set up OscIn and OscOut for testing locally
            _oscOut = gameObject.AddComponent <OscOut>();
            _oscIn  = gameObject.AddComponent <OscIn>();
            _oscOut.Open(7000);
            _oscIn.Open(7000);

            // Forward received messages with addresses to methods.
            _oscIn.Map(address1, OnReceived1);
            _oscIn.Map(address2, OnReceived2);

            // Create a bundle and two messages.
            _bundle   = new OscBundle();
            _message1 = new OscMessage(address1);
            _message2 = new OscMessage(address2);

            // Add the two messages to the bundle.
            _bundle.Add(_message1);
            _bundle.Add(_message2);

            // Messages contained in incoming bundles are automatically unpacked,
            // and so incoming bundle objects are never exposed.
            // If you need to access time tags from incoming bundles you can set a
            // flag that will add the a time tag from contained messages when unpacking.
            //_oscIn.addTimeTagsToBundledMessages = true;
        }
        void Update()
        {
            // OPTIMISATION #2: Cache outgoing messages.
            // Use the cached message for sending.
            floatMessage.args[0] = Random.value;

            // OPTIMISATION #3: Use blobs for large arrays.
            // If you are sending large arrays, the you can compress the message
            // sending them as blobs. By doing this you avoid sending a type tag for every
            // element in the array. See the methods IntArrayToBlob and BlobToIntArray.
            int[]  intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] blob     = IntArrayToBlob(intArray);
            blobMessage.args[0] = blob;

            // OPTIMISATION #1: Bundle your messages
            // Sending one bundle instead of seperate messages is more efficient.
            bundle.Add(floatMessage);
            bundle.Add(blobMessage);
            oscOut.Send(bundle);
            bundle.Clear();

            // Update labels.
            if (oscOut.isOpen)
            {
                sendFloatLabel.text = floatMessage.args[0].ToString();
                string intArrayString = "";
                foreach (int i in intArray)
                {
                    intArrayString += i + " ";
                }
                sendBlobLabel.text = intArrayString;
            }
        }
        void Update()
        {
            // Create a bundle, add two messages with seperate addresses and values, then send.
            OscBundle bundle = new OscBundle();
            OscMessage message1 = new OscMessage( address1, Random.value );
            OscMessage message2 = new OscMessage( address2, Random.value );
            bundle.Add( message1 );
            bundle.Add( message2 );
            oscOut.Send( bundle );

            // Update UI.
            sendLabel1.text = message1.ToString();
            sendLabel2.text = message2.ToString();
        }
Example #4
0
        void Update()
        {
            // Create a bundle, add two messages with seperate addresses and values, then send.
            OscBundle  bundle   = new OscBundle();
            OscMessage message1 = new OscMessage(address1, Random.value);
            OscMessage message2 = new OscMessage(address2, Random.value);

            bundle.Add(message1);
            bundle.Add(message2);
            oscOut.Send(bundle);

            // Update UI.
            sendLabel1.text = message1.ToString();
            sendLabel2.text = message2.ToString();
        }
        void Update()
        {
            // If we are going to send 'as bundle', then we would like OSC io
            // to add the timetag to the messeges contained in the bundle.
            oscIn.addTimeTagsToBundledMessages = sendAsBundle;

            // Create a messege
            OscMessage message = new OscMessage( address );

            // Create a timetag. Default time is DateTime.Now.
            OscTimeTag timetag = new OscTimeTag();

            // Make it 1 milisecond into the future.
            timetag.time = timetag.time.AddMilliseconds( 1 );

            // Two possible methods for sending timetags ...
            if( sendAsBundle )
            {
                // Either create a bundle with the timetag, add the message and send.
                OscBundle bundle = new OscBundle( timetag );
                bundle.Add( message );
                oscOut.Send( bundle );
            } else {
                // Or add the timetag to message and send it.
                message.Add( timetag );
                oscOut.Send( message );
            }

            // Update label.
            sendLabel.text = timetag.time + ":" + timetag.time.Millisecond;
        }
        void Update()
        {
            // Create a messege.
            OscMessage message = new OscMessage(address);

            // Create a timetag. Default time is DateTime.Now.
            OscTimeTag timetag = new OscTimeTag();

            // Make it 1 milisecond into the future.
            timetag.time = timetag.time.AddMilliseconds(1);

            // Two possible methods for sending timetags ...
            if (sendTimeTagWithBundle)
            {
                // Either create a bundle with the timetag, add the message and send.
                OscBundle bundle = new OscBundle(timetag);
                bundle.Add(message);
                oscOut.Send(bundle);
            }
            else
            {
                // Or add the timetag to message and send it.
                message.Add(timetag);
                oscOut.Send(message);
            }

            // Incoming bundles are unpacked automatically and are never exposed.
            // In the case where we send the timetag with a bundle and want to
            // access them through incoming messages, we can ask OscIn to add
            // the timetags from bundles to each of their contained messages.
            oscIn.addTimeTagsToBundledMessages = sendTimeTagWithBundle;

            // Update label.
            sendLabel.text = timetag.time + ":" + timetag.time.Millisecond;
        }
    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);
    }
Example #8
0
    public static new bool FromBytes(byte[] data, ref int pos, int end, out OscPacket packet)
    {
        // Check header size
        int bundleByteSize = end - pos;

        if (bundleByteSize < headerByteSize)
        {
            Debug.LogWarning("[OSC io] OscBundle with invalid header was ignored." + Environment.NewLine);
            packet = null;
            return(false);
        }

        // Check prefix
        string prefixInput = Encoding.ASCII.GetString(data, pos, prefixByteSize);

        if (prefixInput != prefix)
        {
            Debug.LogWarning("[OSC io] OscBundle with invalid header was ignored." + Environment.NewLine);
            packet = null;
            return(false);
        }
        pos += prefixByteSize + 1;         // + 1 to ensure bytes multiple of 4

        // Get timetag
        byte[] oscNtpBytes = new byte[OscTimeTag.byteSize];
        Array.Copy(data, pos, oscNtpBytes, 0, OscTimeTag.byteSize);
        OscTimeTag timeTag = new OscTimeTag(oscNtpBytes);

        pos += OscTimeTag.byteSize;

        // Create and fill bundle
        packet = new OscBundle(timeTag);
        OscBundle bundle = packet as OscBundle;

        while (pos < end)
        {
            int length = BitConverter.ToInt32(data, 0);
            pos += 4;
            int       packetEnd = pos + length;
            OscPacket subPacket;
            if (pos < end && OscPacket.FromBytes(data, ref pos, packetEnd, out subPacket))
            {
                bundle.Add(subPacket);
            }
        }

        return(true);
    }
Example #9
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);
    }
Example #10
0
    /// <summary>
    /// Send an OscMessage or OscBundle.
    /// Returns success status.
    /// </summary>
    public bool Send(OscPacket packet)
    {
        if (!isOpen)
        {
            return(false);
        }

        if (_bundleMessagesOnEndOfFrame && packet is OscMessage)
        {
            _endOfFrameBundle.Add(packet);
            return(true);
        }

        // try to pack the message
        byte[] data;
        if (!packet.ToBytes(out data))
        {
            return(false);
        }

        try {
            // Send!!
            _udpClient.Send(data, data.Length, _endPoint);

            // Socket error reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
        } catch (SocketException ex)
        {
            if (ex.ErrorCode == 10051)               // "Network is unreachable"
            // Ignore. We get this when broadcasting while having no access to a network.

            {
            }
            else if (ex.ErrorCode == 10065)                 // "No route to host"
            // Ignore. We get this sometimes when unicasting.

            {
            }
            else if (ex.ErrorCode == 10049)                 // "The requested address is not valid in this context"
            // Ignore. We get this when we broadcast and have no access to the local network. For example if we are using a VPN.

            {
            }
            else if (ex.ErrorCode == 10061)                 // "Connection refused"
            // Ignore.

            {
            }
            else if (ex.ErrorCode == 10040)                 // "Message too long"
            {
                Debug.LogWarning(logPrepend + "Failed to send message. Packet is too big (" + data.Length + " bytes).");
            }
            else
            {
                Debug.LogWarning(logPrepend + "Failed to send message to " + ipAddress + " on port " + port + Environment.NewLine + ex.ErrorCode + ": " + ex.ToString());
            }
            return(false);
        } catch (Exception ex) {
            Debug.LogWarning(logPrepend + "Failed to send message to " + ipAddress + " on port " + port + Environment.NewLine + ex.ToString());
            return(false);
        }

        InvokeAnyMessageEventRecursively(packet);
        return(true);
    }