private static IEnumerable <RequestData> FetchItems(TcpTuning parameters, Socket socket, bool paramsToWriteExists, bool archived = false, Queue <RequestData> queue = null) { var list = new List <RequestData>(); foreach (var p in parameters.Parameters.Where(item => item.Archived == archived)) { FetchOneItem(socket, paramsToWriteExists, p); if (archived) { if (queue != null && queue.Count > 0) { var item = queue.Dequeue(); FetchOneItem(socket, false, item); queue.Enqueue(item); } } else { list.Add(p); } } return(list); }
static void Main(string[] args) { LocEvClient = new EventClient(); LocEvClient.Connect(new[] { "fetching", "errors" }, PropertyUpdate, ShowError, UpdateLocalConnectionStatus); var configName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "L2M.xml"); if (File.Exists(configName)) { // чтение конфигурационного файла var xdoc = XDocument.Load(configName); XElement listenTcp = xdoc.Element("Config").Element("ListenTcp"); ReadConfigParameters(listenTcp, out IPAddress ipAddress, out int ipPort, out int sendTimeout, out int receiveTimeout); // запуск потока для прослушивания запросов от устройства по протоколу Modbus Tcp var listener = new BackgroundWorker { WorkerSupportsCancellation = true, WorkerReportsProgress = true }; listener.DoWork += ModbusListener_DoWork; listener.ProgressChanged += ModbusListener_ProgressChanged; var tcptuning = new TcpTuning { Address = IPAddress.Any, Port = ipPort, SendTimeout = sendTimeout, ReceiveTimeout = receiveTimeout, }; listener.RunWorkerAsync(tcptuning); // чтение параметров для опрашивающих потоков foreach (XElement fetchingTcp in xdoc.Element("Config").Element("Fetching").Elements("ChannelTcp")) { ReadConfigParameters(fetchingTcp, out ipAddress, out ipPort, out sendTimeout, out receiveTimeout); var parameters = FillParameters(fetchingTcp); var fetcher = new BackgroundWorker { WorkerSupportsCancellation = true, WorkerReportsProgress = true }; fetcher.DoWork += LogikaFetcher_DoWork; fetcher.ProgressChanged += LogikaFetcher_ProgressChanged; tcptuning = new TcpTuning { Address = ipAddress, Port = ipPort, SendTimeout = sendTimeout, ReceiveTimeout = receiveTimeout, Parameters = parameters, }; fetcher.RunWorkerAsync(tcptuning); } } else { return; } // Если запускает пользователь сам if (Environment.UserInteractive) { var s = WcfEventService.EventService; s.Start(); try { Console.WriteLine("MODBUS listening service loaded."); Console.WriteLine("Ver. 0.4\n"); Console.WriteLine("Press any key for exit..."); Console.ReadKey(); } finally { s.Stop(); } } else { var servicesToRun = new ServiceBase[] { new WinService() }; ServiceBase.Run(servicesToRun); } }