Guest User

Untitled

a guest
Nov 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. public class Packet
  2. {
  3. public int ID { get; set; }
  4. public string Body { get; set; }
  5. public PacketType Type { get; set; }
  6. public enum PacketType
  7. {
  8. ServerdataAuth = 3,
  9. ServerdataAuthResponse = 2,
  10. ServerdataExecuteCommand = 2,
  11. ServerdataResponseValue = 0
  12. }
  13.  
  14. public static async Task<Packet> FromStream(Stream stream, int count)
  15. {
  16. int id, type;
  17. string body;
  18.  
  19. byte[] intbuff = new byte[sizeof(int)];
  20. byte[] stringbuff = new byte[count - 10];
  21. byte[] nullbuff = new byte[2];
  22.  
  23. await stream.ReadAsync(intbuff, 0, intbuff.Length);
  24. id = BitConverter.ToInt32(intbuff, 0);
  25.  
  26. await stream.ReadAsync(intbuff, 0, intbuff.Length);
  27. type = BitConverter.ToInt32(intbuff, 0);
  28.  
  29. await stream.ReadAsync(stringbuff, 0, stringbuff.Length);
  30. body = System.Text.Encoding.ASCII.GetString(stringbuff);
  31.  
  32. await stream.ReadAsync(nullbuff, 0, nullbuff.Length);
  33. return new Packet()
  34. {
  35. ID = id,
  36. Type = type == 2 ? PacketType.ServerdataAuthResponse : (PacketType)type,
  37. Body = body
  38. };
  39. }
  40.  
  41. public async Task ToStream(Stream stream)
  42. {
  43. byte[] bodyBuffer = System.Text.Encoding.ASCII.GetBytes(Body);
  44. int size = bodyBuffer.Length + 10;
  45.  
  46. await stream.WriteAsync(BitConverter.GetBytes(size), 0, sizeof(int));
  47. await stream.WriteAsync(BitConverter.GetBytes(ID), 0, sizeof(int));
  48. await stream.WriteAsync(BitConverter.GetBytes((int) Type), 0, sizeof(int));
  49. await stream.WriteAsync(bodyBuffer, 0, bodyBuffer.Length);
  50. await stream.WriteAsync(new byte[2], 0, 2);
  51.  
  52. }
  53. }
Add Comment
Please, Sign In to add comment