Beispiel #1
0
        /// <summary>
        /// 接受客户端连接
        /// </summary>
        private void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                if (SocketConfig.Limit != 0 && SocketConfig.ClientList.Count > SocketConfig.Limit - 1)
                {
                    OnSystemEvent(ServiceType.Limit, string.Format("超出连接限制:{0},拒绝连接。", SocketConfig.Limit));
                    return;
                }
                if (SocketConfig.ClientList.ToList().FindAll(c => c.ConnectTime > DateTime.Now.AddMinutes(-10)).Count >
                    SocketConfig.Current - 1)
                {
                    OnSystemEvent(ServiceType.Current, string.Format("限定时间内连接数超出限制:{0},拒绝连接。", SocketConfig.Current));
                    return;
                }
                var socket = socketListener.EndAccept(asyn);
                var client = new SocketPackage(socket, heardLength);
                OnClientConnect(client);
                SocketConfig.ClientList.Add(client);
                client.ClientEvent += Client_ClientEvent;
                client.ConnectTime  = DateTime.Now;
                ClientFinished(client.IPPoint);

                //等待客户端发送来的数据
                var state = new AsynSocketArg();
                state.InitializeState(socket, heardLength, true);
                client.WaitForData(state);
            }
            catch (Exception ex)
            {
                OnSystemEvent(string.Format("客户端连接失败:{0}", ex));
            }
            finally
            {
                allDone.Set();
            }
        }
Beispiel #2
0
 /// <summary>
 /// 抛出单个连接对象,用于消息发送
 /// </summary>
 protected virtual void OnClientConnect(SocketPackage client)
 {
     ClientEvent?.Invoke(client);
 }