Advertisement
codeplanner

WhileYouWereGone

Mar 25th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. /// <summary>
  2.     /// The helpers for queing and sending messages
  3.     /// Ofcourse you can make this more intelligent to jsut store messages for a certain time or maybe get reciepts etc.
  4.     /// </summary>
  5.     /// <typeparam name="T"></typeparam>
  6.     public static class QueueHelper<T> where T : IXBaseSocket
  7.     {
  8.         public class MessageWrapper<T> where T : IXBaseSocket
  9.         {
  10.             public Func<T, bool> Func { get; set; }
  11.             public ITextArgs TextArgs { get; set; }
  12.  
  13.             public MessageWrapper(ITextArgs textArgs, Func<T, bool> func)
  14.             {
  15.                 this.Func = func;
  16.                 this.TextArgs = textArgs;
  17.             }
  18.  
  19.             public MessageWrapper(ITextArgs textArgs)
  20.             {
  21.                 this.Func = null;
  22.                 this.TextArgs = textArgs;
  23.             }
  24.         }
  25.         private static IDictionary<Guid, IDictionary<string, IList<MessageWrapper<T>>>> queue;
  26.  
  27.         static QueueHelper()
  28.         {
  29.             queue = new Dictionary<Guid, IDictionary<string, IList<MessageWrapper<T>>>>();
  30.         }
  31.  
  32.         public static void OfflineSubscribe(T socket, params string[] events)
  33.         {
  34.             if (!queue.ContainsKey(socket.StorageGuid))
  35.             {
  36.                 queue.Add(socket.StorageGuid, new Dictionary<string, IList<MessageWrapper<T>>>());
  37.             }
  38.  
  39.             foreach (var @event in events.Where(@event => !queue[socket.StorageGuid].ContainsKey(socket.Alias + @event)))
  40.             {
  41.                 queue[socket.StorageGuid].Add(socket.Alias + @event, new List<MessageWrapper<T>>());
  42.             }
  43.         }
  44.  
  45.         public static void OnlinePublish(T socket)
  46.         {
  47.  
  48.             if (queue.ContainsKey(socket.StorageGuid))
  49.             {
  50.                 foreach (var message in queue[socket.StorageGuid].SelectMany(messages => messages.Value))
  51.                 {
  52.                     if (message.Func != null)
  53.                     {
  54.                         if (socket.GetIXBaseSockets<T>(message.Func).Count(x => x.StorageGuid == socket.StorageGuid) > 0)
  55.                         {
  56.                             socket.Send(message.TextArgs);
  57.                         }
  58.                     }
  59.                     else
  60.                     {
  61.                         socket.Send(message.TextArgs);
  62.                     }
  63.                 }
  64.                 queue[socket.StorageGuid] = new Dictionary<string, IList<MessageWrapper<T>>>();
  65.             }
  66.         }
  67.  
  68.         public static void Queue(T socket, ITextArgs message, Func<T, bool> func)
  69.         {
  70.             foreach (var client in queue.Where(client => client.Value.ContainsKey(socket.Alias + message.@event)))
  71.             {
  72.                 client.Value[socket.Alias + message.@event].Add(new MessageWrapper<T>(message, func));
  73.             }
  74.         }
  75.  
  76.         public static void Queue(T socket, ITextArgs message)
  77.         {
  78.             foreach (var client in queue.Where(client => client.Value.ContainsKey(socket.Alias + message.@event)))
  79.             {
  80.                 client.Value[socket.Alias + message.@event].Add(new MessageWrapper<T>(message));
  81.             }
  82.         }
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement