EventDescriptor.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. int callbackCount = Callbacks.Count;
  48. if (CallbackArray == null || CallbackArray.Length < callbackCount)
  49. Array.Resize(ref CallbackArray, callbackCount);
  50. // 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)
  51. // This way we can prevent some strange bug
  52. Callbacks.CopyTo(CallbackArray);
  53. // Go through the delegates and call them
  54. for (int i = 0; i < callbackCount; ++i)
  55. {
  56. try
  57. {
  58. // Call the delegate.
  59. SocketIOCallback callback = CallbackArray[i];
  60. if (callback!= null)
  61. callback(socket, packet, args);
  62. }
  63. catch (Exception ex)
  64. {
  65. // Do not try to emit a new Error when we already tried to deliver an Error, possible causing a
  66. // stack overflow
  67. if (args == null || args.Length == 0 || !(args[0] is Error))
  68. (socket as ISocket).EmitError(SocketIOErrors.User, ex.Message + " " + ex.StackTrace);
  69. HTTPManager.Logger.Exception("EventDescriptor", "Call", ex);
  70. }
  71. // If these callbacks has to be called only once, remove them from the main list
  72. if (this.OnlyOnce)
  73. Callbacks.Remove(CallbackArray[i]);
  74. // Don't keep any reference avoiding memory leaks
  75. CallbackArray[i] = null;
  76. }
  77. }
  78. }
  79. }
  80. #endif