Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using UnityEngine;
  4.  
  5. namespace Utility.Packets
  6. {
  7.     public class PacketSerializer
  8.     {
  9.         public static byte[] Build(UInt16 op_code, byte[] data = null)
  10.         {
  11.             byte[] op = BitConverter.GetBytes(op_code);
  12.  
  13.             UInt32 packet_length = Convert.ToUInt32(op.Length);
  14.             byte[] header = BitConverter.GetBytes(packet_length);
  15.  
  16.             byte[] packet = new byte[op.Length + header.Length + data.Length];
  17.  
  18.             System.Buffer.BlockCopy(header, 0, packet, 0, header.Length);
  19.             System.Buffer.BlockCopy(op, 0, packet, header.Length, op.Length);
  20.             System.Buffer.BlockCopy(data, 0, packet, op.Length, data.Length);
  21.  
  22.             return packet;
  23.         }
  24.  
  25.         public static string Beautify(byte[] packet)
  26.         {
  27.             string beautified = "{";
  28.  
  29.             for (int i = 0; i < packet.Length; i++)
  30.             {
  31.                 if(i == packet.Length - 1)
  32.                     beautified += packet[i];
  33.                 else
  34.                     beautified += packet[i] + ", ";
  35.             }
  36.  
  37.             return beautified + "}";
  38.         }
  39.     }
  40.  
  41.     public static class PacketBuilder {
  42.         public static byte[] Pong()
  43.         {
  44.             return PacketSerializer.Build(OpCode.PONG);
  45.         }
  46.  
  47.         public static byte[] Login(UInt16 _version, string _username, string _password)
  48.         {
  49.             byte[] username = System.Text.Encoding.UTF8.GetBytes(_username);
  50.             byte[] password = System.Text.Encoding.UTF8.GetBytes(_password);
  51.  
  52.             UInt16 _user_len = Convert.ToUInt16(username.Length);
  53.             UInt16 _pass_len = Convert.ToUInt16(password.Length);
  54.  
  55.             byte[] version  = BitConverter.GetBytes(_version);
  56.             byte[] user_len = BitConverter.GetBytes(_user_len);
  57.             byte[] pass_len = BitConverter.GetBytes(_pass_len);
  58.  
  59.             byte[] packet = new byte[version.Length + user_len.Length + username.Length + pass_len.Length + password.Length];
  60.  
  61.             Buffer.BlockCopy(version, 0, packet, 0, version.Length);
  62.             Buffer.BlockCopy(user_len, 0, packet, version.Length, user_len.Length);
  63.             Buffer.BlockCopy(username, 0, packet, user_len.Length, username.Length);
  64.             Buffer.BlockCopy(pass_len, 0, packet, username.Length, pass_len.Length);
  65.             Buffer.BlockCopy(password, 0, packet, pass_len.Length, password.Length);
  66.  
  67.             Debug.Log(">> " + PacketSerializer.Beautify(packet));
  68.  
  69.             return PacketSerializer.Build(OpCode.LOGIN, packet);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement