예제 #1
0
        private MFDevice ConnectToSelectedDevice()
        {
            if (m_device != null)
            {
                Interlocked.Increment(ref m_deviceRefCount);
            }
            else
            {
                MFPortDefinition port = null;
                this.Invoke((MethodInvoker)delegate
                {
                    port = GetSelectedItem();
                });

                if (port != null)
                {
                    try
                    {
                        m_deploy.OpenWithoutConnect = true;
                        
                        m_device = m_deploy.Connect(port, port is MFTcpIpPort ? m_transportTinyBooter : null);

                        m_deploy.OpenWithoutConnect = false;

                        if (m_device != null)
                        {
                            m_device.OnDebugText += new EventHandler<DebugOutputEventArgs>(OnDbgTxt);

                            if (checkBoxUseSSL.Checked)
                            {
                                string certFile = textBoxCert.Text;

                                if (!Path.IsPathRooted(certFile))
                                {
                                    certFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, certFile);
                                }

                                if (!File.Exists(certFile))
                                {
                                    MessageBox.Show(this, "The certificate '" + textBoxCert.Text + "' could not be found.", "MFDeploy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                    m_device.Disconnect();
                                    return null;
                                }
                                try
                                {
                                    X509Certificate2 cert = new X509Certificate2(certFile, textBoxCertPwd.Text);

                                    m_device.UseSsl(cert, Properties.Settings.Default.SslRequireClientCert);
                                }
                                catch
                                {
                                    MessageBox.Show(this, "Invalid password or certificate!", "MFDeploy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    m_device.Disconnect();
                                    return null;
                                }
                            }

                            comboBoxTransport.Invoke((MethodInvoker)delegate
                            {
                                comboBoxDevice.Enabled = false;
                                comboBoxTransport.Enabled = false;
                                connectToolStripMenuItem.Enabled = false;
                            });
                            Interlocked.Increment(ref m_deviceRefCount);
                        }
                    }
                    catch (Exception exc)
                    {
                        DumpToOutput(Properties.Resources.ErrorPrefix + exc.Message);
                    }
                }
            }
            return m_device;
        }