Usage Example (connecting)

Coordinator
May 3, 2010 at 1:53 PM

Here is a basic example on how you would make a connection using RAS:

    public class RasDialManaged : IRasDial, IDisposable
    {
        private RasEntry m_entry = null;
        private RasConnState m_state = RasConnState.Disconnected;
        private RasError m_lastError;

        public event ConnectionStateChangeEventHandler StateChanged;

        public RasDialManaged()
        {        
        }

        public RasConnState State
        {
            get { return m_state; }
        }

        public bool Connect(PhoneBookEntry phoneBookEntry)
        {
            RasDialParams rdp = new RasDialParams(phoneBookEntry.EntryName, phoneBookEntry.Username, phoneBookEntry.Password);
            return Connect(rdp);
        }

        public bool Connect(RasDialParams dialParams)
        {
            if (m_entry != null)
            {
                return false;
            }

            // find the RasEntry that corresponds to the passed-in PhoneBookEntry
            m_entry = Ras.Entries[dialParams.EntryName];

            if(m_entry == null)
            {
                throw new ArgumentException(string.Format("No RasEntry found for '{0}'", dialParams.EntryName)); 
            }

            m_entry.RasStatus += m_entry_RasStatus;
            RasError status = m_entry.Dial(true, dialParams);

            return status == RasError.Success;
        }

        void m_entry_RasStatus(int hConn, RasConnState state, RasError error)
        {
            m_lastError = error;
            m_state = state;

            if (StateChanged != null)
            {
                ConnectionState argState = ConnectionState.Unknown;

                switch (state)
                {
                    case RasConnState.OpenPort:
                    case RasConnState.PortOpened:
                        argState = ConnectionState.Unknown;
                        break;
                    case RasConnState.DeviceConnected:
                    case RasConnState.AllDevicesConnected:
                    case RasConnState.Authenticate:
                    case RasConnState.Authenticated:
                        argState = ConnectionState.Connected;
                        break;
                    case RasConnState.Disconnected:
                        argState = ConnectionState.Disconnected;
                        break;
                    default:
                        argState = ConnectionState.Unknown;
                        break;
                }

                if (error != RasError.Success)
                {
                    argState = ConnectionState.ConnectionError;
                }

                StateChanged(argState);
            }
        }

        public void Disconnect()
        {
            m_entry.Hangup();
            m_entry.RasStatus -= m_entry_RasStatus;
            m_entry = null;
        }

        public String GetRasString(int code)
        {
            return string.Empty;
        }

        public int RasErrorCode
        {
            get { return (int)m_lastError; }
        }

        public int StatusCode
        {
            get { return (int)m_state; }
        }

        public bool Connected
        {
            get { return m_state == RasConnState.Connected; }
        }

        public void Dispose()
        {
            if((m_entry != null) && (m_state != RasConnState.Disconnected))
            {
                m_entry.Hangup();
                m_entry = null;
            }
        }
    }