MirStatusConnection.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Reflection;
  2. using System.Text;
  3. using System.Net.Sockets;
  4. using Server.MirEnvir;
  5. namespace Server.MirNetwork
  6. {
  7. public class MirStatusConnection
  8. {
  9. protected static Envir Envir
  10. {
  11. get { return Envir.Main; }
  12. }
  13. protected static MessageQueue MessageQueue
  14. {
  15. get { return MessageQueue.Instance; }
  16. }
  17. public readonly string IPAddress;
  18. private TcpClient _client;
  19. private long NextSendTime;
  20. private bool _disconnecting;
  21. public bool Connected;
  22. public bool Disconnecting
  23. {
  24. get { return _disconnecting; }
  25. set
  26. {
  27. if (_disconnecting == value) return;
  28. _disconnecting = value;
  29. TimeOutTime = Envir.Time + 500;
  30. }
  31. }
  32. public readonly long TimeConnected;
  33. public long TimeOutTime;
  34. public MirStatusConnection(TcpClient client)
  35. {
  36. try
  37. {
  38. IPAddress = client.Client.RemoteEndPoint.ToString().Split(':')[0];
  39. _client = client;
  40. _client.NoDelay = true;
  41. TimeConnected = Envir.Time;
  42. TimeOutTime = TimeConnected + Settings.TimeOut;
  43. Connected = true;
  44. }
  45. catch (Exception ex)
  46. {
  47. MessageQueue.Enqueue(ex);
  48. }
  49. }
  50. private void BeginSend(byte[] data)
  51. {
  52. if (!Connected || data.Length == 0) return;
  53. try
  54. {
  55. _client.Client.BeginSend(data, 0, data.Length, SocketFlags.None, SendData, null);
  56. }
  57. catch
  58. {
  59. Disconnecting = true;
  60. }
  61. }
  62. private void SendData(IAsyncResult result)
  63. {
  64. try
  65. {
  66. _client.Client.EndSend(result);
  67. }
  68. catch
  69. { }
  70. }
  71. public void Process()
  72. {
  73. try
  74. {
  75. if (_client == null || !_client.Connected)
  76. {
  77. Disconnect();
  78. return;
  79. }
  80. if (Envir.Time > TimeOutTime || Disconnecting)
  81. {
  82. Disconnect();
  83. return;
  84. }
  85. if (Envir.Time > NextSendTime)
  86. {
  87. NextSendTime = Envir.Time + 10000;
  88. string output = string.Format("c;/NoName/{0}/CrystalM2/{1}//;", Envir.PlayerCount,
  89. Assembly.GetCallingAssembly().GetName().Version);
  90. BeginSend(Encoding.ASCII.GetBytes(output));
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. MessageQueue.Enqueue(ex);
  96. }
  97. }
  98. public void Disconnect()
  99. {
  100. try
  101. {
  102. if (!Connected) return;
  103. Connected = false;
  104. lock (Envir.StatusConnections)
  105. Envir.StatusConnections.Remove(this);
  106. if (_client != null) _client.Client.Dispose();
  107. _client = null;
  108. }
  109. catch (Exception ex)
  110. {
  111. MessageQueue.Enqueue(ex);
  112. }
  113. }
  114. public void SendDisconnect()
  115. {
  116. Disconnecting = true;
  117. }
  118. }
  119. }