Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
    IEnumerator SendBundleOnEndOfFrame()
    {
        yield return(new WaitForEndOfFrame());

        _endOfFrameBundle.timeTag.time = DateTime.Now;
        Send(_endOfFrameBundle);
        _endOfFrameBundle.Clear();
    }
Exemplo n.º 3
0
 public static OscBundle GetBundle()
 {
     if (_bundles.Count > 0)
     {
         OscBundle bundle = _bundles.Pop();
         bundle.Clear();
         return(bundle);
     }
     return(new OscBundle());
 }
Exemplo n.º 4
0
	/// <summary>
	/// Send an OscMessage or OscBundle. Data is serialized and no reference is stored, so you can safely 
	/// change values and send packet immediately again.
	/// Returns success status. 
	/// </summary>
	public bool Send( OscPacket packet )
	{
		if( !isOpen ) return false;

		// On any message.
		if( _onAnyMessage != null ) InvokeAnyMessageEventRecursively( packet );

		// Individual messages are always send in bundles at end of frame.
		if( packet is OscMessage ){
			_endOfFrameBuffer.Add( packet as OscMessage );
			return true; // Assume success.
		}

		// Split bundle case.
		if( packet.Size() > _udpBufferSize ){
			ExtractMessages( packet, _tempMessageQueue );
			int bundleByteCount = OscConst.bundleHeaderSize;
			OscBundle splitBundle = OscPool.GetBundle();
			while( _tempMessageQueue.Count > 0 )
			{
				OscMessage message = _tempMessageQueue.Dequeue();
				// Check if message is too big.
				int messageSize = message.Size() + FourByteOscData.byteCount; // Bundle stores size of each message in a 4 byte integer.
				if( messageSize > _udpBufferSize ){
					StringBuilder sb = OscDebug.BuildText( this );
					sb.Append( "Failed to send message. Message size at " ); sb.Append( messageSize );
					sb.Append( " bytes exceeds udp buffer size at " ); sb.Append( _udpBufferSize );
					sb.Append( " bytes. Try increasing the buffer size.'\n" );
					Debug.LogWarning( sb.ToString() );
					return false;
				}
				// If bundle is full, send it and prepare for new bundle.
				if( bundleByteCount + messageSize > _udpBufferSize ) { 
					if( !Send( splitBundle ) ) return false;
					bundleByteCount = OscConst.bundleHeaderSize;
					splitBundle.Clear();
				}
				splitBundle.packets.Add( message );
				bundleByteCount += messageSize;
			}
			if( splitBundle.packets.Count > 0 && !Send( splitBundle ) ) return false;
			OscPool.Recycle( splitBundle );
			return true;
		}

		// Try to pack the message.
		int index = 0;
		if( !packet.TryWriteTo( _cache, ref index ) ) return false;

		//Debug.Log( $"Sending byte count {index}" );

		// Send data!
		return TrySendCache( index );
	}
Exemplo n.º 5
0
    /// <summary>
    /// Send an OscMessage or OscBundle. Data is serialized and no reference is stored, so you can safely
    /// change values and send packet immediately again.
    /// Returns success status.
    /// </summary>
    public bool Send(OscPacket packet)
    {
        if (!isOpen)
        {
            return(false);
        }
        int index = 0;

        // On any message.
        if (_onAnyMessage != null)
        {
            InvokeAnyMessageEventRecursively(packet);
        }

        // Adapt buffer size.
        if (_sendBuffer == null || _sendBuffer.Length != _udpBufferSize)
        {
            _sendBuffer = new byte[_udpBufferSize];
        }

        // Handle user messages.
        if (packet is OscMessage)
        {
            if (_bundleMessagesAutomatically)
            {
                // Collect to be bundled and send by end of the Unity frame.
                _autoBundleMessageBuffer.Add(packet as OscMessage);
                return(true);                // Assume success.
            }
            else
            {
                // Add to cache and send immediately.
                OscMessage message = packet as OscMessage;
                message.TryWriteTo(_sendBuffer, ref index);
                bool success = TrySendBuffer(index);
                if (success)
                {
                    _messageCountThisFrame++;
                }
                return(success);
            }
        }

        // Handle user bundles. Bundles provided by the user are send immediately. If too big, they are split into more bundles.
        OscBundle bundle = packet as OscBundle;

        if (bundle.Size() > _udpBufferSize)
        {
            ExtractMessages(packet, _userBundleTempMessages);
            int       bundleByteCount = OscConst.bundleHeaderSize;
            OscBundle splitBundle     = OscPool.GetBundle();
            while (_userBundleTempMessages.Count > 0)
            {
                OscMessage message = _userBundleTempMessages.Dequeue();
                // Check if message is too big.
                int messageSize = message.Size() + FourByteOscData.byteCount;                 // Bundle stores size of each message in a 4 byte integer.
                if (messageSize > _udpBufferSize)
                {
                    if (OscGlobals.logWarnings)
                    {
                        StringBuilder sb = OscDebug.BuildText(this);
                        sb.Append("Failed to send message. Message size at "); sb.Append(messageSize);
                        sb.Append(" bytes exceeds udp buffer size at "); sb.Append(_udpBufferSize);
                        sb.Append(" bytes. Try increasing the buffer size.'\n");
                        Debug.LogWarning(sb.ToString());
                    }
                    return(false);
                }
                // If bundle is full, send it and prepare for new bundle.
                if (bundleByteCount + messageSize > _udpBufferSize)
                {
                    if (!Send(splitBundle))
                    {
                        return(false);
                    }
                    bundleByteCount = OscConst.bundleHeaderSize;
                    splitBundle.Clear();
                }
                splitBundle.packets.Add(message);
                bundleByteCount += messageSize;
            }
            if (splitBundle.packets.Count > 0 && !Send(splitBundle))
            {
                return(false);
            }
            OscPool.Recycle(splitBundle);
            return(true);
        }

        // Try to pack the message.
        if (!bundle.TryWriteTo(_sendBuffer, ref index))
        {
            return(false);
        }
        _messageCountThisFrame += bundle.packets.Count;

        // Send data!
        return(TrySendBuffer(index));
    }