NetModule.static.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using XGame.Framework.Time;
  3. namespace XGame.Framework.Network
  4. {
  5. /// <summary>
  6. /// 单实例,有双实例需求再改
  7. /// </summary>
  8. public partial class NetModule
  9. {
  10. private static INetModule _instance;
  11. //public static INetModule Instance
  12. //{
  13. // get
  14. // {
  15. // Assert.IsNotNull(_instance, $"NetModule 实例为空, 需要先执行NetModule.Init()");
  16. // return _instance;
  17. // }
  18. //}
  19. public static INetModule Init(ITimeModule time, IMsgGenerator generator, INetModuleListener listener)
  20. {
  21. var netModule = new NetModule(time, generator, listener);
  22. netModule.IsDebug = AppManager.IsSessionDebug;
  23. _instance = netModule;
  24. return netModule;
  25. }
  26. /// <summary>
  27. /// 发送请求消息
  28. /// </summary>
  29. /// <param name="msg"></param>
  30. /// <param name="isFilter">过滤参数,默认开启</param>
  31. public static void Request(IMsgRequest msg, bool isFilter = true)
  32. {
  33. Assert.IsNotNull(_instance, $"NetModule 实例为空, 需要先执行NetModule.Init()");
  34. _instance.Send(msg, isFilter);
  35. }
  36. public static void Dispose()
  37. {
  38. if (_instance == null) return;
  39. _instance.Disconnect();
  40. (_instance as IDisposable)?.Dispose();
  41. _instance = null;
  42. }
  43. }
  44. }