Example #1
0
        /// <summary>
        /// 释放资源
        /// </summary>
        public override void Dispose()
        {
            if (IsDisposed == 0)
            {
                Monitor.Enter(SocketLock);
                try
                {
                    if (IsDisposed == 0)
                    {
                        IsDisposed = 1;
                        closeSocket();

                        if (CheckTimer != null)
                        {
                            CheckTimer.Free(this);
                            ClientCheckTimer.FreeNotNull(CheckTimer);
                        }
                        outputSerializer.Free();
                        if (outputJsonSerializer != null)
                        {
                            outputJsonSerializer.Free();
                        }
                        Buffer.Free();
                    }
                }
                finally { Monitor.Exit(SocketLock); }
            }
        }
Example #2
0
        /// <summary>
        /// 获取 TCP 服务客户端套接字
        /// </summary>
        /// <returns></returns>
        protected bool getSocket()
        {
            if (Socket != null)
            {
                return(true);
            }
            Socket = new Socket(IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            bool isVerifyMethod = false;

            try
            {
#if !MONO
                Socket.ReceiveBufferSize = Socket.SendBufferSize = Buffer.Length;
#endif
                Socket.Connect(IpAddress, Port);
                if (isVerifyMethod = CallVerifyMethod())
                {
                    if (Attribute.GetCheckSeconds > 0 && CheckTimer == null)
                    {
                        ClientCheckTimer checkTimer = new ClientCheckTimer(this, Math.Max(Attribute.GetCheckSeconds, 1));
                        if (Interlocked.CompareExchange(ref CheckTimer, checkTimer, null) == null)
                        {
                            if (IsDisposed == 0)
                            {
                                checkTimer.AppendTaskArray();
                            }
                            else
                            {
                                return(isVerifyMethod = false);
                            }
                        }
                    }
                    return(true);
                }
            }
            finally
            {
                if (!isVerifyMethod)
                {
                    closeSocket();
                }
            }
            return(false);
        }
Example #3
0
        /// <summary>
        /// 获取 TCP 服务客户端套接字
        /// </summary>
        /// <returns></returns>
        protected bool getSocket()
        {
            if (Socket != null)
            {
                return(true);
            }
            Socket = new Socket(IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            bool isVerifyMethod = false;

            try
            {
#if !MONO
                Socket.ReceiveBufferSize = Socket.SendBufferSize = Buffer.Length;
#endif
                Socket.Connect(IpAddress, Port);
                if (isVerifyMethod = CallVerifyMethod())
                {
                    if (Attribute.GetCheckSeconds > 0 && CheckTimer == null)
                    {
                        CheckTimer = ClientCheckTimer.Get(Attribute.GetCheckSeconds);
                        if (IsDisposed == 0)
                        {
                            CheckTimer.Push(this);
                        }
                        else
                        {
                            ClientCheckTimer.Free(ref CheckTimer);
                            return(isVerifyMethod = false);
                        }
                    }
                    return(true);
                }
            }
            finally
            {
                if (!isVerifyMethod)
                {
                    closeSocket();
                }
            }
            return(false);
        }