AService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. namespace ET
  6. {
  7. public abstract class AService : IDisposable
  8. {
  9. public Action<long, IPEndPoint> AcceptCallback;
  10. public Action<long, MemoryStream> ReadCallback;
  11. public Action<long, int> ErrorCallback;
  12. public long Id { get; set; } = IdGenerater.Instance.GenerateId();
  13. public ServiceType ServiceType { get; protected set; }
  14. private const int MaxMemoryBufferSize = 1024;
  15. private readonly Queue<MemoryStream> pool = new Queue<MemoryStream>();
  16. public MemoryStream Fetch(int size = 0)
  17. {
  18. if (size > MaxMemoryBufferSize)
  19. {
  20. return new MemoryStream(size);
  21. }
  22. if (size < MaxMemoryBufferSize)
  23. {
  24. size = MaxMemoryBufferSize;
  25. }
  26. if (this.pool.Count == 0)
  27. {
  28. return new MemoryStream(size);
  29. }
  30. return pool.Dequeue();
  31. }
  32. public void OnError(long channelId, int e)
  33. {
  34. this.Remove(channelId);
  35. this.ErrorCallback?.Invoke(channelId, e);
  36. }
  37. public void Recycle(MemoryStream memoryBuffer)
  38. {
  39. if (memoryBuffer.Capacity > 1024)
  40. {
  41. return;
  42. }
  43. if (this.pool.Count > 10) // 这里不需要太大,其实Kcp跟Tcp,这里1就足够了
  44. {
  45. return;
  46. }
  47. memoryBuffer.Seek(0, SeekOrigin.Begin);
  48. memoryBuffer.SetLength(0);
  49. this.pool.Enqueue(memoryBuffer);
  50. }
  51. // public virtual void Dispose()
  52. // {
  53. // this.Id = 0;
  54. // }
  55. public abstract void Dispose();
  56. public abstract void Update();
  57. public abstract void Remove(long id, int error = 0);
  58. public abstract bool IsDisposed();
  59. // public bool IsDisposed()
  60. // {
  61. // return this.Id == 0;
  62. // }
  63. public abstract void Create(long id, IPEndPoint ipEndPoint, string address);
  64. //public abstract void Send(long channelId, MemoryStream memoryBuffer);
  65. public abstract void Send(long channelId, long actorId, MemoryStream stream);
  66. public virtual (uint, uint) GetChannelConn(long channelId)
  67. {
  68. throw new Exception($"default conn throw Exception! {channelId}");
  69. }
  70. public virtual void ChangeAddress(long channelId, IPEndPoint ipEndPoint)
  71. {
  72. }
  73. // localConn放在低32bit
  74. private long acceptIdGenerater = 1;
  75. public long CreateAcceptChannelId(uint localConn)
  76. {
  77. return (++this.acceptIdGenerater << 32) | localConn;
  78. }
  79. // localConn放在低32bit
  80. private long connectIdGenerater = int.MaxValue;
  81. public long CreateConnectChannelId(uint localConn)
  82. {
  83. return (--this.connectIdGenerater << 32) | localConn;
  84. }
  85. public uint CreateRandomLocalConn()
  86. {
  87. return (1u << 30) | RandomHelper.RandUInt32();
  88. }
  89. protected void OnAccept(long channelId, IPEndPoint ipEndPoint)
  90. {
  91. this.AcceptCallback.Invoke(channelId, ipEndPoint);
  92. }
  93. }
  94. }