INotificationService.cs 6.1 KB

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