public ICollection <Packet> ReadLog(Stream stream, ProgressCallback callback) { int counter = 0; string line = null; Packet pak = null; PacketLog log = new PacketLog(); int dataBytesCount = -1; ArrayList ignoredPacketHeaders = new ArrayList(); string header = ""; using (StreamReader s = new StreamReader(stream, Encoding.ASCII)) { try { while ((line = s.ReadLine()) != null) { if (callback != null && (counter++ & 0x1FFF) == 0) // update progress every 8k lines { callback((int)(stream.Position >> 10), (int)(stream.Length >> 10)); } if (line.Length < 1) { continue; } if (line[0] == '<') { if (pak != null) { if (pak.Length == dataBytesCount) { /// Completed packet! try { pak.InitLog(log); } catch {} } else { ignoredPacketHeaders.Add(header); } } header = line; try { int code; ePacketDirection dir; ePacketProtocol prot; TimeSpan time; dataBytesCount = DaocLoggerV3TextLogReader.ParseHeader(line, out code, out dir, out prot, out time); if (dataBytesCount < 0) { pak = null; ignoredPacketHeaders.Add(header); } else { // header parsed successfully! if (code == 0xa9) { packetsCount++; // get statistics pak = null; // ignore the data } continue; /* pak = PacketManager.CreatePacket(log.Version, code, dir, dataBytesCount); * pak.Code = code; * pak.Direction = dir; * pak.Protocol = prot; * pak.Time = time;*/ } } catch { pak = null; ignoredPacketHeaders.Add(header); } continue; } if (pak == null) { continue; } DaocLoggerV3TextLogReader.ParseDataLine(line, pak); } } catch (Exception e) { Log.Error("reading file", e); } if (pak != null) { if (pak.Length == dataBytesCount) { // last completed packet! } else { ignoredPacketHeaders.Add(header); } } } if (ignoredPacketHeaders.Count > 0) { StringBuilder ignored = new StringBuilder("Ignored packets: " + ignoredPacketHeaders.Count + "\n\n"); for (int i = 0; i < ignoredPacketHeaders.Count; i++) { string s = (string)ignoredPacketHeaders[i]; ignored.Append('\n').Append(s); if (i > 15) { ignored.Append("\n..."); break; } } Log.Info(ignored.ToString()); } return(new List <Packet>(0)); }
public ICollection <Packet> ReadLog(Stream stream, ProgressCallback callback) { string line; ILogReader reader = null; long orgPos = stream.Position; using (StreamReader text = new StreamReader(stream, Encoding.ASCII)) using (BinaryReader bin = new BinaryReader(stream, Encoding.ASCII)) { int i = 0; while ((line = text.ReadLine()) != null && i++ < 100) { if (callback != null && (i & 0xF) == 0) { callback(i, 100); } if (line.Length <= 0) { continue; } if (line[0] == '<' && (line = text.ReadLine()) != null) { if (line.Length < 16 * 3) { continue; } for (int x = 2; x < 15 * 3 - 1; x += 3) { if (line[x] != ' ') { reader = new DaocLoggerV3BinaryLogReader(); break; } } if (reader == null) { reader = new DaocLoggerV3TextLogReader(); } break; } } stream.Position = orgPos; if (reader == null) { line = bin.ReadString(); if (line != null && line.Length > 0 && line[line.Length - 1] == ']' && line.StartsWith("[PacketLogConverter v")) { int startLen = "[PacketLogConverter v".Length; string ver = line.Substring(startLen, line.Length - startLen - 1); switch (ver) { case "1": reader = new PacketLogConverterV1LogReader(); break; default: throw new Exception("Unknown \"PLC\" log version: " + ver); } } } stream.Position = orgPos; if (reader != null) { return(reader.ReadLog(stream, callback)); } return(new DolServerV17LogReader().ReadLog(stream, callback)); } }
public ICollection <Packet> ReadLog(Stream stream, ProgressCallback callback) { List <Packet> packets = new List <Packet>((int)(stream.Length / 128)); int counter = 0; Packet pak = null; PacketLog log = new PacketLog(); int dataBytesCount = -1; int ignoredCount = 0; StringBuilder header = new StringBuilder('<', 64); using (BinaryReader s = new BinaryReader(stream, Encoding.ASCII)) { try { while (s.BaseStream.Position < s.BaseStream.Length) { if (callback != null && (counter++ & 0xFFF) == 0) // update progress every 4096th packet { callback((int)(s.BaseStream.Position >> 10), (int)(s.BaseStream.Length >> 10)); } if (s.ReadChar() != '<') { continue; } if (pak != null) { if (pak.Length == dataBytesCount) { packets.Add(pak); try { pak.InitLog(log); } catch {} } else { ignoredCount++; } } // get the header string header.Length = 1; header.Append(s.ReadChars(48)); for (;;) { char curChar = s.ReadChar(); if (curChar == '<') { s.BaseStream.Seek(-1, SeekOrigin.Current); break; } header.Append(curChar); if (curChar == '>') { s.BaseStream.Seek(2, SeekOrigin.Current); break; } if (header.Length > 128) { break; } } try { int code; ePacketDirection dir; ePacketProtocol prot; TimeSpan time; dataBytesCount = DaocLoggerV3TextLogReader.ParseHeader(header.ToString(), out code, out dir, out prot, out time); pak = PacketManager.CreatePacket(log.Version, (byte)code, dir, dataBytesCount); pak.Code = (byte)code; pak.Direction = dir; pak.Protocol = prot; pak.Time = time; } catch { pak = null; ignoredCount++; // MessageBox.Show("parse header failed:\nposition="+s.BaseStream.Position+"\n'"+header.ToString()+"'\n"); } if (pak == null) { continue; } byte[] pakData = s.ReadBytes(dataBytesCount); pak.Write(pakData, 0, pakData.Length); } } catch (Exception e) { Log.Error("reading file", e); } if (pak != null) { if (pak.Length == dataBytesCount) { packets.Add(pak); } else { ignoredCount++; } } } if (ignoredCount > 0) { Log.Info("ignored packets: " + ignoredCount); } return(packets); }
public ICollection<Packet> ReadLog(Stream stream, ProgressCallback callback) { string line; ILogReader reader = null; long orgPos = stream.Position; using (StreamReader text = new StreamReader(stream, Encoding.ASCII)) using (BinaryReader bin = new BinaryReader(stream, Encoding.ASCII)) { int i = 0; while ((line = text.ReadLine()) != null && i++ < 100) { if (callback != null && (i & 0xF) == 0) callback(i, 100); if (line.Length <= 0) continue; if (line[0] == '<' && (line = text.ReadLine()) != null) { if (line.Length < 16*3) continue; for (int x = 2; x < 15*3-1; x+=3) { if (line[x] != ' ') { reader = new DaocLoggerV3BinaryLogReader(); break; } } if (reader == null) reader = new DaocLoggerV3TextLogReader(); break; } } stream.Position = orgPos; if (reader == null) { line = bin.ReadString(); if (line != null && line.Length > 0 && line[line.Length - 1] == ']' && line.StartsWith("[PacketLogConverter v")) { int startLen = "[PacketLogConverter v".Length; string ver = line.Substring(startLen, line.Length - startLen - 1); switch (ver) { case "1": reader = new PacketLogConverterV1LogReader(); break; default: throw new Exception("Unknown \"PLC\" log version: " + ver); } } } stream.Position = orgPos; if (reader != null) return reader.ReadLog(stream, callback); return new DolServerV17LogReader().ReadLog(stream, callback); } }