Ejemplo n.º 1
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));
    }