private static ManualResetEvent connectDone = new ManualResetEvent(false); // ManualResetEvent instances signal completion. /// <summary> /// Запуск подключения к серверу /// </summary> /// <param name="IPAddress">IPAddress сервера</param> /// <param name="Port">Порт</param> /// <param name="dataToSerialize">Структура на сериализацию</param> /// <param name="Parent">Родительский объект прогресс-аспекта</param> /// <returns></returns> public static DataToSerialize StartClient(IPAddress IPAddress, int Port, DataToSerialize dataToSerialize, out ConnectionBackInformation ConnectionBackInformation) // Для отправки DataSet { DataToSerialize ReturnDataSet = default; { TimeSpan SerializeBefore_Start = DateTime.Now.TimeOfDay; DataToBinary.Convert(dataToSerialize, out byte[] Bytes_ToSend, out string Exception); // Сериализация dataToSerialize = null; TimeSpan SerializeToSend = DateTime.Now.TimeOfDay - SerializeBefore_Start; if (!string.IsNullOrEmpty(Exception)) { } byte[] Bytes_FromRecieve = StartClient(IPAddress, Port, Bytes_ToSend, out ConnectionBackInformation); ConnectionBackInformation.Send_Serialize = SerializeToSend; ConnectionBackInformation.Send_Bytes = Bytes_ToSend is null ? default : Bytes_ToSend.Length; Bytes_ToSend = null; ConnectionBackInformation.Recieve_Bytes = Bytes_FromRecieve is null ? default : Bytes_FromRecieve.Length; if (!(Bytes_FromRecieve is null)) { TimeSpan SerializeAfter_Start = DateTime.Now.TimeOfDay; DataToBinary.Convert(Bytes_FromRecieve, out ReturnDataSet, out string Ex); // Десериализация ConnectionBackInformation.Recieve_Deserialize = DateTime.Now.TimeOfDay - SerializeAfter_Start; if (!string.IsNullOrEmpty(Ex)) { } Bytes_FromRecieve = null; }
} // Для немедленной отдачи готового параметра #region Serialize / Deserialize Config public static void DeserializeConfig(TimeSpan TimeOut) { TimeSpan timeSpan_StartConfig = DateTime.Now.TimeOfDay; DataToBinary.IsDeSerializeObject = true; StructureValueForClient.IsServer = true; ProgressOfUpdateAtStructAttribute.WriteInConsole_Default = true; ProgressOfUpdateAtStructAttribute.IsSaveProgress = true; ReadyStructure = new StructureValueForClient(); DataToBinary.TimeOut = TimeOut; { ReadyStructure.HR = DataToBinary.DeSerializeObject_ToFile <HR>(nameof(ReadyStructure.HR), out TimeSpan SpendOnIt_Statistic); if (ReadyStructure.HR is null ? false : ReadyStructure.HR.Workers_Hierarchy.Count > 0) { ReadyStructure.UpdateTime_Module_HR = DateTime.Now; }
/// <summary> /// Прослойка/адаптер. Призван конвертировать входящий запрос, обработать на сервере и отправить обратный ответ /// </summary> /// <param name="bytes"></param> /// <param name="MYIpClient"></param> /// <param name="ClassName"></param> /// <param name="Parent"></param> /// <returns></returns> public static Tuple <byte[], string> CommonProcess(byte[] bytes, string MYIpClient, string ClassName) { byte[] Bytes_FromServerToClient = default; { DataToSerialize Data_FromClient = default; { DateTime TimeDeSerialize_Start = DateTime.Now; DataToBinary.Convert(bytes, out Data_FromClient, out string Exception); if (!string.IsNullOrEmpty(Exception)) { } TimeSpan TimeDeSerialize = DateTime.Now - TimeDeSerialize_Start; if (TimeDeSerialize > TimeSpan.FromSeconds(5)) { } } if (!(Data_FromClient is null)) { Tuple <List <dynamic>, string, List <dynamic> > ParametersFromDataSet = Converter.GetParametersFromDataSet(Data_FromClient.dataset); List <dynamic> SetParameters = ParametersFromDataSet.Item1; // Поступающие параметры if (SetParameters.Count > 0 ? SetParameters[0] is StructureValueForClient : false) { SetParameters[0] = Data_FromClient.ReadyStructure; } string NameOfCallMethod = ParametersFromDataSet.Item2; // Имя метода обработчика if (string.IsNullOrEmpty(NameOfCallMethod)) { throw new Exception("Calling method not found"); // It's a exception } else { List <dynamic> ReturnParameters = ParametersFromDataSet.Item3; // Возвращаемые параметры string comment = string.Format("({0}) to access [ {1} ]", MYIpClient, NameOfCallMethod); ConsoleWriteLine.WriteInConsole("Network", "Listener", "In Process", comment, ConsoleColor.White); List <dynamic> ReturnItems_FromServer = null; string Instruction_FromServer = null; ReturnItems_FromServer = ProcessThis(SetParameters, ReturnParameters, NameOfCallMethod, ClassName); ReturnItems_FromServer = ReturnItems_FromServer is null ? null : ReturnItems_FromServer.Select(x => { return(x is null ? "" : x); }).ToList(); Instruction_FromServer = Instructions.CreateInstructions(ReturnItems_FromServer, new List <dynamic>(), ""); // Собрали инструкцию для клиента DataSet Date_FromServer = Converter.ConvertFrom(ReturnItems_FromServer, new List <dynamic>(), Instruction_FromServer); if (ReturnItems_FromServer.Count > 0) { DateTime TimeRecieve_Start = DateTime.Now; dynamic Data_FromServerToClient = ReturnItems_FromServer[0]; if (Data_FromServerToClient is StructureValueForClient) // GetParamatersForClientReady { DateTime LastTime = default; string Key = ""; { // Check Time and properties Dictionary <string, MainParentClass> props = Helper.GetProperties <MainParentClass, StructureValueForClient>((StructureValueForClient)Data_FromServerToClient); foreach (var From in props) { string Name = From.Key; if (Name != "Log_File") { Key += Name; var ReadyStructure_Type = ((StructureValueForClient)Data_FromServerToClient).GetType(); if (!(ReadyStructure_Type is null)) { var To_Property = ReadyStructure_Type.GetField("updateTime_Module_" + Name); if (!(To_Property is null)) { DateTime To_Value = (DateTime)To_Property.GetValue((StructureValueForClient)Data_FromServerToClient); LastTime += To_Value.TimeOfDay; // сумма проверит включит все элементы } } } } } lock (AlreadySerialized) { var Finded = AlreadySerialized.FirstOrDefault(x => x.Key.Equals(Key) || x.Key == Key); if (Finded.Value is null ? false : Finded.Value.Item2 != LastTime) // remove old serialized properties { AlreadySerialized.Remove(Finded.Key); Finded = default; } if (!(Finded.Key is null)) { Bytes_FromServerToClient = Finded.Value.Item1; // return already serialized } else { DateTime TimeSerialize_Start = DateTime.Now; DataToBinary.Convert(Date_FromServer, (StructureValueForClient)Data_FromServerToClient, out Bytes_FromServerToClient, out string Exception);// Конвертнули датасет в байты TimeSpan TimeSerialize = DateTime.Now - TimeSerialize_Start; if (!string.IsNullOrEmpty(Exception)) { } if (TimeSerialize > TimeSpan.FromSeconds(5)) { } { DateTime DeserializeStart = DateTime.Now; DataToBinary.Convert(Bytes_FromServerToClient, out DataToSerialize ReturnDataSet, out string Ex); // Десериализация TimeSpan Deserialize = DateTime.Now - DeserializeStart; if (!string.IsNullOrEmpty(Ex)) { DataToBinary.Convert(Date_FromServer, (StructureValueForClient)Data_FromServerToClient, out Bytes_FromServerToClient, out string Exceptionn);// Конвертнули датасет в байты } } if (!string.IsNullOrEmpty(Key)) { if (AlreadySerialized.FirstOrDefault(x => x.Key == Key).Key is null) { AlreadySerialized.Add(Key, new Tuple <byte[], DateTime>(Bytes_FromServerToClient, LastTime)); // serialize new properties } } } } }