/// <summary>
        /// Closes the socket and releases all resources associated with it
        /// </summary>
        public void Dispose()
        {
            lock (m_Lock)
            {
                if (m_State == OscSocketState.Connected)
                {
                    m_State = OscSocketState.Closing;

                    OnClosing();

                    m_Socket.Close();

                    m_Socket = null;

                    m_State = OscSocketState.Closed;
                }
            }
        }
        /// <summary>
        /// Connect the socket
        /// </summary>
        public void Connect()
        {
            lock (m_Lock)
            {
                if (m_State != OscSocketState.NotConnected &&
                    m_State != OscSocketState.Closed)
                {
                    throw new Exception(Strings.Socket_AlreadyOpenOrNotClosed);
                }

                // create the instance of the socket
                m_Socket = new Socket(m_UseIpV6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                m_Socket.Blocking = false;

                if (RemoteAddress == IPAddress.Broadcast)
                {
                    m_Socket.EnableBroadcast = true;
                    m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                }

                // allow the reuse of addresses
                m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 16);
                m_Socket.ExclusiveAddressUse = false;

                if (m_UseDynamicLocalPort == true)
                {
                    IPEndPoint tempEndPoint = new IPEndPoint(m_LocalEndPoint.Address, 0);

                    // bind the the temp local origin
                    m_Socket.Bind(tempEndPoint);

                    // set the local port from the resolved socket port
                    switch (m_Socket.LocalEndPoint.AddressFamily)
                    {
                    case AddressFamily.InterNetworkV6:
                    case AddressFamily.InterNetwork:
                        m_LocalPort          = ((IPEndPoint)m_Socket.LocalEndPoint).Port;
                        m_LocalEndPoint.Port = m_LocalPort;
                        break;

                    default:
                        throw new InvalidOperationException(String.Format(Strings.Socket_UnsupportedAddressFamily, m_Socket.LocalEndPoint.AddressFamily));
                    }
                }
                else
                {
                    // bind the local origin
                    m_Socket.Bind(m_LocalEndPoint);
                }

                if (OscSocketType == Osc.OscSocketType.Receive)
                {
                    if (IsMulticastEndPoint == true)
                    {
                        if (m_UseIpV6 == true)
                        {
                            // add to the membership of the IPv6 multicast origin
                            m_Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(m_RemoteAddress));
                        }
                        else
                        {
                            // add to the membership of the IP multicast origin
                            m_Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(m_RemoteAddress));
                        }
                    }
                }
                else
                {
                    if (IsMulticastEndPoint == true)
                    {
                        if (m_UseIpV6 == true)
                        {
                            // set the multicast TTL
                            m_Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, TimeToLive);
                        }
                        else
                        {
                            // set the multicast TTL
                            m_Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, TimeToLive);
                        }
                    }

                    // connect to the remote origin
                    m_Socket.Connect(m_RemoteEndPoint);
                }

                m_State = OscSocketState.Connected;

                OnConnect();
            }
        }
예제 #3
0
 /// <summary>
 /// Creates a new osc socket state exception
 /// </summary>
 /// <param name="socket">The socket that this exception relates to</param>
 /// <param name="state">The state the socket was in when the exception was thrown</param>
 /// <param name="message">A message string</param>
 /// <param name="innerException">An inner exception</param>
 public OscSocketStateException(OscSocket socket, OscSocketState state, string message, Exception innerException)
     : base(socket, message, innerException)
 {
     this.State = state;
 }