-
Notifications
You must be signed in to change notification settings - Fork 6
/
PacketLog.cs
56 lines (45 loc) · 1.28 KB
/
PacketLog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Ostara {
static class PacketLog {
public static List<PacketInfo> Load(string file) {
var result = new List<PacketInfo>();
var f = File.OpenRead(file);
var r = new BinaryReader(f, Encoding.ASCII);
while (r.PeekChar() != -1) {
var length = r.ReadInt32();
var data = r.ReadBytes(length);
var p = new PacketInfo(data);
p.Opcode = r.ReadUInt16();
p.Time = DateTime.FromFileTimeUtc(r.ReadInt64());
p.Source = (PacketInfo.Daemon)r.ReadByte();
p.Destination = (PacketInfo.Daemon)r.ReadByte();
result.Add(p);
}
r.Dispose();
return result;
}
public static string Save(IEnumerable packets) {
if (!Directory.Exists("log"))
Directory.CreateDirectory("log");
var file = $"log/{DateTime.Now.ToString("dd-MM-yy_HH.mm.ss")}.log";
var f = File.Create(file);
var w = new BinaryWriter(f);
foreach (PacketInfo p in packets) {
w.Write(p.Length);
w.Write(p.Data);
w.Write(p.Opcode);
w.Write(p.Time.ToFileTimeUtc());
w.Write((byte)p.Source);
w.Write((byte)p.Destination);
}
w.Flush();
w.Close();
w.Dispose();
return file;
}
}
}