示例#1
0
 /// <summary>
 /// 初始化连接端
 /// </summary>
 public void InitClient()
 {
     for (int i = 0; i < nettyClienStates.Length; i++)
     {
         nettyClienStates[i] = new NettyClientState()
         {
             Address = Address, ClientFlage = ClientFlage, CreateTick = DateTime.Now.Ticks, State = Init
         };
     }
 }
示例#2
0
        /// <summary>
        /// 验证单个客户端
        /// </summary>
        /// <param name="clientState"></param>
        /// <returns></returns>
        private async Task ValidateClient(NettyClientState clientState)
        {
            bool isAll = false;//是否应该验证所有连接

            if (clientState.Client.IsUnConnect)
            {
                clientState.State = Close;
            }
            if (clientState.State == Open)
            {
                if (clientState.Client.IsUnConnect)
                {
                    Logger.Singleton.WarnFormat("Manager验证客户端连接异常,服务端IP{0},端口{1}", clientState.Address.Host, clientState.Address.Port);
                    await clientState.Client.Close();

                    clientState.State = Close; //关闭的网络是放在最后启动的
                    isAll             = true;  //网络已经异常,验证所有连接
                }
                else if (clientState.Client.IsConnected)
                {
                    clientState.State = Free;
                }
                else if (ClientSettings.IsConnectTimeout)
                {
                    if ((DateTime.Now.Ticks - clientState.Client.ConnecTime) / MTikcs > ClientSettings.TimeOut)
                    {
                        Logger.Singleton.WarnFormat("客户端连接超时,服务端IP{0},端口{1}", clientState.Address.Host, clientState.Address.Port);
                        if (!clientState.IsConnected)
                        {
                            await clientState.Client.Close();

                            clientState.State = Close; //关闭的网络是放在最后启动的
                        }
                        isAll = true;                  //网络已经异常,验证所有连接
                    }
                }
            }
            if (isAll)
            {
                await  CheckAll();
            }
        }
示例#3
0
 /// <summary>
 /// 异步验证客户端连接
 /// </summary>
 /// <param name="clientState"></param>
 private void CheckConnect(NettyClientState clientState)
 {
     bagClients.Add(clientState);
     if (!isCheckClient)
     {
         isCheckClient = true;
         Task.Factory.StartNew(async() =>
         {
             NettyClientState client = null;
             while (!bagClients.IsEmpty)
             {
                 if (bagClients.TryTake(out client))
                 {
                     await ValidateClient(client);
                 }
             }
             isCheckClient = false;
         });
     }
 }
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="client"></param>
 /// <param name="data"></param>
 private void Send(NettyClientState client, byte[] data, int commit = 0)
 {
     if (client != null && client.IsConnected)
     {
         client.Client.Send(data);
         client.LastTick = DateTime.Now.Ticks;
         Interlocked.Increment(ref client.SubmitCount);
         Console.WriteLine("发送数据:" + data.Length);
     }
     else
     {
         if (commit == 0)
         {
             SubmitAsync(client.ClientFlage, data);
         }
         else if (commit == 1)
         {
             Push(client.ClientFlage, data);
         }
     }
 }
示例#5
0
        /// <summary>
        /// 找到一个合适的连接客户端
        /// </summary>
        /// <returns></returns>
        public async Task <NettyClientState> FindBestAsync()
        {
            double minCount        = double.MaxValue; //需要找最小的
            int    minTimeIndex    = -1;              //从头找
            int    minCountIndex   = -1;
            int    firstInitIndex  = -1;
            int    firstCloseIndex = -1;

            int len = nettyClienStates.Length;

            for (int i = 0; i < len; i++)
            {
                var c = nettyClienStates[i];

                if (c.State == Free && c.IsConnected)
                {
                    c.State = Use;
                    return(c);//有空闲的直接返回
                }
                else if (c.State == Init)
                {
                    //初始化预留,这样一定有一个预留的客户端连接
                    if (firstInitIndex == -1)
                    {
                        c.Client             = new NettyClient();
                        dicState[c.Client]   = c;
                        c.Client.DataNotify += Client_DataNotify;
                        await c.Client.Start(c.Address.Host, c.Address.Port);

                        c.State        = Open;
                        firstInitIndex = i;
                    }
                }
                else if (c.State == Open)
                {
                    //通知服务端监视线程验证是否无法连接
                    CheckConnect(c);
                }
                else if (c.State == Close)
                {
                    if (firstCloseIndex == -1)
                    {
                        firstCloseIndex = i;
                    }
                }
                else if (c.State == Use)
                {
                    if (minCount > c.CommitRate)
                    {
                        //找到最小包
                        minCount      = c.CommitRate;
                        minCountIndex = i;
                    }
                }
                else if (c.State == Free && !c.IsConnected)
                {
                    await c.Client.Close();

                    c.State = Close;
                }
            }
            //
            NettyClientState findC = null;

            if (minCountIndex != -1)
            {
                //数据提交最少的
                findC = nettyClienStates[minCountIndex];
            }
            else if (firstInitIndex != -1)
            {
                //初始化的
                findC = nettyClienStates[firstInitIndex];
            }
            else if (firstCloseIndex != -1)
            {
                findC = nettyClienStates[firstCloseIndex];
                await findC.Client.Start(findC.Address.Host, findC.Address.Port);

                findC.State = Open;
            }
            else if (minTimeIndex != -1)
            {
                //最早一次提交数据的
                findC = nettyClienStates[minTimeIndex];
            }
            return(findC);
        }