HelperClasses.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #if !BESTHTTP_DISABLE_SIGNALR_CORE && !BESTHTTP_DISABLE_WEBSOCKET
  2. using System;
  3. using System.Collections.Generic;
  4. namespace BestHTTP.SignalRCore
  5. {
  6. public enum TransportTypes
  7. {
  8. WebSocket
  9. }
  10. public enum TransferModes
  11. {
  12. Binary,
  13. Text
  14. }
  15. public enum TransportStates
  16. {
  17. Initial,
  18. Connecting,
  19. Connected,
  20. Closing,
  21. Failed,
  22. Closed
  23. }
  24. /// <summary>
  25. /// Possible states of a HubConnection
  26. /// </summary>
  27. public enum ConnectionStates
  28. {
  29. Initial,
  30. Authenticating,
  31. Negotiating,
  32. Redirected,
  33. Connected,
  34. CloseInitiated,
  35. Closed
  36. }
  37. public interface ITransport
  38. {
  39. TransferModes TransferMode { get; }
  40. TransportTypes TransportType { get; }
  41. TransportStates State { get; }
  42. string ErrorReason { get; }
  43. event Action<TransportStates, TransportStates> OnStateChanged;
  44. void StartConnect();
  45. void StartClose();
  46. void Send(byte[] msg);
  47. }
  48. public interface IEncoder
  49. {
  50. string Name { get; }
  51. string EncodeAsText<T>(T value);
  52. T DecodeAs<T>(string text);
  53. byte[] EncodeAsBinary<T>(T value);
  54. T DecodeAs<T>(byte[] data);
  55. object ConvertTo(Type toType, object obj);
  56. }
  57. public class StreamItemContainer<T>
  58. {
  59. public readonly long id;
  60. public List<T> Items { get; private set; }
  61. public T LastAdded { get; private set; }
  62. //public int newIdx;
  63. //public int newCount;
  64. public bool IsCanceled;
  65. public StreamItemContainer(long _id)
  66. {
  67. this.id = _id;
  68. this.Items = new List<T>();
  69. }
  70. public void AddItem(T item)
  71. {
  72. if (this.Items == null)
  73. this.Items = new List<T>();
  74. //this.newIdx = this.Items.Count;
  75. //this.newCount = 1;
  76. this.Items.Add(item);
  77. this.LastAdded = item;
  78. }
  79. //public void AddItems(T[] items)
  80. //{
  81. // if (this.Items == null)
  82. // this.Items = new List<T>();
  83. //
  84. // this.newIdx = this.Items.Count;
  85. // this.newCount = items.Length;
  86. //
  87. // this.Items.AddRange(items);
  88. //}
  89. }
  90. struct CallbackDescriptor
  91. {
  92. public readonly Type[] ParamTypes;
  93. public readonly Action<object[]> Callback;
  94. public CallbackDescriptor(Type[] paramTypes, Action<object[]> callback)
  95. {
  96. this.ParamTypes = paramTypes;
  97. this.Callback = callback;
  98. }
  99. }
  100. internal sealed class Subscription
  101. {
  102. public List<CallbackDescriptor> callbacks = new List<CallbackDescriptor>(1);
  103. public void Add(Type[] paramTypes, Action<object[]> callback)
  104. {
  105. lock(callbacks)
  106. this.callbacks.Add(new CallbackDescriptor(paramTypes, callback));
  107. }
  108. public void Remove(Action<object[]> callback)
  109. {
  110. lock (callbacks)
  111. {
  112. int idx = -1;
  113. for (int i = 0; i < this.callbacks.Count && idx == -1; ++i)
  114. if (this.callbacks[i].Callback == callback)
  115. idx = i;
  116. if (idx != -1)
  117. this.callbacks.RemoveAt(idx);
  118. }
  119. }
  120. }
  121. }
  122. #endif