public void Add(OscMessage message)
        {
            int messageSize = message.Size();

            // Adapt size.
            int requiredCapacity = _occupiedSize + messageSize;

            if (requiredCapacity > _data.Length)
            {
                byte[] newData = new byte[_data.Length + capacityStep];
                Buffer.BlockCopy(_data, 0, newData, 0, _occupiedSize);
                _data = newData;
            }

            // Write.
            int index = _occupiedSize;

            message.TryWriteTo(_data, ref index);

            // Update counts.
            _occupiedSize = index;
            _sizes.Add(messageSize);

            //UnityEngine.Debug.Log( "Buffered message in Frame num " + UnityEngine.Time.frameCount + "\n" + message );
        }
Пример #2
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 );
	}
Пример #3
0
        public void Add(OscMessage message)
        {
            int messageSize = message.Size();

            // Adapt size.
            int requiredCapacity = _occupiedSize + messageSize;

            if (requiredCapacity > _data.Length)
            {
                byte[] newData = new byte[requiredCapacity];
                Buffer.BlockCopy(_data, 0, newData, 0, _occupiedSize);
                _data = newData;
            }

            // Write.
            int index = _occupiedSize;

            message.TryWriteTo(_data, ref index);

            // Update counts.
            _occupiedSize = index;
            _sizes.Add(messageSize);
        }
Пример #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);
        }
        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));
    }