private void Frame_Frame_OK(object o, Frame_Comm.FOKEventArg e) { MessageDTO msgDTO = new MessageDTO(); byte[] recObj = frame.Get_Input_Frame(); int raw_size; raw_size = Marshal.SizeOf(msgDTO); IntPtr ptr = Marshal.AllocHGlobal(raw_size); Marshal.Copy(recObj, 0, ptr, raw_size); msgDTO = (MessageDTO)Marshal.PtrToStructure(ptr, msgDTO.GetType()); Log log = new Log(); log.Message = Encoding.UTF8.GetString(msgDTO.Message).Replace("\0", ""); log.Level = BitConverter.ToInt32(msgDTO.Level, 0); log.MethodName = Encoding.ASCII.GetString(msgDTO.MethodName).Replace("\0", ""); log.ClassName = Encoding.ASCII.GetString(msgDTO.ClassName).Replace("\0", ""); log.LineNumber = BitConverter.ToUInt32(msgDTO.LineNumber, 0); log.timeStamp = DateTime.FromBinary(BitConverter.ToInt64(msgDTO.timeStamp, 0)); switch (log.Level) { case (int)Levels.Debug: SimonJConsole.WriteLineDebug($"Debug: {log.timeStamp}, {log.ClassName}/{log.MethodName}, Line: {log.LineNumber}, M: {log.Message}"); break; case (int)Levels.Info: SimonJConsole.WriteLineInfo($"Info: {log.timeStamp}, {log.ClassName}/{log.MethodName}, Line: {log.LineNumber}, M: {log.Message}"); break; case (int)Levels.Warning: SimonJConsole.WriteLineWarn($"Warn: {log.timeStamp}, {log.ClassName}/{log.MethodName}, Line: {log.LineNumber}, M: {log.Message}"); break; case (int)Levels.Error: SimonJConsole.WriteLineErr($"Error: {log.timeStamp}, {log.ClassName}/{log.MethodName}, Line: {log.LineNumber}, M: {log.Message}"); break; } Marshal.FreeHGlobal(ptr); }
private static void RecieveData(TcpClient client) { NetworkStream stream = client.GetStream(); //// Send the message to the connected TcpServer. //stream.Write(data, 0, data.Length); //Console.WriteLine("Sent: {0}", message); BinaryFormatter binaryFormatter = new BinaryFormatter(); // Buffer to store the response bytes. //Byte[] responseBuffer = new Byte[256]; // String to store the response ASCII representation. String responseData = String.Empty; int bytesToRead; while (true) { //stream.Write(data, 0, data.Length); Byte[] responseBuffer = new Byte[client.ReceiveBufferSize]; //Console.WriteLine("Sent: {0}", message); //// Read the first batch of the TcpServer response bytes. //Int32 bytes = stream.Read(responseBuffer, 0, responseBuffer.Length); ATDiagnosticsLogger.Log obj = new ATDiagnosticsLogger.Log(); while (stream.DataAvailable && stream.CanRead) //bytesToRead = stream.Read(responseBuffer, 0, responseBuffer.Length)) != 0 { try { obj = (ATDiagnosticsLogger.Log)binaryFormatter.Deserialize(stream); switch (obj.Level) { case Levels.Debug: SimonJConsole.WriteLineDebug($"Debug: {obj.timeStamp}, {obj.ClassName}/{obj.MethodName}, Line: {obj.LineNumber}, M: {obj.Message}"); break; case Levels.Info: SimonJConsole.WriteLineInfo($"Info: {obj.timeStamp}, {obj.ClassName}/{obj.MethodName}, Line: {obj.LineNumber}, M: {obj.Message}"); break; case Levels.Warning: SimonJConsole.WriteLineWarn($"Warn: {obj.timeStamp}, {obj.ClassName}/{obj.MethodName}, Line: {obj.LineNumber}, M: {obj.Message}"); break; case Levels.Error: SimonJConsole.WriteLineErr($"Error: {obj.timeStamp}, {obj.ClassName}/{obj.MethodName}, Line: {obj.LineNumber}, M: {obj.Message}"); break; } //Console.WriteLine($"Received: {obj.ClassName}, {obj.LineNumber}, {obj.MethodName}, {obj.Message}"); } catch (Exception ex) { //some shit } } //responseData = System.Text.Encoding.ASCII.GetString(responseBuffer, 0, bytes); Thread.Sleep(100); } }