/
SROPacket.cs
189 lines (149 loc) · 6.85 KB
/
SROPacket.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#region License
// ====================================================
// EasySSA Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
#endregion
using System;
using System.Text;
using System.Linq;
using EasySSA.SSA;
using EasySSA.Common;
namespace EasySSA.Packets {
public sealed class SROPacket : Packet, ISROPacket, IEquatable<Packet>, IEquatable<SROPacket> {
private string m_packetID;
private PacketSendType m_sendType = PacketSendType.UNKNOWN;
private ServerServiceType m_serverType = ServerServiceType.UNKNOWN;
private PacketSocketType m_socketType = PacketSocketType.UNKNOWN;
public string PacketID {
get { return this.m_packetID; }
}
public PacketSendType SendType {
get { return this.m_sendType; }
}
public ServerServiceType ServerServiceType {
get { return this.m_serverType; }
}
public PacketSocketType SocketType {
get { return this.m_socketType; }
}
public SROPacket(Packet rhs) : base(rhs) { }
public SROPacket(string id, Packet packet) : base(packet) { this.m_packetID = id; }
public SROPacket(ushort opcode) : base(opcode) { }
public SROPacket(ushort opcode, bool encrypted) : base(opcode, encrypted) { }
public SROPacket(ushort opcode, bool encrypted, bool massive) : base(opcode, encrypted, massive) { }
public SROPacket(ushort opcode, bool encrypted, bool massive, byte[] bytes) : base(opcode, encrypted, massive, bytes) { }
public SROPacket(ushort opcode, bool encrypted, bool massive, byte[] bytes, int offset, int length) : base(opcode, encrypted, massive, bytes, offset, length) { }
public SROPacket(string id, Packet packet, PacketSendType sendType, ServerServiceType serverType, PacketSocketType socketType) : base(packet) {
this.m_packetID = id;
this.m_sendType = sendType;
this.m_serverType = serverType;
this.m_socketType = socketType;
}
public SROPacket(string id, ushort opcode, bool encrypted, bool massive, PacketSendType sendType, ServerServiceType serverType, PacketSocketType socketType) : base(opcode, encrypted, massive) {
this.m_packetID = id;
this.m_sendType = sendType;
this.m_serverType = serverType;
this.m_socketType = socketType;
}
public void Create(PacketSendType sendType, ServerServiceType serverType, PacketSocketType socketType){
this.m_sendType = sendType;
this.m_serverType = serverType;
this.m_socketType = socketType;
}
public string Dump(bool setLock = false) {
StringBuilder sb = new StringBuilder();
if (this.m_sendType == PacketSendType.REQUEST) {
sb.Append(string.Format("[CLIENT->{0}]", this.m_serverType));
} else if (this.m_sendType == PacketSendType.RESPONSE) {
sb.Append(string.Format("[{0}->CLIENT]", this.m_serverType));
} else {
sb.Append("[?->?]");
}
sb.Append("\t\t");
sb.Append(string.Format("[{0}]", this.m_packetID));
sb.Append("\t\t");
sb.Append(string.Format("[{0:X4}]", this.Opcode));
if (setLock) {
this.Lock();
sb.Append("\t\t");
sb.Append(string.Format("[{0} bytes]", this.Length));
}
if (this.Encrypted) {
sb.Append("\t\t");
sb.Append("[Encrypted]");
}
if (this.Massive) {
sb.Append("\t\t");
sb.Append("[Massive]");
}
//sb.AppendLine(Environment.NewLine);
//sb.Append(HexDump(this.GetBytes()));
return sb.ToString();
}
private string HexDump(byte[] buffer) {
return HexDump(buffer, 0, buffer.Length);
}
private string HexDump(byte[] buffer, int offset, int count) {
const int bytesPerLine = 16;
StringBuilder output = new StringBuilder();
StringBuilder ascii_output = new StringBuilder();
int length = count;
if (length % bytesPerLine != 0) {
length += bytesPerLine - length % bytesPerLine;
}
for (int x = 0; x <= length; ++x) {
if (x % bytesPerLine == 0) {
if (x > 0) {
output.AppendFormat(" {0}{1}", ascii_output.ToString(), Environment.NewLine);
ascii_output.Clear();
}
if (x != length) {
output.AppendFormat("{0:d10} ", x);
}
}
if (x < count) {
output.AppendFormat("{0:X2} ", buffer[offset + x]);
char ch = (char)buffer[offset + x];
if (!Char.IsControl(ch)) {
ascii_output.AppendFormat("{0}", ch);
} else {
ascii_output.Append(".");
}
} else {
output.Append(" ");
ascii_output.Append(".");
}
}
return output.ToString();
}
public bool Equals(SROPacket obj) {
if (obj == null || this.GetType() != obj.GetType()) {
return false;
}
SROPacket other = obj as SROPacket;
bool flag1 = this.Opcode == other.Opcode && this.Encrypted == other.Encrypted && this.Massive == other.Massive && this.GetBytes().SequenceEqual(other.GetBytes());
bool flag2 = this.m_sendType == other.SendType && this.m_serverType == other.ServerServiceType && this.m_socketType == other.SocketType;
return flag1 && flag2;
}
public bool Equals(Packet obj) {
if (obj == null || this.GetType() != obj.GetType()) {
return false;
}
Packet other = obj as Packet;
bool flag1 = this.Opcode == other.Opcode && this.Encrypted == other.Encrypted && this.Massive == other.Massive && this.GetBytes().SequenceEqual(other.GetBytes());
return flag1;
}
public override int GetHashCode() {
unchecked {
var result = 13;
result = (result * 7) ^ m_sendType.GetHashCode();
result = (result * 7) ^ m_serverType.GetHashCode();
result = (result * 7) ^ m_socketType.GetHashCode();
return result;
}
}
}
}