IPCNotifier.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using InABox.API;
  2. using InABox.Core;
  3. namespace InABox.IPC
  4. {
  5. public class IPCNotifier : Notifier
  6. {
  7. IPCNotifyState NotifyState { get; set; }
  8. public IPCNotifier(IPCNotifyState notifyState)
  9. {
  10. NotifyState = notifyState;
  11. NotifyState.OnPoll += NotifyState_OnPoll;
  12. }
  13. private void NotifyState_OnPoll(IPCNotifyState.Session session)
  14. {
  15. Notify.Poll(session.SessionID);
  16. }
  17. protected override IEnumerable<Guid> GetSessions(Platform platform)
  18. {
  19. return NotifyState.SessionMap.Where(x => x.Value.Platform == platform).Select(x => x.Key);
  20. }
  21. protected override IEnumerable<Guid> GetUserSessions(Guid userID)
  22. {
  23. return CredentialsCache.GetUserSessions(userID);
  24. }
  25. protected override void NotifyAll<TNotification>(TNotification notification)
  26. {
  27. foreach(var session in NotifyState.SessionMap.Values)
  28. {
  29. session.Connection.WriteAsync(IPCMessage.Notification(notification)).ContinueWith(task =>
  30. {
  31. if(task.Exception != null)
  32. {
  33. Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
  34. }
  35. });
  36. }
  37. }
  38. protected override void NotifySession<TNotification>(Guid sessionID, TNotification notification)
  39. {
  40. if(NotifyState.SessionMap.TryGetValue(sessionID, out var session))
  41. {
  42. session.Connection.WriteAsync(IPCMessage.Notification(notification)).ContinueWith(task =>
  43. {
  44. if(task.Exception != null)
  45. {
  46. Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
  47. }
  48. });
  49. }
  50. }
  51. protected override void NotifySession(Guid sessionID, Type TNotification, BaseObject notification)
  52. {
  53. if(NotifyState.SessionMap.TryGetValue(sessionID, out var session))
  54. {
  55. session.Connection.WriteAsync(IPCMessage.Notification(TNotification, notification)).ContinueWith(task =>
  56. {
  57. if(task.Exception != null)
  58. {
  59. Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
  60. }
  61. });
  62. }
  63. }
  64. }
  65. }