NetworkComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Base;
  5. namespace Model
  6. {
  7. public abstract class NetworkComponent: Component
  8. {
  9. private AService Service;
  10. private readonly Dictionary<long, Entity> sessions = new Dictionary<long, Entity>();
  11. private readonly Dictionary<string, Entity> adressSessions = new Dictionary<string, Entity>();
  12. protected void Awake(NetworkProtocol protocol)
  13. {
  14. switch (protocol)
  15. {
  16. case NetworkProtocol.TCP:
  17. this.Service = new TService();
  18. break;
  19. case NetworkProtocol.UDP:
  20. this.Service = new UService();
  21. break;
  22. default:
  23. throw new ArgumentOutOfRangeException();
  24. }
  25. }
  26. protected void Awake(NetworkProtocol protocol, string host, int port)
  27. {
  28. switch (protocol)
  29. {
  30. case NetworkProtocol.TCP:
  31. this.Service = new TService(host, port);
  32. break;
  33. case NetworkProtocol.UDP:
  34. this.Service = new UService(host, port);
  35. break;
  36. default:
  37. throw new ArgumentOutOfRangeException();
  38. }
  39. this.StartAccept();
  40. }
  41. private async void StartAccept()
  42. {
  43. while (true)
  44. {
  45. if (this.Id == 0)
  46. {
  47. return;
  48. }
  49. AChannel channel = await this.Service.AcceptChannel();
  50. Entity session = new Entity(EntityType.Session);
  51. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  52. session.AddComponent<MessageComponent, AChannel>(channel);
  53. this.Add(session);
  54. }
  55. }
  56. private void Add(Entity session)
  57. {
  58. this.sessions.Add(session.Id, session);
  59. this.adressSessions.Add(session.GetComponent<MessageComponent>().RemoteAddress, session);
  60. }
  61. public void Remove(long id)
  62. {
  63. Entity session;
  64. if (!this.sessions.TryGetValue(id, out session))
  65. {
  66. return;
  67. }
  68. this.sessions.Remove(id);
  69. this.adressSessions.Remove(session.GetComponent<MessageComponent>().RemoteAddress);
  70. }
  71. public Entity Get(long id)
  72. {
  73. Entity session;
  74. this.sessions.TryGetValue(id, out session);
  75. return session;
  76. }
  77. public Entity Get(string address)
  78. {
  79. Entity session;
  80. if (this.adressSessions.TryGetValue(address, out session))
  81. {
  82. return session;
  83. }
  84. string[] ss = address.Split(':');
  85. int port = int.Parse(ss[1]);
  86. string host = ss[0];
  87. AChannel channel = this.Service.ConnectChannel(host, port);
  88. session = new Entity(EntityType.Session);
  89. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  90. session.AddComponent<MessageComponent, AChannel>(channel);
  91. this.Add(session);
  92. return session;
  93. }
  94. public override void Dispose()
  95. {
  96. if (this.Id == 0)
  97. {
  98. return;
  99. }
  100. base.Dispose();
  101. foreach (Entity entity in this.sessions.Values.ToArray())
  102. {
  103. entity.Dispose();
  104. }
  105. this.Service.Dispose();
  106. }
  107. public void Update()
  108. {
  109. if (this.Service == null)
  110. {
  111. return;
  112. }
  113. this.Service.Update();
  114. }
  115. }
  116. }