⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpsipconnection.cs

📁 破解的飞信源代码
💻 CS
字号:
namespace Imps.Client.CommLayer.TcpSipConnection
{
    using Imps.Client.Base;
    using Imps.Client.CommLayer;
    using Imps.Client.CommLayer.Common;
    using Imps.Client.Resource;
    using Imps.Client.Utils;
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Runtime.CompilerServices;

    internal class TcpSipConnection : ThreadSipConnectionBase
    {
        protected Socket _clientSocket;
        private const int KeepAliveInterval = 0x3e8;
        private const int KeepAliveOnOff = 1;
        private const int KeepAliveTime = 0xea60;

        public event EventHandler<FailedEventArgs> ConnectFailed;

        public TcpSipConnection(object context, TcpSipConnectionOption option) : base(context)
        {
            if (option == null)
            {
                throw new ArgumentNullException("option");
            }
            base.Option = option;
        }

        public override void Close()
        {
            if (base.State == ConnectionState.Connected)
            {
                base.Close();
                base.State = ConnectionState.Disconnecting;
                Socket socket = this._clientSocket;
                if (socket != null)
                {
                    try
                    {
                        socket.Shutdown(SocketShutdown.Both);
                        socket.BeginDisconnect(false, new AsyncCallback(this.SocketDisConnected), socket);
                    }
                    catch (Exception exception)
                    {
                        base.State = ConnectionState.Disconnected;
                        this._clientSocket = null;
                        base.LogError(exception);
                    }
                }
            }
        }

        public override void Connect(string host, int port)
        {
            try
            {
                if (this._clientSocket != null)
                {
                    throw new SipConnectionException("试图在已经连接的对象上再次连接!");
                }
                ClientLogger.WriteConnection("TcpSipConnection", 0, "", 0);
                this.CreateClientSocket();
                base.State = ConnectionState.Connecting;
                this._clientSocket.BeginConnect(host, port, new AsyncCallback(this.SocketConnected), this._clientSocket);
            }
            catch (Exception exception)
            {
                this._clientSocket = null;
                base.State = ConnectionState.Disconnected;
                this.RaiseConnectFailed(new FailedEventArgs(exception, GetErrorString(exception)));
            }
        }

        protected void CreateClientSocket()
        {
            this._clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this._clientSocket.set_SendTimeout(this.Option.Timeout * 0x3e8);
        }

        protected static string GetErrorString(Exception ex)
        {
            if (ex is SocketException)
            {
                switch ((ex as SocketException).get_SocketErrorCode())
                {
                    case 0x274d:
                        return StringTable.ClientCommLayerString.ConnectionRefusedErrorText;

                    case 0x2750:
                        return StringTable.ClientCommLayerString.HostDownErrorText;

                    case 0x2751:
                        return StringTable.ClientCommLayerString.HostUnreachableErrorText;

                    case 0x2af9:
                        return StringTable.ClientCommLayerString.HostNotFoundErrorText;

                    case 0x2742:
                        return StringTable.ClientCommLayerString.NetworkDownErrorText;
                }
            }
            return StringTable.ClientCommLayerString.DefaultErrorText;
        }

        private void RaiseConnectFailed(FailedEventArgs argument)
        {
            EventHandler<FailedEventArgs> connectFailed = this.ConnectFailed;
            if (connectFailed != null)
            {
                connectFailed.Invoke(this, argument);
            }
        }

        protected override int ReceiveMessage(byte[] buffer)
        {
            return this._clientSocket.Receive(buffer);
        }

        protected override void SendMessage(byte[] buffer)
        {
            this._clientSocket.Send(buffer);
        }

        protected void SetKeepAlive()
        {
            byte[] dst = new byte[12];
            byte[] buffer2 = new byte[12];
            Buffer.BlockCopy(BitConverter.GetBytes(1), 0, dst, 0, 4);
            Buffer.BlockCopy(BitConverter.GetBytes(0xea60), 0, dst, 4, 4);
            Buffer.BlockCopy(BitConverter.GetBytes(0x3e8), 0, dst, 8, 4);
            this._clientSocket.IOControl(0x98000004, dst, buffer2);
        }

        private void SocketConnected(IAsyncResult ar)
        {
            try
            {
                this._clientSocket.EndConnect(ar);
                this.SetKeepAlive();
                base.State = ConnectionState.Connected;
            }
            catch (Exception exception)
            {
                this._clientSocket = null;
                base.State = ConnectionState.Disconnected;
                this.RaiseConnectFailed(new FailedEventArgs(exception, GetErrorString(exception)));
            }
        }

        private void SocketDisConnected(IAsyncResult ar)
        {
            try
            {
                Socket socket = this._clientSocket;
                if (socket != null)
                {
                    socket.EndDisconnect(ar);
                    ((IDisposable) socket).Dispose();
                }
            }
            catch (Exception exception)
            {
                base.LogError(exception);
            }
            finally
            {
                this._clientSocket = null;
                base.State = ConnectionState.Disconnected;
            }
        }

        public override string ToString()
        {
            return (StringTable.ClientCommLayerString.Tcp + " - " + Tools.GetConnectionStateString(base.State));
        }

        public override IPEndPoint LocalEndPoint
        {
            get
            {
                if (base.State != ConnectionState.Connected)
                {
                    return null;
                }
                return (this._clientSocket.LocalEndPoint as IPEndPoint);
            }
        }

        public TcpSipConnectionOption Option
        {
            get
            {
                return (base.Option as TcpSipConnectionOption);
            }
        }

        public override IPEndPoint RemoteEndPoint
        {
            get
            {
                if (base.State != ConnectionState.Connected)
                {
                    return null;
                }
                return (this._clientSocket.RemoteEndPoint as IPEndPoint);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -