Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace ArcServer
  7. {
  8.     public class Packet
  9.     {
  10.         public MemoryStream memoryStream;
  11.         public BinaryReader binaryReader;
  12.         public BinaryWriter binaryWriter;
  13.         public short Opcode { get; set; }
  14.  
  15.         /// <summary>
  16.         /// Write a new packet without opcode
  17.         /// </summary>
  18.         public Packet()
  19.         {
  20.             memoryStream = new MemoryStream();
  21.             memoryStream.Position = 0;
  22.             binaryReader = new BinaryReader(memoryStream);
  23.             binaryWriter = new BinaryWriter(memoryStream);
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Write a new packet
  28.         /// </summary>
  29.         /// <param name="Opc"></param>
  30.         public Packet(short Opc)
  31.         {
  32.             memoryStream = new MemoryStream();
  33.             memoryStream.Position = 0;
  34.             binaryReader = new BinaryReader(memoryStream);
  35.             binaryWriter = new BinaryWriter(memoryStream);
  36.             this.Opcode = Opc;
  37.             WriteShort(Opc);
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Packet class made to READ packet
  42.         /// </summary>
  43.         /// <param name="packetArray"></param>
  44.         public Packet(byte[] packetArray)
  45.         {
  46.             memoryStream = new MemoryStream(packetArray);
  47.             memoryStream.Position = 0;
  48.             binaryReader = new BinaryReader(memoryStream);
  49.             binaryWriter = new BinaryWriter(memoryStream);
  50.             Opcode = ReadShort();
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Release packet from memory
  55.         /// </summary>
  56.         public void Dispose()
  57.         {
  58.             memoryStream.Dispose();
  59.         }
  60.  
  61.         public int GetSize()
  62.         {
  63.             return (int)binaryWriter.BaseStream.Length;
  64.         }
  65.  
  66.         public byte[] GetData()
  67.         {
  68.             byte[] totBuffer = new byte[memoryStream.Length];
  69.             long startPos = memoryStream.Position;
  70.             memoryStream.Position = 0;
  71.             memoryStream.Read(totBuffer, 0, (int)memoryStream.Length);
  72.             memoryStream.Position = startPos;
  73.             return totBuffer;
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Write whole file into packet
  78.         /// </summary>
  79.         /// <param name="path"></param>
  80.         public void WriteFile(string path)
  81.         {
  82.             if (!File.Exists(path))
  83.             {
  84.                 Console.WriteLine("Cannot find " + path);
  85.                 return;
  86.             }
  87.             Stream file = new FileStream(path, FileMode.Open);
  88.             byte[] buffer = new byte[file.Length];
  89.             file.Read(buffer, 0, (int)file.Length);
  90.             WriteBytes(buffer);
  91.             file.Dispose();
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Return the packet buffer including lenght
  96.         /// </summary>
  97.         /// <returns></returns>
  98.         public byte[] GetPacket()
  99.         {
  100.             byte[] totBuffer = new byte[memoryStream.Length + 4];
  101.             Buffer.BlockCopy(BitConverter.GetBytes((int)memoryStream.Length), 0, totBuffer, 0, 4);
  102.             Buffer.BlockCopy(memoryStream.ToArray(), 0, totBuffer, 4, (int)memoryStream.Length);
  103.  
  104.             return totBuffer;
  105.         }
  106.  
  107.         /// <summary>
  108.         /// Display packet in console
  109.         /// </summary>
  110.         public void Display()
  111.         {
  112.             DisplayFromPos(0);
  113.         }
  114.  
  115.         public void DisplayFromPos(long pos)
  116.         {
  117.             long oldpos = memoryStream.Position;
  118.             memoryStream.Position = pos;
  119.             byte[] packetdata = new byte[this.memoryStream.Length - memoryStream.Position];
  120.             memoryStream.Read(packetdata, 0, (int)memoryStream.Length - (int)memoryStream.Position);
  121.             Console.Write("[" + DateTime.Now.ToShortDateString() + "] " + "Packet display: " + Opcode.ToString());
  122.             foreach (byte x in packetdata)
  123.             {
  124.                 Console.Write(x.ToString("X2") + " ");
  125.             }
  126.             Console.Write("\n");
  127.             memoryStream.Position = oldpos;
  128.         }
  129.  
  130.         public void Padding(int count) { for (int i = 0; i < count; i++) binaryWriter.Write((byte)00); }
  131.  
  132.         public void FillPadding(string value, int count)
  133.         {
  134.             int padding = count - value.Length;
  135.             foreach (char c in value) binaryWriter.Write((byte)c);
  136.             Padding(padding);
  137.         }
  138.  
  139.         public byte[] ReadBytes(int count)
  140.         {
  141.             return binaryReader.ReadBytes(count);
  142.         }
  143.  
  144.         public void WriteUShort(ushort shorty)
  145.         {
  146.             binaryWriter.Write(shorty);
  147.         }
  148.  
  149.         public void WriteUInt(uint intege)
  150.         {
  151.             binaryWriter.Write(intege);
  152.         }
  153.  
  154.         public void Fill(byte data, int len)
  155.         {
  156.             for (int i = 0; i < len; i++)
  157.             {
  158.                 WriteByte(data);
  159.             }
  160.         }
  161.  
  162.         public void WriteBytes(byte[] data)
  163.         {
  164.             binaryWriter.Write(data);
  165.         }
  166.  
  167.         public void FillBytes(byte[] bytes, int count)
  168.         {
  169.             int topad = count - bytes.Length;
  170.             WriteBytes(bytes);
  171.             Padding(topad);
  172.         }
  173.  
  174.         public void SkipBytes(int count)
  175.         {
  176.             this.memoryStream.Position += count;
  177.         }
  178.  
  179.         public void setIndex(long number)
  180.         {
  181.             this.memoryStream.Position = number;
  182.         }
  183.  
  184.         public ushort ReadUShort() { return binaryReader.ReadUInt16(); }
  185.  
  186.         public byte ReadByte() { return binaryReader.ReadByte(); }
  187.  
  188.         public sbyte ReadSByte() { return binaryReader.ReadSByte(); }
  189.  
  190.         public string ReadStringL()
  191.         {
  192.             string tempString = null;
  193.             int size = binaryReader.ReadInt16();
  194.             char[] stringRead = binaryReader.ReadChars(size);
  195.             foreach (char c in stringRead) tempString += c;
  196.             return tempString;
  197.         }
  198.  
  199.         public string ReadString(int length)
  200.         {
  201.             string tempString = null;
  202.             char[] stringRead = binaryReader.ReadChars(length);
  203.             foreach (char c in stringRead) tempString += c;
  204.             return tempString.Replace("\0", "");
  205.         }
  206.  
  207.         public short ReadShort() { return binaryReader.ReadInt16(); }
  208.  
  209.         public int ReadInt() { return binaryReader.ReadInt32(); }
  210.  
  211.         public void WriteByte(byte value) { binaryWriter.Write(value); }
  212.  
  213.         public void WriteSByte(sbyte value) { binaryWriter.Write(value); }
  214.  
  215.         public void WriteInt(int value) { binaryWriter.Write(value); }
  216.  
  217.         public void WriteShort(short value) { binaryWriter.Write(value); }
  218.  
  219.         public void WriteStringL(string value)
  220.         {
  221.             binaryWriter.Write((short)value.Length);
  222.             foreach (char c in value) binaryWriter.Write(c);
  223.         }
  224.  
  225.         public void WriteString(string value) { foreach (char c in value) binaryWriter.Write(c); }
  226.  
  227.         public void WriteArray(byte[] arrayToWrite) { foreach (byte b in arrayToWrite) binaryWriter.Write(b); }
  228.  
  229.         /// <summary>
  230.         /// Write hex string to packet e.g. ("00 01 02 A5")
  231.         /// </summary>
  232.         /// <param name="stringToWrite"></param>
  233.         public void WriteHexString(string stringToWrite)
  234.         {
  235.             string[] tempArray = stringToWrite.Split(' ');
  236.             foreach (string s in tempArray)
  237.                 binaryWriter.Write(byte.Parse(s, System.Globalization.NumberStyles.HexNumber));
  238.         }
  239.  
  240.         public void WriteLong(ulong p) { binaryWriter.Write((ulong)p); }
  241.  
  242.         public string ToReadableString()
  243.         {
  244.             return memoryStream.ToArray().ToReadableString();
  245.         }
  246.         public string ToReadableByteArray()
  247.         {
  248.             return memoryStream.ToArray().ToReadableByteArray();
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement