Example #1
0
 public void Clear()
 {
     lock (this.pool)
     {
         for (int i = 0; i < this.pool.Count; i++)
         {
             TCPClient tcpClient = this.pool.ElementAt(i);
             tcpClient.Disconnect();
         }
         this.pool.Clear();
     }
 }
Example #2
0
        /// <summary>
        /// 获取一个连接
        /// </summary>
        /// <returns></returns>
        public TCPClient Pop()
        {
            TCPClient tcpClient  = null;
            bool      needSupply = false;

            lock (this.pool)
            {
                if (ErrCount >= _InitCount)
                {
                    //首先尝试补充连接,如果失败,则返回null
                    needSupply = true;
                    if (!Supply())
                    {
                        return(null);
                    }
                }
            }

            if (this.semaphoreClients.WaitOne(20000)) //防止无法获取, 阻塞等待
            {
                lock (this.pool)
                {
                    tcpClient = this.pool.Dequeue();

                    if (!tcpClient.ValidateIpPort(RemoteIP, RemotePort))
                    {
                        try
                        {
                            tcpClient.Disconnect();
                            tcpClient.Connect(RemoteIP, RemotePort, tcpClient.ServerName);
                        }
                        catch (System.Exception ex)
                        {
                            ErrCount++;
                            ErrorClientStack.Push(tcpClient);
                            LastConnectErrorTime = TimeUtil.NowDateTime();
                            LogManager.WriteExceptionUseCache(ex.ToString());
                        }
                    }
                }
            }

            return(tcpClient);
        }
Example #3
0
        public TCPClient Pop()
        {
            TCPClient tcpClient = null;

            lock (this.pool)
            {
                if (this.ErrCount >= this._InitCount)
                {
                    if (!this.Supply())
                    {
                        return(null);
                    }
                }
            }
            if (this.semaphoreClients.WaitOne(20000))
            {
                lock (this.pool)
                {
                    tcpClient = this.pool.Dequeue();
                    if (!tcpClient.ValidateIpPort(this.RemoteIP, this.RemotePort))
                    {
                        try
                        {
                            tcpClient.Disconnect();
                            tcpClient.Connect(this.RemoteIP, this.RemotePort, tcpClient.ServerName);
                        }
                        catch (Exception ex)
                        {
                            this.ErrCount++;
                            this.ErrorClientStack.Push(tcpClient);
                            this.LastConnectErrorTime = TimeUtil.NowDateTime();
                            LogManager.WriteExceptionUseCache(ex.ToString());
                        }
                    }
                }
            }
            return(tcpClient);
        }
        public static int RegisterNameToNameServer(int zoneID, string userID, string[] nameAndPingTaiID, int type, int roleID = 0)
        {
            if (GameManager.FlagDisableNameServer || !Global.Flag_NameServer || NameServerConfig == -1)
            {
                return(NameErrorCodes.ErrorSuccess); //未配置名字服务器则允许注册且不到名字服务器验证
            }

            string name = nameAndPingTaiID[0];
            string pingTai;

            if (NameServerConfig == 1)
            {
                pingTai = ServerPingTaiID;
            }
            else if (nameAndPingTaiID.Length == 2)
            {
                pingTai = nameAndPingTaiID[1];
            }
            else
            {
                pingTai = "Global";
            }

            // 原来的检测放到函数外面

            /*int ret = CheckInvalidCharacters(name);
             * if (ret <= 0)
             * {
             *  return NameErrorCodes.ErrorInvalidCharacter;
             * }*/

            if (NameServerConfig < 0)
            {
                if (NameServerConfig == -1)
                {
                    return(NameErrorCodes.ErrorSuccess);        //否则允许注册且不到名字服务器验证
                }
                else if (NameServerConfig == -2)                //如果配置了-2则不允许注册,否则允许注册且不到名字服务器验证
                {
                    return(NameErrorCodes.ErrorServerDisabled); //如果配置了-2则不允许注册
                }
            }
            if (zoneID < 0)
            {
                DataHelper.WriteStackTraceLog(string.Format("注册名字到名字服务器时区号不合法 zoneID={0} userID={1} name={2}", zoneID, userID, name));
                return(NameErrorCodes.ErrorServerDisabled);
            }

            int       ret        = 0;
            TCPClient connection = null;

            try
            {
                connection = new TCPClient()
                {
                    RootWindow = Program.ServerConsole, ListIndex = 100
                };
                NameRegisterData nameRegisterData = new NameRegisterData()
                {
                    Name      = name,
                    PingTaiID = pingTai,
                    ZoneID    = zoneID,
                    UserID    = userID,
                    NameType  = type,
                };

                string         ip;
                NameServerData nameServerData;
                lock (ZoneID2NameServerDict)
                {
                    if (!ZoneID2NameServerDict.TryGetValue(zoneID, out nameServerData))
                    {
                        nameServerData = DefaultServerData;
                    }
                }
                ip = GetIPV4IP(nameServerData);
                if (null == ip)
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("解析名字服务器IP失败 zoneID={0} host={1} NameServerID={2}", zoneID, nameServerData.Host, nameServerData.ID));
                    return(NameErrorCodes.ErrorServerDisabled);
                }

                connection.Connect(ip, nameServerData.Port, "NameServer" + nameServerData.ID + 1000);
                ret = Global.SendToNameServer <NameRegisterData, int>(connection, (int)TCPGameServerCmds.CMD_NAME_REGISTERNAME, nameRegisterData);
                if (ret == 0)
                {
                    ret = NameErrorCodes.ErrorNameHasBeUsed;
                }
            }
            catch (System.Exception ex)
            {
                DataHelper.WriteFormatExceptionLog(ex, string.Format("注册名字到名字服务器时发生错误 zoneID={0} userID={1} name={2}", zoneID, userID, name), false);
                ret = NameErrorCodes.ErrorServerDisabled;
            }

            if (null != connection)
            {
                connection.Disconnect();
            }
            return(ret);
        }