HelperClasses.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. Connected,
  33. CloseInitiated,
  34. Closed
  35. }
  36. public interface ITransport
  37. {
  38. TransferModes TransferMode { get; }
  39. TransportTypes TransportType { get; }
  40. TransportStates State { get; }
  41. string ErrorReason { get; }
  42. event Action<TransportStates, TransportStates> OnStateChanged;
  43. void StartConnect();
  44. void StartClose();
  45. void Send(byte[] msg);
  46. }
  47. public interface IEncoder
  48. {
  49. string Name { get; }
  50. string EncodeAsText<T>(T value);
  51. T DecodeAs<T>(string text);
  52. byte[] EncodeAsBinary<T>(T value);
  53. T DecodeAs<T>(byte[] data);
  54. object ConvertTo(Type toType, object obj);
  55. }
  56. public class StreamItemContainer<T>
  57. {
  58. public readonly long id;
  59. public List<T> Items { get; private set; }
  60. public T LastAdded { get; private set; }
  61. //public int newIdx;
  62. //public int newCount;
  63. public bool IsCanceled;
  64. public StreamItemContainer(long _id)
  65. {
  66. this.id = _id;
  67. this.Items = new List<T>();
  68. }
  69. public void AddItem(T item)
  70. {
  71. if (this.Items == null)
  72. this.Items = new List<T>();
  73. //this.newIdx = this.Items.Count;
  74. //this.newCount = 1;
  75. this.Items.Add(item);
  76. this.LastAdded = item;
  77. }
  78. //public void AddItems(T[] items)
  79. //{
  80. // if (this.Items == null)
  81. // this.Items = new List<T>();
  82. //
  83. // this.newIdx = this.Items.Count;
  84. // this.newCount = items.Length;
  85. //
  86. // this.Items.AddRange(items);
  87. //}
  88. }
  89. struct CallbackDescriptor
  90. {
  91. public readonly Type[] ParamTypes;
  92. public readonly Action<object[]> Callback;
  93. public CallbackDescriptor(Type[] paramTypes, Action<object[]> callback)
  94. {
  95. this.ParamTypes = paramTypes;
  96. this.Callback = callback;
  97. }
  98. }
  99. internal sealed class Subscription
  100. {
  101. public List<CallbackDescriptor> callbacks = new List<CallbackDescriptor>(1);
  102. public void Add(Type[] paramTypes, Action<object[]> callback)
  103. {
  104. lock(callbacks)
  105. this.callbacks.Add(new CallbackDescriptor(paramTypes, callback));
  106. }
  107. public void Remove(Action<object[]> callback)
  108. {
  109. lock (callbacks)
  110. {
  111. int idx = -1;
  112. for (int i = 0; i < this.callbacks.Count && idx == -1; ++i)
  113. if (this.callbacks[i].Callback == callback)
  114. idx = i;
  115. if (idx != -1)
  116. this.callbacks.RemoveAt(idx);
  117. }
  118. }
  119. }
  120. }
  121. #endif