IpcHandleDesc.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Microsoft.IO;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Memory;
  4. using System;
  5. using System.IO;
  6. namespace Ryujinx.HLE.HOS.Ipc
  7. {
  8. class IpcHandleDesc
  9. {
  10. public bool HasPId { get; private set; }
  11. public ulong PId { get; private set; }
  12. public int[] ToCopy { get; private set; }
  13. public int[] ToMove { get; private set; }
  14. public IpcHandleDesc(BinaryReader reader)
  15. {
  16. int word = reader.ReadInt32();
  17. HasPId = (word & 1) != 0;
  18. PId = HasPId ? reader.ReadUInt64() : 0;
  19. int toCopySize = (word >> 1) & 0xf;
  20. int[] toCopy = toCopySize == 0 ? Array.Empty<int>() : new int[toCopySize];
  21. for (int index = 0; index < toCopy.Length; index++)
  22. {
  23. toCopy[index] = reader.ReadInt32();
  24. }
  25. ToCopy = toCopy;
  26. int toMoveSize = (word >> 5) & 0xf;
  27. int[] toMove = toMoveSize == 0 ? Array.Empty<int>() : new int[toMoveSize];
  28. for (int index = 0; index < toMove.Length; index++)
  29. {
  30. toMove[index] = reader.ReadInt32();
  31. }
  32. ToMove = toMove;
  33. }
  34. public IpcHandleDesc(int[] copy, int[] move)
  35. {
  36. ToCopy = copy ?? throw new ArgumentNullException(nameof(copy));
  37. ToMove = move ?? throw new ArgumentNullException(nameof(move));
  38. }
  39. public IpcHandleDesc(int[] copy, int[] move, ulong pId) : this(copy, move)
  40. {
  41. PId = pId;
  42. HasPId = true;
  43. }
  44. public static IpcHandleDesc MakeCopy(params int[] handles)
  45. {
  46. return new IpcHandleDesc(handles, Array.Empty<int>());
  47. }
  48. public static IpcHandleDesc MakeMove(params int[] handles)
  49. {
  50. return new IpcHandleDesc(Array.Empty<int>(), handles);
  51. }
  52. public RecyclableMemoryStream GetStream()
  53. {
  54. RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream();
  55. int word = HasPId ? 1 : 0;
  56. word |= (ToCopy.Length & 0xf) << 1;
  57. word |= (ToMove.Length & 0xf) << 5;
  58. ms.Write(word);
  59. if (HasPId)
  60. {
  61. ms.Write(PId);
  62. }
  63. ms.Write(ToCopy);
  64. ms.Write(ToMove);
  65. ms.Position = 0;
  66. return ms;
  67. }
  68. }
  69. }