Ejemplo n.º 1
0
        private void Initialize(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new Exception(string.Format("指定的文件不存在!{0}", fileName));
            }

            var iniFile = new WinIniFile(fileName);

            // Parse Local Port.
            var value = iniFile.ReadValue("Local", "Port");

            this.LocalEndPoint.Port = (int)SettingsParser.ParseDecimal(value);

            //
            var index = 1;

            while (index <= 100)
            {
                var section = string.Format("Remote{0}", index++);
                var keys    = iniFile.ReadValue(section, null);
                if (string.IsNullOrEmpty(keys))
                {
                    continue;
                }

                var remoteId = (ushort)SettingsParser.ParseDecimal(iniFile.ReadValue(section, "ID"));
                var endPoint = SettingsParser.ParseIPEndPoint(iniFile.ReadValue(section, "IPEndPoint"));

                this.RemoteEndPoints.Add(remoteId, endPoint);
            }
        }
        private void BuildLocalSafetyParameter()
        {
            this.LocalSafetyParam = new SafetyParameters();

            // address
            //var localAddrs = SettingsParser.ParseDictionary(_iniFile.ReadValue("Local", "Address"), new String[] { ",", "," }, new string[] { "=>" });
            //if (localAddrs.Count < 2) throw new Exception(string.Format("Local.Address个数不能小于2。"));
            //this.LocalSafetyParam.Addr = (ushort)localAddrs[this.LocalCodeInAts];
            this.LocalSafetyParam.Addr = (ushort)SettingsParser.ParseDecimal(_iniFile.ReadValue("Local", "Address"));
            var sid = SettingsParser.ParseDecimals(_iniFile.ReadValue("Local", "SID"), new string[] { ",", "," });

            if (sid.Count < 2)
            {
                throw new Exception(string.Format("Local.SID个数不能小于2。"));
            }
            this.LocalSafetyParam.SID = sid.Select(p => (uint)p).ToArray();

            // SINIT
            var sinit = SettingsParser.ParseDecimals(_iniFile.ReadValue("Local", "SINIT"), new string[] { ",", "," });

            if (sinit.Count < 2)
            {
                throw new Exception(string.Format("Local.SINIT个数不能小于2。"));
            }
            this.LocalSafetyParam.SINIT = sinit.Select(p => (uint)p).ToArray();

            // DataVersion
            var dataVer = SettingsParser.ParseDecimals(_iniFile.ReadValue("Local", "DATAVER"), new string[] { ",", "," });

            if (dataVer.Count < 2)
            {
                throw new Exception(string.Format("Local.DATAVER个数不能小于2。"));
            }
            this.LocalSafetyParam.DATAVER = dataVer.Select(p => (uint)p).ToArray();
        }
        private SafetyParameters CreateRemoteSafetyParameters(string section)
        {
            var remoteSafetyParam = new SafetyParameters();

            // Address
            remoteSafetyParam.Addr = (ushort)SettingsParser.ParseDecimal(_iniFile.ReadValue(section, "Address"));
            // SID
            var sid = SettingsParser.ParseDecimals(_iniFile.ReadValue(section, "SID"), new string[] { ",", "," });

            if (sid.Count < 2)
            {
                throw new Exception(string.Format("{0}.SID个数不能小于2。", section));
            }
            remoteSafetyParam.SID = sid.Select(p => (uint)p).ToArray();
            // SINIT
            var sinit = SettingsParser.ParseDecimals(_iniFile.ReadValue(section, "SINIT"), new string[] { ",", "," });

            if (sinit.Count < 2)
            {
                throw new Exception(string.Format("{0}.SINIT个数不能小于2。", section));
            }
            remoteSafetyParam.SINIT = sinit.Select(p => (uint)p).ToArray();
            // DataVersion
            var dataVer = SettingsParser.ParseDecimals(_iniFile.ReadValue(section, "DATAVER"), new string[] { ",", "," });

            if (dataVer.Count < 2)
            {
                throw new Exception(string.Format("{0}.DATAVER个数不能小于2。", section));
            }
            remoteSafetyParam.DATAVER = dataVer.Select(p => (uint)p).ToArray();

            return(remoteSafetyParam);
        }
        private void BuildLocalWorkParameter()
        {
            this.WorkPeriod = (int)SettingsParser.ParseDecimal(_iniFile.ReadValue("Local", "WorkPeriod"));

            this.LocalWorkParameter                = new WorkParameters();
            this.LocalWorkParameter.IndexError     = (uint)SettingsParser.ParseDecimal(_iniFile.ReadValue("Local", "IndexError"));
            this.LocalWorkParameter.MaxUserDataLen = (uint)SettingsParser.ParseDecimal(_iniFile.ReadValue("Local", "MaxUserDataLen"));
            this.LocalWorkParameter.TimeoutCycle   = (uint)SettingsParser.ParseDecimal(_iniFile.ReadValue("Local", "TimeoutCycle"));
        }
        private void BuildSessionParam()
        {
            this.BuildLocalWorkParameter();

            this.BuildLocalSafetyParameter();

            var result = new List <Tuple <IEnumerable <UdpParameters>, SessionConfig> >();

            var index = 1;

            while (index <= 1000)
            {
                var section = string.Format("Remote{0}", index++);
                var keys    = _iniFile.ReadValue(section, null);
                if (string.IsNullOrEmpty(keys))
                {
                    continue;
                }

                // Read CustomNodeCode。
                var customNodeCode = (ushort)SettingsParser.ParseDecimal(_iniFile.ReadValue(section, "CustomNodeCode"));

                // LoalPort
                var ports = _iniFile.ReadValue(section, "LocalPort").Trim().Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                if (ports.Count < 1)
                {
                    throw new Exception(string.Format("{0}.LocalPort个数不能小于1。", section));
                }
                int localRedPort = 0, localBluePort = 0;
                localRedPort  = (int)SettingsParser.ParseDecimal(ports[0]);
                localBluePort = ports.Count < 2 ? localRedPort : (int)SettingsParser.ParseDecimal(ports[1]);

                // Create Remote EndPoint
                var remoteEP = SettingsParser.ParseIPEndPoints(_iniFile.ReadValue(section, "RemoteEndPoints"), new string[] { ",", "," });
                if (remoteEP.Count < 2)
                {
                    throw new Exception(string.Format("{0}.RemoteEndPoints个数不能小于2。", section));
                }

                // Create UdpParam.
                var udpParam = this.CreateUdpParameters(localRedPort, localBluePort, remoteEP[0], remoteEP[1]);

                // Create Remote SafetyParamters
                var remoteSafetyParam = this.CreateRemoteSafetyParameters(section);

                //
                var sessionCfg   = new SessionConfig(this.LocalWorkParameter, this.LocalSafetyParam, remoteSafetyParam, customNodeCode);
                var sessionParam = new Tuple <IEnumerable <UdpParameters>, SessionConfig>(udpParam, sessionCfg);
                result.Add(sessionParam);
            }
            ;

            this.SessionParam = result;
        }