NetworkComponent.cs 3.0 KB

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