Advertisement
Guest User

Untitled

a guest
May 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace Project_Terror_v2.ServerSockets
  8. {
  9. public unsafe class Packet : IDisposable
  10. {
  11.  
  12. public const int MAX_SIZE = 1024;
  13. private const int TQ_SEALSIZE = 8;
  14.  
  15. private static string seal;
  16.  
  17. /*[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
  18. private static extern void* memcpy(void* dst, void* src, int num);*/
  19. [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
  20. private static extern void* memset(void* dst, int val, int count);
  21.  
  22.  
  23. public static string SealString
  24. {
  25. get { return seal; }
  26. set
  27. {
  28.  
  29. if (value.Length != TQ_SEALSIZE)
  30. throw new ArgumentOutOfRangeException("value", "Packet seal must be " + TQ_SEALSIZE + " chars long.");
  31.  
  32. seal = value;
  33. }
  34. }
  35.  
  36. public static int SealSize
  37. {
  38. get { return seal != null ? TQ_SEALSIZE : 0; }
  39. }
  40.  
  41. public int Size { get; set; }
  42. public byte* Memory { get; private set; }
  43. private bool IsDisposed = false;
  44. public byte* stream;
  45.  
  46. public int Position { get { return (int)(stream - Memory); } }
  47. // public byte* Pointer { get { return stream; } }
  48. public Packet(byte[] buffer)
  49. {
  50. Memory = (byte*)Marshal.AllocHGlobal(1024);
  51. stream = Memory;
  52. Marshal.Copy(buffer, 0, (IntPtr)this.stream, buffer.Length);
  53. this.Size = buffer.Length;
  54. }
  55. public Packet(int size)
  56. {
  57. Memory = (byte*)Marshal.AllocHGlobal(size);
  58. stream = Memory;
  59. }
  60.  
  61. ~Packet()
  62. {
  63. this.Dispose();
  64. }
  65. public void Dispose()
  66. {
  67. // lock (this)
  68. {
  69. if (this.IsDisposed) return;
  70. IsDisposed = true;
  71. if ((IntPtr)this.Memory == IntPtr.Zero)
  72. return;
  73. Marshal.FreeHGlobal((IntPtr)this.Memory);
  74. this.Memory = null;
  75. GC.SuppressFinalize(this);
  76. }
  77. }
  78.  
  79. public void InitWriter()
  80. {
  81. Seek(4);
  82. }
  83. public void Seek(int offset)
  84. {
  85. stream = &Memory[offset];
  86. }
  87. public void SeekForward(int amount)
  88. {
  89. Seek(Position + amount);
  90. }
  91. public void SeekBackwards(int amount)
  92. {
  93. Seek(Position - amount);
  94. }
  95. public void Write(byte value)
  96. {
  97. if (Position + 1 >= MAX_SIZE)
  98. return;
  99. *stream = value;
  100. stream++;
  101. }
  102. public void Write(ushort value)
  103. {
  104. if (Position + 2 >= MAX_SIZE)
  105. return;
  106. *((ushort*)stream) = value;
  107. stream += sizeof(ushort);
  108. }
  109. public void Write(uint value)
  110. {
  111. if (Position + 4 >= MAX_SIZE)
  112. return;
  113. *((uint*)stream) = value;
  114. stream += sizeof(uint);
  115. }
  116. public void Write(ulong value)
  117. {
  118. if (Position + 8 >= MAX_SIZE)
  119. return;
  120. *((ulong*)stream) = value;
  121. stream += sizeof(ulong);
  122. }
  123. public void Write(sbyte value) { Write((byte)value); }
  124. public void Write(short value) { Write((ushort)value); }
  125. public void Write(int value) { Write((uint)value); }
  126. public void Write(long value) { Write((ulong)value); }
  127. public void Write(string value, int length)
  128. {
  129. int min = Math.Min(value.Length, length);
  130. var buf = Encoding.Default.GetBytes(value);
  131. for (int i = 0; i < min; i++)
  132. Write(buf[i]);
  133. ZeroFill(length - min);
  134. }
  135. public void Write(params string[] value)
  136. {
  137. Write((byte)value.Length);
  138. for (int i = 0; i < value.Length; i++)
  139. {
  140. var str = value[i];
  141. if (string.IsNullOrEmpty(str))
  142. {
  143. Write((byte)0);
  144. continue;
  145. }
  146.  
  147. Write((byte)str.Length);
  148. Write(str, str.Length);
  149. }
  150. }
  151. public void WriteUnsafe(void* buf, int length)
  152. {
  153. if (Position + length >= MAX_SIZE)
  154. return;
  155. memcpy(stream, buf, length);
  156. stream += length;
  157. }
  158. public void ZeroFill(int amount)
  159. {
  160. if (Position + amount >= MAX_SIZE)
  161. return;
  162. memset(stream, 0, amount);
  163. stream += amount;
  164. }
  165. public string[] ReadStringList()
  166. {
  167. var result = new string[ReadUInt8()];
  168. for (int i = 0; i < result.Length; i++)
  169. result[i] = ReadCString(ReadUInt8());
  170. return result;
  171. }
  172. public void ReadUnsafe(void* buf, int length)
  173. {
  174. if (Position + length >= MAX_SIZE)
  175. return;
  176. memcpy(buf, stream, length);
  177. stream += length;
  178. }
  179. public byte[] ReadBytes(int size)
  180. {
  181. byte[] res = new byte[size];
  182. for (int i = 0; i < res.Length; i++)
  183. res[i] = ReadUInt8();
  184. return res;
  185. }
  186. public string ReadCString(int size)
  187. {
  188. if (Position + size >= MAX_SIZE)
  189. return "";
  190. string result = new string((sbyte*)this.stream, 0, size);
  191. stream += size;
  192. int idx = result.IndexOf('\0');
  193. return (idx > -1) ? result.Substring(0, idx) : result;
  194. }
  195. public byte ReadUInt8()
  196. {
  197. if (Position + 1 >= MAX_SIZE)
  198. return 0;
  199. var result = *stream;
  200. stream++;
  201. return result;
  202. }
  203. public ushort ReadUInt16()
  204. {
  205. if (Position + 2 >= MAX_SIZE)
  206. return 0;
  207. var result = *((ushort*)stream);
  208. stream += sizeof(ushort);
  209. return result;
  210. }
  211. public uint ReadUInt32()
  212. {
  213. if (Position + 4 >= MAX_SIZE)
  214. return 0;
  215. var result = *((uint*)stream);
  216. stream += sizeof(uint);
  217. return result;
  218. }
  219. public ulong ReadUInt64()
  220. {
  221. if (Position + 8 >= MAX_SIZE)
  222. return 0;
  223. var result = *((ulong*)stream);
  224. stream += sizeof(ulong);
  225. return result;
  226. }
  227. public sbyte ReadInt8() { return (sbyte)ReadUInt8(); }
  228. public short ReadInt16() { return (short)ReadUInt16(); }
  229. public int ReadInt32() { return (int)ReadUInt32(); }
  230. public long ReadInt64() { return (long)ReadUInt64(); }
  231.  
  232. public void Finalize(ushort type)
  233. {
  234. if (SealSize > 0)
  235. {
  236. WriteSeal();
  237. }
  238.  
  239. this.Size = this.Position;
  240. Seek(0);
  241. Write((ushort)(this.Size - SealSize));
  242. Write((ushort)type);
  243. }
  244. public void ProtoBufferSerialize(object obj)
  245. {
  246. using (var ms = new System.IO.MemoryStream())
  247. {
  248. ProtoBuf.Serializer.Serialize(ms, obj);
  249. byte[] array = ms.ToArray();
  250.  
  251. fixed (byte* proto_ptr = array)
  252. memcpy(stream, proto_ptr, array.Length);
  253.  
  254. stream += array.Length;
  255. }
  256. }
  257. public T ProtoBufferDeserialize<T>(T obj)
  258. {
  259. Seek(0);
  260. ushort packet_length = ReadUInt16();
  261. ReadUInt16();
  262. byte[] array = new byte[packet_length - 4];
  263. array = ReadBytes(packet_length - 4);
  264.  
  265. using (var ms = new System.IO.MemoryStream(array))
  266. {
  267. obj = ProtoBuf.Serializer.Deserialize<T>(ms);
  268. }
  269. return obj;
  270. }
  271. public T ProtoBufferDeserialize<T>(T obj, byte[] packet)
  272. {
  273.  
  274. using (var ms = new System.IO.MemoryStream(packet))
  275. {
  276. obj = ProtoBuf.Serializer.Deserialize<T>(ms);
  277. }
  278. return obj;
  279. }
  280.  
  281. public void WriteSeal()
  282. {
  283. Write(SealString, TQ_SEALSIZE); // NOTE: must be 8 chars
  284. }
  285. private static string CreatePacketStringWithNumbers(byte* b, int len)
  286. {
  287. //
  288. // -- Taken from ConquerAI
  289. // Special Thanks: John
  290. //
  291.  
  292. int msgSize = (len) * 4 //4 chars, 2 for number, 1 for white space, 1 for letter
  293. + ((len / 16) + 1) * 9; //1 for /t and 2 for new line/ret, 3 for number, 2 for [ ] and 1 for space
  294. StringBuilder hex = new StringBuilder(msgSize);
  295. for (int i = 0; i < len; i += 16)
  296. {
  297. hex.AppendFormat("[{0:000}] ", i);
  298. for (int z = i; z < i + 16; z++)
  299. {
  300. if (z >= len) hex.Append(" ");
  301. else
  302. hex.AppendFormat("{0:x2} ", b[z]);
  303. }
  304. hex.Append('\t');
  305. for (int z = i; z < i + 16; z++)
  306. {
  307. if (z >= len) hex.Append(" ");
  308. else
  309. {
  310. if (b[z] > 32 && b[z] < 127)
  311. {
  312. hex.AppendFormat("{0}", (char)b[z]);
  313. }
  314. else
  315. {
  316. hex.Append('.');
  317. }
  318. }
  319. }
  320. hex.Append("\r\n");
  321. }
  322. return hex.ToString();
  323. }
  324. public static string Dump(byte[] b)
  325. {
  326. fixed (byte* ptr = b)
  327. return CreatePacketStringWithNumbers(ptr, b.Length);
  328. }
  329. public string Dump(string header)
  330. {
  331. int offset = Position; Seek(2);
  332. int type = ReadUInt16(); Seek(offset);
  333.  
  334. string str = "Packet (" + header + ") - Type: " + type + " Size - " + Size + "\r\n";
  335. str += CreatePacketStringWithNumbers(this.Memory, this.Size);
  336. return str;
  337. }
  338.  
  339. public unsafe void memcpy(void* dest, void* src, Int32 size)
  340. {
  341. Int32 count = size / sizeof(long);
  342. for (Int32 i = 0; i < count; i++)
  343. *(((long*)dest) + i) = *(((long*)src) + i);
  344.  
  345. Int32 pos = size - (size % sizeof(long));
  346. for (Int32 i = 0; i < size % sizeof(long); i++)
  347. *(((Byte*)dest) + pos + i) = *(((Byte*)src) + pos + i);
  348. }
  349.  
  350.  
  351. }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement