Пример #1
0
        /// <summary>
        ///
        /// </summary>

        public SIMAuthDaemon(CardExpress aCard, SIMAuthDaemonForm.LogMessageEvent log)
        {
            LogMessage += log;
            m_aCard     = aCard;
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>

        private SIMAuthDaemon _ObtainSim()
        {
            if (CommService == null)
            {
                try
                {
                    CommService = new ServiceHost(typeof(AuthServer));
                    CommService.AddServiceEndpoint(typeof(AuthService), new NetTcpBinding(), "net.tcp://" + Dns.GetHostName() + ":" + NetPort);
                    CommService.Open();
                    LogMessage("Created daemon service. Listening for GSMAnalyzer on port " + NetPort);
                }
                catch (Exception e)
                {
                    CommService = null;
                    LogMessage("Failed to create service on port " + NetPort + ": " + e);
                }
            }

            // Determine the list of readers that are connected to this system. If this
            // list is empty, then bail out.

            ArrayList vsReaderNames = new ArrayList();

            try
            {
                int nReaders = m_aCardResourceManager.ListReaders(vsReaderNames);
                if (nReaders == 0)
                {
                    MessageBox.Show("No smart card readers are connected to this system!");
                    return(null);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("No smart card readers are connected to this system!");
                return(null);
            }

            // Try to connect to a smart card inside any of the readers that are connected
            // to this system. This could be simplified if we only have to deal with a
            // single kind of reader that is known in advance.

            CardExpress aCard = new CardExpress(m_aCardResourceManager);

            try
            {
                for (;;)
                {
                    bool fConnected = false;

                    foreach (string sReaderName in vsReaderNames)
                    {
                        fConnected = aCard.Connect(sReaderName, SCardAccessMode.Exclusive,
                                                   SCardProtocolIdentifiers.T0 | SCardProtocolIdentifiers.T1);
                        if (fConnected)
                        {
                            break;
                        }
                    }
                    if (fConnected)
                    {
                        break;
                    }

                    // No card was found. So we prompt the user to insert one.

                    DialogResult nOK = m_aCardForm.InsertCard(this,
                                                              "No SIM card found",
                                                              "Please insert a GSM SIM card into any of the smart card readers " +
                                                              "that are listed below.",
                                                              (string[])vsReaderNames.ToArray(typeof(string)));
                    if (nOK == DialogResult.Cancel)
                    {
                        aCard.Dispose();
                        return(null);
                    }
                }
            }
            catch
            {
                if (aCard != null)
                {
                    aCard.Dispose();
                    aCard = null;
                }

                MessageBox.Show("Error accessing GSM SIM card!");
                return(null);
            }

            return(new SIMAuthDaemon(aCard, LogMessage));
        }