NetworkComponent.cs 3.1 KB

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