Exemple #1
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);
    }
    /// <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;
        //Debug.Log( "Packet size: " + data.Length + " bytes" + Environment.NewLine );

        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 == 10040 ){ // "Message too long"
                Debug.LogWarning( "[OscOut] Failed to send message. Packet is too big (" + data.Length + " bytes)." );

            } else {
                Debug.LogWarning( "[OscOut] Failed to send message to " + ipAddress + " on port " + port + Environment.NewLine + ex.ErrorCode + ": " + ex.ToString() );
            }
            return false;
        } catch( Exception ex ) {
            Debug.LogWarning( "[OscOut] Failed to send message to " + ipAddress + " on port " + port + Environment.NewLine + ex.ToString() );
            return false;
        }

        InvokeAnyMessageEventRecursively( packet );
        return true;
    }