NetworkComponent.cs 2.7 KB

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