| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System.Reflection;
- using System.Text;
- using System.Net.Sockets;
- using Server.MirEnvir;
- namespace Server.MirNetwork
- {
- public class MirStatusConnection
- {
- protected static Envir Envir
- {
- get { return Envir.Main; }
- }
- protected static MessageQueue MessageQueue
- {
- get { return MessageQueue.Instance; }
- }
- public readonly string IPAddress;
- private TcpClient _client;
- private long NextSendTime;
- private bool _disconnecting;
- public bool Connected;
- public bool Disconnecting
- {
- get { return _disconnecting; }
- set
- {
- if (_disconnecting == value) return;
- _disconnecting = value;
- TimeOutTime = Envir.Time + 500;
- }
- }
- public readonly long TimeConnected;
- public long TimeOutTime;
- public MirStatusConnection(TcpClient client)
- {
- try
- {
- IPAddress = client.Client.RemoteEndPoint.ToString().Split(':')[0];
- _client = client;
- _client.NoDelay = true;
- TimeConnected = Envir.Time;
- TimeOutTime = TimeConnected + Settings.TimeOut;
- Connected = true;
- }
- catch (Exception ex)
- {
- MessageQueue.Enqueue(ex);
- }
- }
- private void BeginSend(byte[] data)
- {
- if (!Connected || data.Length == 0) return;
- try
- {
- _client.Client.BeginSend(data, 0, data.Length, SocketFlags.None, SendData, null);
- }
- catch
- {
- Disconnecting = true;
- }
- }
- private void SendData(IAsyncResult result)
- {
- try
- {
- _client.Client.EndSend(result);
- }
- catch
- { }
- }
- public void Process()
- {
- try
- {
- if (_client == null || !_client.Connected)
- {
- Disconnect();
- return;
- }
- if (Envir.Time > TimeOutTime || Disconnecting)
- {
- Disconnect();
- return;
- }
- if (Envir.Time > NextSendTime)
- {
- NextSendTime = Envir.Time + 10000;
- string output = string.Format("c;/NoName/{0}/CrystalM2/{1}//;", Envir.PlayerCount,
- Assembly.GetCallingAssembly().GetName().Version);
- BeginSend(Encoding.ASCII.GetBytes(output));
- }
- }
- catch (Exception ex)
- {
- MessageQueue.Enqueue(ex);
- }
- }
- public void Disconnect()
- {
- try
- {
- if (!Connected) return;
- Connected = false;
- lock (Envir.StatusConnections)
- Envir.StatusConnections.Remove(this);
- if (_client != null) _client.Client.Dispose();
- _client = null;
- }
- catch (Exception ex)
- {
- MessageQueue.Enqueue(ex);
- }
- }
- public void SendDisconnect()
- {
- Disconnecting = true;
- }
- }
- }
|