示例#1
0
        public bool Init(int count, string ip, int port, string serverName)
        {
            bool result;

            if (null != this.semaphoreClients)
            {
                LogManager.WriteLog(LogTypes.Error, "不正确的重复调用函数GameDbClientPool.Init(int count, string ip, int port, string serverName)", null, true);
                result = false;
            }
            else
            {
                this.ServerName       = serverName;
                this._InitCount       = count;
                this.ItemCount        = count;
                this.RemoteIP         = ip;
                this.RemotePort       = port;
                this.semaphoreClients = new Semaphore(0, count);
                for (int i = 0; i < count; i++)
                {
                    TCPClient tcpClient = new TCPClient
                    {
                        RootWindow = this.RootWindow,
                        ListIndex  = this.ItemCount,
                        NoDelay    = false
                    };
                    this.ErrorClientStack.Push(tcpClient);
                    this.ErrCount++;
                    try
                    {
                        this.RootWindow.AddDBConnectInfo(this.ItemCount, string.Format("{0}, 准备连接到{1}: {2}{3}", new object[]
                        {
                            this.ItemCount,
                            this.ServerName,
                            this.RemoteIP,
                            this.RemotePort
                        }));
                    }
                    catch (Exception ex)
                    {
                    }
                }
                result = this.Supply();
            }
            return(result);
        }
示例#2
0
 public void Clear()
 {
     try
     {
         lock (this.pool)
         {
             for (int i = 0; i < this.pool.Count; i++)
             {
                 TCPClient tcpClient = this.pool.ElementAt(i);
                 tcpClient.Disconnect();
             }
             this.pool.Clear();
         }
     }
     catch
     {
     }
 }
示例#3
0
 public void Push(TCPClient tcpClient)
 {
     if (!tcpClient.IsConnected())
     {
         lock (this.pool)
         {
             this.ErrCount++;
         }
     }
     else
     {
         lock (this.pool)
         {
             this.pool.Enqueue(tcpClient);
         }
         this.semaphoreClients.Release();
     }
 }
示例#4
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);
        }
示例#5
0
        /// <summary>
        /// 压入一个连接
        /// </summary>
        /// <param name="tcpClient"></param>
        /// <returns></returns>
        public void Push(TCPClient tcpClient)
        {
            //如果是已经无效的连接,则不再放入缓存池
            if (!tcpClient.IsConnected())
            {
                lock (this.pool)
                {
                    ErrCount++;
                }

                return;
            }

            lock (this.pool)
            {
                this.pool.Enqueue(tcpClient);
            }

            this.semaphoreClients.Release();
        }
示例#6
0
        /// <summary>
        ///  补充断开的连接
        /// </summary>
        public bool Supply()
        {
            lock (this.pool)
            {
                if (ErrCount <= 0)
                {
                    return(true);
                }

                DateTime now = TimeUtil.NowDateTime();
                if ((now - LastConnectErrorTime).TotalSeconds < 10)
                {
                    return(false);
                }

                if (ErrCount > 0)
                {
                    while (ErrorClientStack.Count > 0)
                    {
                        TCPClient tcpClient = ErrorClientStack.Pop();

                        try
                        {
                            tcpClient.Connect(RemoteIP, RemotePort, ServerName);
                            this.pool.Enqueue(tcpClient);
                            ErrCount--;

                            this.semaphoreClients.Release();
                        }
                        catch (Exception)
                        {
                            LastConnectErrorTime = now;
                            ErrorClientStack.Push(tcpClient);
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
示例#7
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);
        }
示例#8
0
        /// <summary>
        /// 初始化连接池
        /// </summary>
        /// <param name="count"></param>
        public void Init(int count, string ip, int port, string serverName)
        {
            ServerName = serverName;

            _InitCount            = count;
            ItemCount             = 0;
            RemoteIP              = ip;
            RemotePort            = port;
            this.semaphoreClients = new Semaphore(count, count);
            for (int i = 0; i < count; i++)
            {
                TCPClient tcpClient = new TCPClient()
                {
                    RootWindow = RootWindow, ListIndex = ItemCount
                };

                RootWindow.AddDBConnectInfo(ItemCount, string.Format("{0}, 准备连接到{1}: {2}{3}", ItemCount, ServerName, RemoteIP, RemotePort));

                tcpClient.Connect(RemoteIP, RemotePort, ServerName);
                this.pool.Enqueue(tcpClient);
                ItemCount++;
            }
        }
示例#9
0
        /// <summary>
        /// 初始化连接池
        /// </summary>
        /// <param name="count"></param>
        public bool Init(int count, string ip, int port, string serverName)
        {
            if (null != semaphoreClients)
            {
                LogManager.WriteLog(LogTypes.Error, "不正确的重复调用函数GameDbClientPool.Init(int count, string ip, int port, string serverName)");
                return(false);
            }

            ServerName = serverName;

            _InitCount            = count;
            ItemCount             = count;
            RemoteIP              = ip;
            RemotePort            = port;
            this.semaphoreClients = new Semaphore(0, count);

            for (int i = 0; i < count; i++)
            {
                TCPClient tcpClient = new TCPClient()
                {
                    RootWindow = RootWindow, ListIndex = ItemCount, NoDelay = false
                };
                ErrorClientStack.Push(tcpClient);
                ErrCount++;

                try
                {
                    RootWindow.AddDBConnectInfo(ItemCount, string.Format("{0}, 准备连接到{1}: {2}{3}", ItemCount, ServerName, RemoteIP, RemotePort));
                }
                catch (System.Exception ex)
                {
                }
            }

            return(Supply());
        }
示例#10
0
        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);
        }