Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PacketHistory.cs
Go to the documentation of this file.
1using System;
4using System.IO;
5using System.Text;
7
8namespace Terraria.Testing;
9
10public class PacketHistory
11{
12 private struct PacketView
13 {
14 public static readonly PacketView Empty = new PacketView(0, 0, DateTime.Now);
15
16 public readonly int Offset;
17
18 public readonly int Length;
19
20 public readonly DateTime Time;
21
22 public PacketView(int offset, int length, DateTime time)
23 {
24 Offset = offset;
25 Length = length;
26 Time = time;
27 }
28 }
29
30 private byte[] _buffer;
31
33
34 private int _bufferPosition;
35
36 private int _historyPosition;
37
38 public PacketHistory(int historySize = 100, int bufferSize = 65535)
39 {
40 }
41
42 [Conditional("DEBUG")]
43 public void Record(byte[] buffer, int offset, int length)
44 {
45 length = Math.Max(0, length);
46 PacketView packetView = AppendPacket(length);
48 }
49
50 private PacketView AppendPacket(int size)
51 {
52 int num = _bufferPosition;
53 if (num + size > _buffer.Length)
54 {
55 num = 0;
56 }
57 PacketView packetView = new PacketView(num, size, DateTime.Now);
58 _packets[_historyPosition] = packetView;
60 _bufferPosition = num + size;
61 return packetView;
62 }
63
64 [Conditional("DEBUG")]
65 public void Dump(string reason)
66 {
67 byte[] dst = new byte[_buffer.Length];
70 StringBuilder stringBuilder = new StringBuilder();
71 int num = 1;
72 for (int i = 0; i < _packets.Length; i++)
73 {
75 if (packetView.Offset == 0 && packetView.Length == 0)
76 {
77 continue;
78 }
79 stringBuilder.Append(string.Format("Packet {0} [Assumed MessageID: {4}, Size: {2}, Buffer Position: {1}, Timestamp: {3:G}]\r\n", num++, packetView.Offset, packetView.Length, packetView.Time, _buffer[packetView.Offset]));
80 for (int j = 0; j < packetView.Length; j++)
81 {
82 stringBuilder.Append(_buffer[packetView.Offset + j].ToString("X2") + " ");
83 if (j % 16 == 15 && j != _packets.Length - 1)
84 {
85 stringBuilder.Append("\r\n");
86 }
87 }
88 stringBuilder.Append("\r\n\r\n");
89 }
90 stringBuilder.Append(reason);
92 File.WriteAllText(Path.Combine(Main.SavePath, "NetDump", CreateDumpFileName()), stringBuilder.ToString());
93 }
94
95 private string CreateDumpFileName()
96 {
97 DateTime dateTime = DateTime.Now.ToLocalTime();
98 return string.Format("Net_{0}_{1}_{2}_{3}.txt", Main.dedServ ? "TerrariaServer" : "Terraria", Main.versionNumber, dateTime.ToString("MM-dd-yy_HH-mm-ss-ffff", CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId);
99 }
100
101 [Conditional("DEBUG")]
102 private void InitializeBuffer(int historySize, int bufferSize)
103 {
104 _packets = new PacketView[historySize];
105 _buffer = new byte[bufferSize];
106 }
107}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static CultureInfo InvariantCulture
static DirectoryInfo CreateDirectory(string path)
Definition Directory.cs:28
static void WriteAllText(string path, string? contents)
Definition File.cs:282
static string Combine(string path1, string path2)
Definition Path.cs:304
static byte Max(byte val1, byte val2)
Definition Math.cs:738
override string ToString()
StringBuilder Append(char value, int repeatCount)
static Thread CurrentThread
Definition Thread.cs:312
static string versionNumber
Definition Main.cs:303
static bool dedServ
Definition Main.cs:1226
static string SavePath
Definition Main.cs:2680
PacketView AppendPacket(int size)
void InitializeBuffer(int historySize, int bufferSize)
PacketHistory(int historySize=100, int bufferSize=65535)
void Record(byte[] buffer, int offset, int length)
static DateTime Now
Definition DateTime.cs:103
override string ToString()
Definition DateTime.cs:1109
PacketView(int offset, int length, DateTime time)