Example #1
0
        /// <summary>
        /// Establishes a physical connection to the remote server identified by a OpcUrl.
        /// </summary>
        /// <param name="url">The network address of the remote server.</param>
        /// <param name="connectData">Any protocol configuration or user authenication information.</param>
        public virtual void Connect(OpcUrl url, OpcConnectData connectData)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }
            if (_server != null)
            {
                throw new OpcResultException(new OpcResult((int)OpcResult.E_FAIL.Code, OpcResult.FuncCallType.SysFuncCall, null), "The server is already connected.");
            }

            //  save url.
            SetUrl(url);

            try
            {
                _factory.ForceDa20Usage = ForceDa20Usage;

                // instantiate the server object.
                _server = _factory.CreateInstance(url, connectData);

                // save the connect data.
                _connectData = connectData;

                try
                {
                    // cache the supported locales.
                    GetSupportedLocales();

                    // update the default locale.
                    SetLocale(_locale);

                    SupportedSpecifications = new List <OpcSpecification>();
#if (_OPCCLIENTSDK_DA)
                    if (_server is Com.Da20.Server)
                    {
                        SupportedSpecifications.Add(OpcSpecification.OPC_DA_20);
                    }
                    else if (_server is Com.Da.Server)
                    {
                        SupportedSpecifications.Add(OpcSpecification.OPC_DA_30);
                        SupportedSpecifications.Add(OpcSpecification.OPC_DA_20);
                    }
#endif
#if (_OPCCLIENTSDK_AE)
#if (_OPCCLIENTSDK_DA)
                    else if (_server is Com.Ae.Server)
#else
                    if (_server is Com.Ae.Server)
#endif
                    {
                        SupportedSpecifications.Add(OpcSpecification.OPC_AE_10);
                    }
#endif
#if (_OPCCLIENTSDK_HDA)
#if (_OPCCLIENTSDK_DA || _OPCCLIENTSDK_AE)
                    else if (_server is Com.Hda.Server)
#else
                    if (_server is Com.Hda.Server)
#endif
                    {
                        SupportedSpecifications.Add(OpcSpecification.OPC_HDA_10);
                    }
#endif
#if (_OPCCLIENTSDK_XMLDA)
                    else if (m_server is Xml.Da.Server)
                    {
                        SupportedSpecifications.Add(OpcSpecification.XML_DA_10);
                    }
#endif
#if (_OPCCLIENTSDK_XI)
                    else if (m_server is Xi.Server)
                    {
                        SupportedSpecifications.Add(OpcSpecification.OPC_XI_10);
                    }
#endif
                }
                catch (Exception e)
                {
                }
            }
            catch (Exception e)
            {
                if (_server != null)
                {
                    try { Disconnect(); }
                    catch (Exception e1)
                    {
                    }
                }

                throw e;
            }
        }
Example #2
0
 /// <summary>
 /// Contructs a server by de-serializing its OpcUrl from the stream.
 /// </summary>
 internal OpcServer(SerializationInfo info, StreamingContext context)
 {
     _serverName = info.GetString(Names.NAME);
     _url        = (OpcUrl)info.GetValue(Names.URL, typeof(OpcUrl));
     _factory    = (IOpcFactory)info.GetValue(Names.FACTORY, typeof(IOpcFactory));
 }
Example #3
0
        /// <summary>
        /// Updates the OpcUrl for the server.
        /// </summary>
        private void SetUrl(OpcUrl url)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            //  cannot change the OpcUrl if the remote server is already instantiated.
            if (_server != null)
            {
                throw new OpcResultException(new OpcResult((int)OpcResult.E_FAIL.Code, OpcResult.FuncCallType.SysFuncCall, null), "The server is already connected.");
            }

            //  copy the url.
            _url = (OpcUrl)url.Clone();

            //  construct a name for the server.
            string name = "";

            //  use the host name as a base.
            if (_url.HostName != null)
            {
                name = _url.HostName.ToLower();

                // suppress localhoat and loopback as explicit hostnames.
                if (name == "localhost" || name == "127.0.0.1")
                {
                    name = "";
                }
            }

            //  append the port.
            if (_url.Port != 0)
            {
                name += String.Format(".{0}", _url.Port);
            }

            //  add a separator.
            if (name != "")
            {
                name += ".";
            }

            //  use the prog id as the name.
            if (_url.Scheme != OpcUrlScheme.HTTP)
            {
                string progID = _url.Path;

                int index = progID.LastIndexOf('/');

                if (index != -1)
                {
                    progID = progID.Substring(0, index);
                }

                name += progID;
            }

            // use full path without the extension as the name.
            else
            {
                string path = _url.Path;

                // strip the file extension.
                int index = path.LastIndexOf('.');

                if (index != -1)
                {
                    path = path.Substring(0, index);
                }

                // replace slashes with dashes.
                while (path.IndexOf('/') != -1)
                {
                    path = path.Replace('/', '-');
                }

                name += path;
            }

            //  save the generated name in case the server name is not already set
            if (String.IsNullOrEmpty(_serverName))
            {
                _serverName = name;
            }
        }
Example #4
0
 /// <summary>
 /// Construct a server by de-serializing its OpcUrl from the stream.
 /// </summary>
 internal OpcServer(SerializationInfo info, StreamingContext context)
 {
     serverName_ = info.GetString(Names.Name);
     opcUrl_     = (OpcUrl)info.GetValue(Names.Url, typeof(OpcUrl));
     Factory     = (IOpcFactory)info.GetValue(Names.Factory, typeof(IOpcFactory));
 }
        /// <summary>
        /// Creates a new instance of the server.
        /// </summary>
        public virtual IOpcServer CreateInstance(OpcUrl url, OpcConnectData connectData)
        {
            IOpcServer server = (IOpcServer)Activator.CreateInstance(SystemType, new object[] { url, connectData });

            return(server);
        }
        /// <summary>
        /// Loads the configuration for the current server.
        /// </summary>
        private bool OnLoad(bool prompt, Technosoftware.DaAeHdaClient.OpcUrl url)
        {
            Stream stream = null;

            try
            {
                Cursor = Cursors.WaitCursor;

                // prompt user to select a configuration file.
                if (prompt)
                {
                    var dialog = new OpenFileDialog
                    {
                        CheckFileExists = true,
                        CheckPathExists = true,
                        DefaultExt      = ".config",
                        Filter          = "Config Files (*.config)|*.config|All Files (*.*)|*.*",
                        Multiselect     = false,
                        ValidateNames   = true,
                        Title           = "Open Server Configuration File",
                        FileName        = configFile_
                    };

                    if (dialog.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    // save the new config file name.
                    configFile_ = dialog.FileName;
                }

                // disconnect from current server.
                OnDisconnect();

                // open configuration file.
                stream = new FileStream(configFile_, FileMode.Open, FileAccess.Read, FileShare.Read);

                // deserialize the server object.
                server_ = (TsCHdaServer) new BinaryFormatter().Deserialize(stream);

                // overrided default url.
                if (url != null)
                {
                    server_.Url = url;
                }

                // connect to new server.
                OnConnect();

                // load succeeded.
                return(true);
            }
            catch (Exception e)
            {
                if (prompt)
                {
                    MessageBox.Show(e.Message);
                }
                return(false);
            }
            finally
            {
                // close the stream.
                if (stream != null)
                {
                    stream.Close();
                }

                Cursor = Cursors.Default;
            }
        }