NetworkComponent.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. namespace ET
  5. {
  6. public abstract class NetworkComponent : Entity
  7. {
  8. protected AService Service;
  9. public readonly Dictionary<long, Session> Sessions = new Dictionary<long, Session>();
  10. public IMessagePacker MessagePacker { get; set; }
  11. public IMessageDispatcher MessageDispatcher { get; set; }
  12. public void Awake(NetworkProtocol protocol)
  13. {
  14. switch (protocol)
  15. {
  16. case NetworkProtocol.KCP:
  17. this.Service = new KService() { Parent = this };
  18. break;
  19. case NetworkProtocol.TCP:
  20. this.Service = new TService() { Parent = this };
  21. break;
  22. case NetworkProtocol.WebSocket:
  23. this.Service = new WService() { Parent = this };
  24. break;
  25. }
  26. }
  27. public void Awake(NetworkProtocol protocol, string address)
  28. {
  29. try
  30. {
  31. IPEndPoint ipEndPoint;
  32. switch (protocol)
  33. {
  34. case NetworkProtocol.KCP:
  35. ipEndPoint = NetworkHelper.ToIPEndPoint(address);
  36. this.Service = new KService(ipEndPoint, (channel)=> { this.OnAccept(channel); }) { Parent = this };
  37. break;
  38. case NetworkProtocol.TCP:
  39. ipEndPoint = NetworkHelper.ToIPEndPoint(address);
  40. this.Service = new TService(ipEndPoint, (channel)=> { this.OnAccept(channel); }) { Parent = this };
  41. break;
  42. case NetworkProtocol.WebSocket:
  43. string[] prefixs = address.Split(';');
  44. this.Service = new WService(prefixs, (channel)=> { this.OnAccept(channel); }) { Parent = this };
  45. break;
  46. }
  47. }
  48. catch (Exception e)
  49. {
  50. throw new Exception($"NetworkComponent Awake Error {address}", e);
  51. }
  52. }
  53. public int Count
  54. {
  55. get { return this.Sessions.Count; }
  56. }
  57. public virtual Session OnAccept(AChannel channel)
  58. {
  59. Session session = EntityFactory.CreateWithParent<Session, AChannel>(this, channel);
  60. this.Sessions.Add(session.Id, session);
  61. channel.Start();
  62. return session;
  63. }
  64. public virtual void Remove(long id)
  65. {
  66. Session session;
  67. if (!this.Sessions.TryGetValue(id, out session))
  68. {
  69. return;
  70. }
  71. this.Sessions.Remove(id);
  72. session.Dispose();
  73. }
  74. public Session Get(long id)
  75. {
  76. Session session;
  77. this.Sessions.TryGetValue(id, out session);
  78. return session;
  79. }
  80. /// <summary>
  81. /// 创建一个新Session
  82. /// </summary>
  83. public Session Create(IPEndPoint ipEndPoint)
  84. {
  85. AChannel channel = this.Service.ConnectChannel(ipEndPoint);
  86. Session session = EntityFactory.CreateWithParent<Session, AChannel>(this, channel);
  87. this.Sessions.Add(session.Id, session);
  88. channel.Start();
  89. return session;
  90. }
  91. /// <summary>
  92. /// 创建一个新Session
  93. /// </summary>
  94. public Session Create(string address)
  95. {
  96. AChannel channel = this.Service.ConnectChannel(address);
  97. Session session = EntityFactory.CreateWithParent<Session, AChannel>(this, channel);
  98. this.Sessions.Add(session.Id, session);
  99. channel.Start();
  100. return session;
  101. }
  102. public void Update()
  103. {
  104. if (this.Service == null)
  105. {
  106. return;
  107. }
  108. this.Service.Update();
  109. }
  110. public override void Dispose()
  111. {
  112. if (this.IsDisposed)
  113. {
  114. return;
  115. }
  116. base.Dispose();
  117. this.Service.Dispose();
  118. }
  119. }
  120. }