Esempio n. 1
0
        //关闭连接进来的Socket
        public void CloseClientSocket(UserTokenEventArgs userToken)
        {
            if (userToken != null)
            {
                if (userToken.Socket == null)
                {
                    return;
                }
                try
                {
                    if (this.customizeHander != null && !userToken.CurrentUserID.IsNullEmpty())
                    {
                        customizeHander.ClientClose(userToken, userToken.CurrentUserID);
                    }

                    EventHandler <UserTokenEventArgs> handler = OnClientDisconnect;   // 如果订户事件将为空(null)
                    if ((handler != null) && userToken.Socket != null)
                    {
                        handler(this, userToken);//抛出连接断开事件
                    }
                    if (userToken.Socket.Connected)
                    {
                        userToken.Socket.Shutdown(SocketShutdown.Both);
                    }
                    userToken.Socket.Close();
                    userToken.Socket = null;
                    userToken.SetInit();
                    this.maxNumberAcceptedClients.Release();
                    lock (recvStackPool)
                    {
                        recvStackPool.Push(userToken.ReceiveSocketAsyncEventArgs);  // 释放以使它们可以被其他客户端重新利用,总数不变
                    }
                }
                // 抛出客户处理已经被关闭
                catch (ObjectDisposedException)
                {
                    Console.WriteLine("关闭对象已经被释放");
                }
                catch (SocketException)
                {
                }
                catch (Exception exception_debug)
                {
                    Console.WriteLine("关闭Socket调试:" + exception_debug.ToString());
                    //throw exception_debug;
                }
                finally
                {
                    if (connectedSocketsCount > 0) //减少连接到服务器的socket计数器
                    {
                        Interlocked.Decrement(ref connectedSocketsCount);
                    }
                    //this.UserDic.RemoveByValue(userToken);
                    if (!userToken.CurrentUserID.IsNullEmpty())
                    {
                        this.UserDic.Remove(userToken.CurrentUserID);
                    }
                }
            }
        }
Esempio n. 2
0
        public void RaiseDisconnectedEvent(UserTokenEventArgs userToken)//引发断开连接事件
        {
            if (null != userToken && userToken.UserSocket != null)
            {
                try
                {
                    EventHandler <UserTokenEventArgs> handler = OnClientDisconnect;//最先抛出事件
                    // 如果订户事件将为空(null)
                    if (handler != null)
                    {
                        handler(this, userToken);//抛出连接断开事件
                    }
                    if (this.customizeHander != null && !userToken.ConnectedId.IsNullEmpty())
                    {
                        customizeHander.ClientClose(userToken);
                    }
                    userToken.Reset();//方便下一次使用

                    if (userToken.UserSocket.Connected)
                    {
                        try
                        {
                            userToken.UserSocket.Shutdown(SocketShutdown.Both);
                        }
                        catch { }
                    }

                    userToken.UserSocket.Close();
                    userToken.UserSocket = null;
                    Interlocked.Decrement(ref connectedSocketsCount);
                    maxNumberAcceptedClients.Release();
                    this.UserDic.Remove(userToken.ConnectedId);
                    recvStackPool.Push(userToken.RecvSocketAsyncEventArgs);// 释放以使它们可以被其他客户端重新利用,总数不变
                }
                catch (ObjectDisposedException)
                {
                    // 抛出客户处理已经被关闭
                }
                catch (Exception ex)
                {
                    RaiseErrorEvent(userToken, ex);
                }
                finally
                {
                }
            }
        }