Exemplo n.º 1
0
        /// <summary>
        /// Process the accept for the socket listener.
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed accept operation.</param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //Socket s = e.AcceptSocket;
            TMSKSocket s = new TMSKSocket(e.AcceptSocket);

            //检查IP名单
            bool disableConnect = false;

            if (EnabledIPListFilter)
            {
                lock (IPWhiteList)
                {
                    if (EnabledIPListFilter && null != s && null != s.RemoteEndPoint)
                    {
                        IPEndPoint remoteIPEndPoint = (s.RemoteEndPoint as IPEndPoint);
                        if (null != remoteIPEndPoint && null != remoteIPEndPoint.Address)
                        {
                            string remoteIP = remoteIPEndPoint.Address.ToString();
                            if (!string.IsNullOrEmpty(remoteIP) && !IPWhiteList.ContainsKey(remoteIP))
                            {
                                disableConnect = true;
                            }
                        }
                    }
                }
            }

            //是否不再接受新的用户
            if (DontAccept || disableConnect)
            {
                try
                {
                    if (disableConnect)
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是客户端IP处于IP过滤中,直接关闭连接:{1}", s.RemoteEndPoint, ConnectedSocketsCount));
                    }
                    else if (DontAccept)
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是服务器端处于不接受新连接状态,直接关闭连接:{1}", s.RemoteEndPoint, ConnectedSocketsCount));
                    }
                }
                catch (Exception)
                {
                }

                try
                {
                    s.Shutdown(SocketShutdown.Both);
                }
                catch (Exception)
                {
                    // Throws if client process has already closed.
                }

                try
                {
                    s.Close();
                }
                catch (Exception)
                {
                }

                // Accept the next connection request.
                this.StartAccept(e);
                return;
            }

            byte[] inOptionValues = new byte[sizeof(uint) * 3];
            BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
            BitConverter.GetBytes((uint)120000).CopyTo(inOptionValues, sizeof(uint));
            BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, sizeof(uint) * 2);
            (s as TMSKSocket).IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);

            SocketAsyncEventArgs readEventArgs = null;

            if (GameManager.FlagOptimizeThreadPool3)
            {
                readEventArgs = s.PopReadSocketAsyncEventArgs(); //线程安全的操作
            }
            else
            {
                readEventArgs = this.readPool.Pop();
            }
            if (null == readEventArgs)
            {
                try
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是readPool内的缓存不足,直接关闭连接:{1}", s.RemoteEndPoint, ConnectedSocketsCount));
                }
                catch (Exception)
                {
                }

                try
                {
                    s.Shutdown(SocketShutdown.Both);
                }
                catch (Exception)
                {
                    // Throws if client process has already closed.
                }

                try
                {
                    s.Close();
                }
                catch (Exception)
                {
                }

                // Accept the next connection request.
                this.StartAccept(e);
                return;
            }

            //增加计数器
            //Interlocked.Increment(ref this.numConnectedSockets);

            // Get the socket for the accepted client connection and put it into the
            // ReadEventArg object user token.
            (readEventArgs.UserToken as AsyncUserToken).CurrentSocket = s;

            Global._SendBufferManager.Add(s);

            AddSocket(s);

            try
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 当前总共: {1}", s.RemoteEndPoint, ConnectedSocketsCount));
            }
            catch (Exception)
            {
            }

            /// 连接成功通知函数
            if (null != SocketConnected)
            {
                SocketConnected(this, readEventArgs);
            }

            // As soon as the client is connected, post a receive to the connection.
            Boolean willRaiseEvent = _ReceiveAsync(readEventArgs);

            if (!willRaiseEvent)
            {
                this.ProcessReceive(readEventArgs);
            }

            // Accept the next connection request.
            this.StartAccept(e);
        }
Exemplo n.º 2
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            TMSKSocket s = new TMSKSocket(e.AcceptSocket);

            s.SetAcceptIp();
            bool disableConnect = false;
            bool?inIpWriteList  = null;

            if (this.EnabledIPListFilter)
            {
                lock (this.IPWhiteList)
                {
                    if (this.EnabledIPListFilter && s != null && null != s.RemoteEndPoint)
                    {
                        IPEndPoint remoteIPEndPoint = s.RemoteEndPoint as IPEndPoint;
                        if (remoteIPEndPoint != null && null != remoteIPEndPoint.Address)
                        {
                            string remoteIP = remoteIPEndPoint.Address.ToString();
                            if (!string.IsNullOrEmpty(remoteIP) && !this.IPWhiteList.ContainsKey(remoteIP))
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是客户端IP处于IP过滤中:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true);
                                inIpWriteList = new bool?(false);
                            }
                            else
                            {
                                inIpWriteList = new bool?(true);
                            }
                        }
                    }
                }
            }
            if (IPStatisticsManager.getInstance().GetIPInBeOperation(s, IPOperaType.BanConnect))
            {
                disableConnect = true;
            }
            if (this.DontAccept || disableConnect)
            {
                try
                {
                    if (disableConnect)
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是客户端IP处于IP过滤中,直接关闭连接:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true);
                    }
                    else if (this.DontAccept)
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是服务器端处于不接受新连接状态,直接关闭连接:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true);
                    }
                }
                catch (Exception)
                {
                }
                try
                {
                    s.Shutdown(SocketShutdown.Both);
                }
                catch (Exception)
                {
                }
                try
                {
                    s.Close(30);
                }
                catch (Exception)
                {
                }
                this.StartAccept(e);
            }
            else
            {
                byte[] inOptionValues = new byte[12];
                BitConverter.GetBytes(1U).CopyTo(inOptionValues, 0);
                BitConverter.GetBytes(120000U).CopyTo(inOptionValues, 4);
                BitConverter.GetBytes(5000U).CopyTo(inOptionValues, 8);
                s.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
                LingerOption lingerOption = new LingerOption(true, 10);
                s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
                SocketAsyncEventArgs readEventArgs = null;
                readEventArgs = s.PopReadSocketAsyncEventArgs();
                if (null == readEventArgs)
                {
                    try
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是readPool内的缓存不足,直接关闭连接:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true);
                    }
                    catch (Exception)
                    {
                    }
                    try
                    {
                        s.Shutdown(SocketShutdown.Both);
                    }
                    catch (Exception)
                    {
                    }
                    try
                    {
                        s.Close(30);
                    }
                    catch (Exception)
                    {
                    }
                    this.StartAccept(e);
                }
                else
                {
                    (readEventArgs.UserToken as AsyncUserToken).CurrentSocket = s;
                    Global._SendBufferManager.Add(s);
                    this.AddSocket(s);
                    try
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 当前总共: {1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true);
                    }
                    catch (Exception)
                    {
                    }
                    if (null != this.SocketConnected)
                    {
                        this.SocketConnected(this, readEventArgs);
                    }
                    s.session.InIpWhiteList = inIpWriteList;
                    s.session.SetSocketTime(0);
                    if (!this._ReceiveAsync(readEventArgs))
                    {
                        this.ProcessReceive(readEventArgs);
                    }
                    this.StartAccept(e);
                }
            }
        }