INotificationService.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using Ryujinx.HLE.Utilities;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace Ryujinx.HLE.HOS.Services.Friend
  9. {
  10. class INotificationService : IpcService, IDisposable
  11. {
  12. private readonly UInt128 _userId;
  13. private readonly FriendServicePermissionLevel _permissionLevel;
  14. private readonly object _lock = new object();
  15. private KEvent _notificationEvent;
  16. private int _notificationEventHandle = 0;
  17. private LinkedList<NotificationInfo> _notifications;
  18. private bool _hasNewFriendRequest;
  19. private bool _hasFriendListUpdate;
  20. public INotificationService(ServiceCtx context, UInt128 userId, FriendServicePermissionLevel permissionLevel)
  21. {
  22. _userId = userId;
  23. _permissionLevel = permissionLevel;
  24. _notifications = new LinkedList<NotificationInfo>();
  25. _notificationEvent = new KEvent(context.Device.System);
  26. _hasNewFriendRequest = false;
  27. _hasFriendListUpdate = false;
  28. NotificationEventHandler.Instance.RegisterNotificationService(this);
  29. }
  30. [Command(0)] //2.0.0+
  31. // nn::friends::detail::ipc::INotificationService::GetEvent() -> handle<copy>
  32. public ResultCode GetEvent(ServiceCtx context)
  33. {
  34. if (_notificationEventHandle == 0)
  35. {
  36. if (context.Process.HandleTable.GenerateHandle(_notificationEvent.ReadableEvent, out _notificationEventHandle) != KernelResult.Success)
  37. {
  38. throw new InvalidOperationException("Out of handles!");
  39. }
  40. }
  41. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_notificationEventHandle);
  42. return ResultCode.Success;
  43. }
  44. [Command(1)] //2.0.0+
  45. // nn::friends::detail::ipc::INotificationService::Clear()
  46. public ResultCode Clear(ServiceCtx context)
  47. {
  48. lock (_lock)
  49. {
  50. _hasNewFriendRequest = false;
  51. _hasFriendListUpdate = false;
  52. _notifications.Clear();
  53. }
  54. return ResultCode.Success;
  55. }
  56. [Command(2)] // 2.0.0+
  57. // nn::friends::detail::ipc::INotificationService::Pop() -> nn::friends::detail::ipc::SizedNotificationInfo
  58. public ResultCode Pop(ServiceCtx context)
  59. {
  60. lock (_lock)
  61. {
  62. if (_notifications.Count >= 1)
  63. {
  64. NotificationInfo notificationInfo = _notifications.First.Value;
  65. _notifications.RemoveFirst();
  66. if (notificationInfo.Type == NotificationEventType.FriendListUpdate)
  67. {
  68. _hasFriendListUpdate = false;
  69. }
  70. else if (notificationInfo.Type == NotificationEventType.NewFriendRequest)
  71. {
  72. _hasNewFriendRequest = false;
  73. }
  74. context.ResponseData.WriteStruct(notificationInfo);
  75. return ResultCode.Success;
  76. }
  77. }
  78. return ResultCode.NotificationQueueEmpty;
  79. }
  80. public void SignalFriendListUpdate(UInt128 targetId)
  81. {
  82. lock (_lock)
  83. {
  84. if (_userId == targetId)
  85. {
  86. if (!_hasFriendListUpdate)
  87. {
  88. NotificationInfo friendListNotification = new NotificationInfo();
  89. if (_notifications.Count != 0)
  90. {
  91. friendListNotification = _notifications.First.Value;
  92. _notifications.RemoveFirst();
  93. }
  94. friendListNotification.Type = NotificationEventType.FriendListUpdate;
  95. _hasFriendListUpdate = true;
  96. if (_hasNewFriendRequest)
  97. {
  98. NotificationInfo newFriendRequestNotification = new NotificationInfo();
  99. if (_notifications.Count != 0)
  100. {
  101. newFriendRequestNotification = _notifications.First.Value;
  102. _notifications.RemoveFirst();
  103. }
  104. newFriendRequestNotification.Type = NotificationEventType.NewFriendRequest;
  105. _notifications.AddFirst(newFriendRequestNotification);
  106. }
  107. // We defer this to make sure we are on top of the queue.
  108. _notifications.AddFirst(friendListNotification);
  109. }
  110. _notificationEvent.ReadableEvent.Signal();
  111. }
  112. }
  113. }
  114. public void SignalNewFriendRequest(UInt128 targetId)
  115. {
  116. lock (_lock)
  117. {
  118. if ((_permissionLevel & FriendServicePermissionLevel.OverlayMask) != 0 && _userId == targetId)
  119. {
  120. if (!_hasNewFriendRequest)
  121. {
  122. if (_notifications.Count == 100)
  123. {
  124. SignalFriendListUpdate(targetId);
  125. }
  126. NotificationInfo newFriendRequestNotification = new NotificationInfo
  127. {
  128. Type = NotificationEventType.NewFriendRequest
  129. };
  130. _notifications.AddLast(newFriendRequestNotification);
  131. _hasNewFriendRequest = true;
  132. }
  133. _notificationEvent.ReadableEvent.Signal();
  134. }
  135. }
  136. }
  137. public void Dispose()
  138. {
  139. NotificationEventHandler.Instance.UnregisterNotificationService(this);
  140. }
  141. }
  142. }