EventDescriptor.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #if !BESTHTTP_DISABLE_SOCKETIO
  2. using System;
  3. using System.Collections.Generic;
  4. namespace BestHTTP.SocketIO.Events
  5. {
  6. public delegate void SocketIOCallback(Socket socket, Packet packet, params object[] args);
  7. public delegate void SocketIOAckCallback(Socket socket, Packet packet, params object[] args);
  8. /// <summary>
  9. /// A class to describe an event, and its metadatas.
  10. /// </summary>
  11. internal sealed class EventDescriptor
  12. {
  13. #region Public Properties
  14. /// <summary>
  15. /// List of Callback delegates.
  16. /// </summary>
  17. public List<SocketIOCallback> Callbacks { get; private set; }
  18. /// <summary>
  19. /// If this property is true, callbacks are removed automatically after the event dispatch.
  20. /// </summary>
  21. public bool OnlyOnce { get; private set; }
  22. /// <summary>
  23. /// If this property is true, the dispatching packet's Payload will be decoded using the Manager's Encoder.
  24. /// </summary>
  25. public bool AutoDecodePayload { get; private set; }
  26. #endregion
  27. /// <summary>
  28. /// Cache an array on a hot-path.
  29. /// </summary>
  30. private SocketIOCallback[] CallbackArray;
  31. /// <summary>
  32. /// Constructor to create an EventDescriptor instance and set the meta-datas.
  33. /// </summary>
  34. public EventDescriptor(bool onlyOnce, bool autoDecodePayload, SocketIOCallback callback)
  35. {
  36. this.OnlyOnce = onlyOnce;
  37. this.AutoDecodePayload = autoDecodePayload;
  38. this.Callbacks = new List<SocketIOCallback>(1);
  39. if (callback != null)
  40. Callbacks.Add(callback);
  41. }
  42. /// <summary>
  43. /// Will call the Callback delegates with the given parameters and remove the callbacks if this descriptor marked with a true OnlyOnce property.
  44. /// </summary>
  45. public void Call(Socket socket, Packet packet, params object[] args)
  46. {
  47. if (CallbackArray == null || CallbackArray.Length < Callbacks.Count)
  48. Array.Resize(ref CallbackArray, Callbacks.Count);
  49. // Copy the Callback delegates to an array, because in one of the callbacks we can modify the list(by calling On/Once/Off in an event handler)
  50. // This way we can prevent some strange bug
  51. Callbacks.CopyTo(CallbackArray);
  52. // Go through the delegates and call them
  53. for (int i = 0; i < CallbackArray.Length; ++i)
  54. {
  55. try
  56. {
  57. // Call the delegate.
  58. SocketIOCallback callback = CallbackArray[i];
  59. if (callback!= null)
  60. callback(socket, packet, args);
  61. }
  62. catch (Exception ex)
  63. {
  64. (socket as ISocket).EmitError(SocketIOErrors.User, ex.Message + " " + ex.StackTrace);
  65. HTTPManager.Logger.Exception("EventDescriptor", "Call", ex);
  66. }
  67. // If these callbacks has to be called only once, remove them from the main list
  68. if (this.OnlyOnce)
  69. Callbacks.Remove(CallbackArray[i]);
  70. // Don't keep any reference avoiding memory leaks
  71. CallbackArray[i] = null;
  72. }
  73. }
  74. }
  75. }
  76. #endif