NetworkComponent.cs 3.1 KB

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