AService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. namespace ET
  5. {
  6. public abstract class AService: IDisposable
  7. {
  8. public ServiceType ServiceType { get; protected set; }
  9. public ThreadSynchronizationContext ThreadSynchronizationContext;
  10. // localConn放在低32bit
  11. private long connectIdGenerater = int.MaxValue;
  12. public long CreateConnectChannelId(uint localConn)
  13. {
  14. return (--this.connectIdGenerater << 32) | localConn;
  15. }
  16. public uint CreateRandomLocalConn(Random random)
  17. {
  18. return (1u << 30) | random.RandUInt32();
  19. }
  20. #region 网络线程
  21. // localConn放在低32bit
  22. private long acceptIdGenerater = 1;
  23. public long CreateAcceptChannelId(uint localConn)
  24. {
  25. return (++this.acceptIdGenerater << 32) | localConn;
  26. }
  27. public abstract void Update();
  28. public abstract void Remove(long id);
  29. public abstract bool IsDispose();
  30. protected abstract void Get(long id, IPEndPoint address);
  31. public abstract void Dispose();
  32. protected abstract void Send(long channelId, long actorId, MemoryStream stream);
  33. protected void OnAccept(long channelId, IPEndPoint ipEndPoint)
  34. {
  35. #if NET_THREAD
  36. ThreadSynchronizationContext.Instance.Post(() =>
  37. {
  38. this.AcceptCallback.Invoke(channelId, ipEndPoint);
  39. });
  40. #else
  41. this.AcceptCallback.Invoke(channelId, ipEndPoint);
  42. #endif
  43. }
  44. public void OnRead(long channelId, MemoryStream memoryStream)
  45. {
  46. #if NET_THREAD
  47. ThreadSynchronizationContext.Instance.Post(() =>
  48. {
  49. this.ReadCallback.Invoke(channelId, memoryStream);
  50. });
  51. #else
  52. this.ReadCallback.Invoke(channelId, memoryStream);
  53. #endif
  54. }
  55. public void OnError(long channelId, int e)
  56. {
  57. this.Remove(channelId);
  58. #if NET_THREAD
  59. ThreadSynchronizationContext.Instance.Post(() =>
  60. {
  61. this.ErrorCallback?.Invoke(channelId, e);
  62. });
  63. #else
  64. this.ErrorCallback?.Invoke(channelId, e);
  65. #endif
  66. }
  67. #endregion
  68. #region 主线程
  69. public Action<long, IPEndPoint> AcceptCallback;
  70. public Action<long, int> ErrorCallback;
  71. public Action<long, MemoryStream> ReadCallback;
  72. public void Destroy()
  73. {
  74. #if NET_THREAD
  75. this.ThreadSynchronizationContext.Post(this.Dispose);
  76. #else
  77. this.Dispose();
  78. #endif
  79. }
  80. public void RemoveChannel(long channelId)
  81. {
  82. #if NET_THREAD
  83. this.ThreadSynchronizationContext.Post(() =>
  84. {
  85. this.Remove(channelId);
  86. });
  87. #else
  88. this.Remove(channelId);
  89. #endif
  90. }
  91. public void SendStream(long channelId, long actorId, MemoryStream stream)
  92. {
  93. #if NET_THREAD
  94. this.ThreadSynchronizationContext.Post(() =>
  95. {
  96. this.Send(channelId, actorId, stream);
  97. });
  98. #else
  99. this.Send(channelId, actorId, stream);
  100. #endif
  101. }
  102. public void GetOrCreate(long id, IPEndPoint address)
  103. {
  104. #if NET_THREAD
  105. this.ThreadSynchronizationContext.Post(()=>
  106. {
  107. this.Get(id, address);
  108. });
  109. #else
  110. this.Get(id, address);
  111. #endif
  112. }
  113. #endregion
  114. }
  115. }