Advertisement
KpoKec

PacketData

Apr 18th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace DataTypes {
  5.     public struct PacketData {
  6.         public static readonly int        _packetLength = 184;
  7.         private readonly       List<byte> rawData;
  8.         private                int        position;
  9.         private static         int        packetNum;
  10.         public readonly        int        command;
  11.  
  12.     #region Add data
  13.         public void AddData(string value) {
  14.             AddBytes(BinaryToolz.GetBytes(value));
  15.         }
  16.  
  17.         public string GetString() {
  18.             var length = GetByte();
  19.             return BinaryToolz.BytesToUTF(Extract(length));
  20.         }
  21.  
  22.         public void AddData(uint data) {
  23.             AddBytes(BitConverter.GetBytes(data));
  24.         }
  25.  
  26.         public uint GetUint() {
  27.             return BitConverter.ToUInt32(Extract(4), 0);
  28.         }
  29.  
  30.         public void AddData(int data) {
  31.             AddBytes(BitConverter.GetBytes(data));
  32.         }
  33.  
  34.         public int GetInt() {
  35.             return BitConverter.ToInt32(Extract(4), 0);
  36.         }
  37.  
  38.         public void AddData(byte data) {
  39.             AddBytes(data);
  40.         }
  41.  
  42.         public byte GetByte() {
  43.             return rawData[position++];
  44.         }
  45.  
  46.         public void AddData(float value) {
  47.             AddBytes(BitConverter.GetBytes(value));
  48.         }
  49.  
  50.         public float GetFloat() {
  51.             return BitConverter.ToSingle(Extract(4), 0);
  52.         }
  53.  
  54.         public void AddData(double value) {
  55.             AddBytes(BitConverter.GetBytes(value));
  56.         }
  57.  
  58.         public double GetDouble() {
  59.             return BitConverter.ToDouble(Extract(8), 0);
  60.         }
  61.  
  62.         public void AddData(long value) {
  63.             AddBytes(BitConverter.GetBytes(value));
  64.         }
  65.  
  66.         public long GetLong() {
  67.             return BitConverter.ToInt64(Extract(8), 0);
  68.         }
  69.  
  70.         public void AddData(bool value) {
  71.             AddBytes(BitConverter.GetBytes(value));
  72.         }
  73.  
  74.         public bool GetBool() {
  75.             return BitConverter.ToBoolean(Extract(1), 0);
  76.         }
  77.  
  78.         public void AddData(INetworkPacket data) {
  79.             AddBytes(data.GetBytes());
  80.         }
  81.  
  82.         public void AddData(byte[] data) {
  83.             AddBytes(BitConverter.GetBytes(data.Length));
  84.             foreach (var i in data)
  85.                 AddBytes(BitConverter.GetBytes(i));
  86.         }
  87.  
  88.         public byte[] GetByteArray() {
  89.             var l = GetInt();
  90.             var a = new byte[l];
  91.             for (var i = 0; i < l; i++) {
  92.                 a[i] = GetByte();
  93.             }
  94.  
  95.             return a;
  96.         }
  97.  
  98.         public void AddData(int[] data) {
  99.             AddBytes(BitConverter.GetBytes(data.Length));
  100.             foreach (var i in data)
  101.                 AddBytes(BitConverter.GetBytes(i));
  102.         }
  103.  
  104.         public int[] GetIntArray() {
  105.             var l = GetInt();
  106.             var a = new int[l];
  107.             for (var i = 0; i < l; i++) {
  108.                 a[i] = GetInt();
  109.             }
  110.  
  111.             return a;
  112.         }
  113.  
  114.         public void AddData(double[] data) {
  115.             AddBytes(BitConverter.GetBytes(data.Length));
  116.             foreach (var i in data)
  117.                 AddBytes(BitConverter.GetBytes(i));
  118.         }
  119.  
  120.         public double[] GetDoubleArray() {
  121.             var l = GetInt();
  122.             var a = new double[l];
  123.             for (var i = 0; i < l; i++) {
  124.                 a[i] = GetDouble();
  125.             }
  126.  
  127.             return a;
  128.         }
  129.  
  130.         public void AddData(float[] data) {
  131.             AddBytes(BitConverter.GetBytes(data.Length));
  132.             foreach (var i in data)
  133.                 AddBytes(BitConverter.GetBytes(i));
  134.         }
  135.  
  136.         public float[] GetFloatArray() {
  137.             var l = GetInt();
  138.             var a = new float[l];
  139.             for (var i = 0; i < l; i++) {
  140.                 a[i] = GetFloat();
  141.             }
  142.  
  143.             return a;
  144.         }
  145.     #endregion
  146.  
  147.     #region Public
  148.         public PacketData(byte[] data = null) {
  149.             rawData = new List<byte>(_packetLength);
  150.             command = -1;
  151.             if (data != null) {
  152.                 if (data.Length != _packetLength) throw new Exception("Incorrect packet length!");
  153.                 rawData.AddRange(data);
  154.                 position = 0;
  155.                 command  = GetInt();
  156.                 packetNum--;
  157.             }
  158.  
  159.             packetNum++;
  160.             position = 0;
  161.             for (var i = 0; i < _packetLength; i++)
  162.                 rawData.Add(0);
  163.         }
  164.  
  165.         public byte[] GetPacketBytes() {
  166.             return rawData.ToArray();
  167.         }
  168.  
  169.         public byte[] ToArray() {
  170.             return GetPacketBytes();
  171.         }
  172.     #endregion
  173.  
  174.     #region Internal
  175.         private void AddBytes(byte[] data) {
  176.             if (data.Length + position > _packetLength) throw new Exception("Can not add bytes to PacketData (overflow length)");
  177.             foreach (var b in data) {
  178.                 rawData[position++] = b;
  179.             }
  180.         }
  181.  
  182.         private void AddBytes(byte data) {
  183.             if (position + 1 > _packetLength) throw new Exception("Can not add bytes to PacketData (overflow length)");
  184.             rawData[position++] = data;
  185.         }
  186.  
  187.         private void InsertBytes(byte[] data) {
  188.             rawData.InsertRange(0, data);
  189.         }
  190.  
  191.         private byte[] Extract(int length) {
  192.             if (position - length < 0) throw new Exception("Read to many data from packet!");
  193.             var res = new byte[length];
  194.             for (var i = 0; i < length; i++)
  195.                 res[i] = rawData[position++];
  196.             return res;
  197.         }
  198.     #endregion
  199.  
  200.     #region Static generators
  201.         public static byte[] ExtractSubarray(byte[] array, int offset, int length) {
  202.             var result = new byte[length];
  203.             Array.Copy(array, offset, result, 0, length);
  204.             return result;
  205.         }
  206.  
  207.         public static PacketData[] SplitToPackets(byte[] data) {
  208.             var list = new List<PacketData>(5);
  209.             var pos  = 0;
  210.             while (data.Length - pos >= _packetLength) {
  211.                 list.Add(new PacketData(ExtractSubarray(data, pos, _packetLength)));
  212.                 pos += _packetLength;
  213.             }
  214.  
  215.             return list.ToArray();
  216.         }
  217.  
  218.         /// <summary>
  219.         /// Пакет для коннекта с сервером
  220.         /// </summary>
  221.         /// <param name="version">Версия клиента</param>
  222.         /// <returns>Массив пакетов</returns>
  223.         public static PacketData[] Handshake(int version) {
  224.             var pd = new PacketData();
  225.             pd.AddData(0x00000001);  // код команды
  226.             pd.AddData(0x00000001);  // количество пакетов
  227.             pd.AddData(packetNum++); // номер пакета
  228.             pd.AddData(0x1829af91);
  229.             pd.AddData(0x359de321);
  230.             pd.AddData(0x49ea2231);
  231.             pd.AddData(0x546f21a1);
  232.             pd.AddData(0x35647da1);
  233.             pd.AddData(version); // номер версии клиента
  234.             return new[] {pd};
  235.         }
  236.     #endregion
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement