NetworkComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, Session> sessions = new Dictionary<long, Session>();
  11. private readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
  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. Session session = new Session(channel);
  51. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  52. this.Add(session);
  53. }
  54. }
  55. private void Add(Session session)
  56. {
  57. this.sessions.Add(session.Id, session);
  58. this.adressSessions.Add(session.RemoteAddress, session);
  59. }
  60. public void Remove(long id)
  61. {
  62. Session session;
  63. if (!this.sessions.TryGetValue(id, out session))
  64. {
  65. return;
  66. }
  67. this.sessions.Remove(id);
  68. this.adressSessions.Remove(session.RemoteAddress);
  69. }
  70. public Session Get(long id)
  71. {
  72. Session session;
  73. this.sessions.TryGetValue(id, out session);
  74. return session;
  75. }
  76. public Session Get(string address)
  77. {
  78. Session 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 Session(channel);
  88. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  89. this.Add(session);
  90. return session;
  91. }
  92. public override void Dispose()
  93. {
  94. if (this.Id == 0)
  95. {
  96. return;
  97. }
  98. base.Dispose();
  99. foreach (Session session in this.sessions.Values.ToArray())
  100. {
  101. session.Dispose();
  102. }
  103. this.Service.Dispose();
  104. }
  105. public void Update()
  106. {
  107. if (this.Service == null)
  108. {
  109. return;
  110. }
  111. this.Service.Update();
  112. }
  113. }
  114. }