/// <summary>
        /// 移除元素
        /// 如果元素不存在而返回false
        /// </summary>
        /// <param name="socketAsync">元素</param>
        /// <returns></returns>
        internal bool Remove(SocketAsync <T> socketAsync)
        {
            if (socketAsync == null)
            {
                return(false);
            }
            var key = socketAsync.GetHashCode();

            return(this.dic.TryRemove(key, out socketAsync));
        }
        /// <summary>
        /// 添加元素
        /// 如果已包含此元素则返回false,同时不会增加记录
        /// </summary>
        /// <param name="socketAsync">元素</param>
        /// <returns></returns>
        internal bool Add(SocketAsync <T> socketAsync)
        {
            if (socketAsync == null)
            {
                return(false);
            }
            var key = socketAsync.GetHashCode();

            return(dic.TryAdd(key, socketAsync));
        }
Beispiel #3
0
 /// <summary>
 /// 关闭并复用客户端对象
 /// </summary>
 /// <param name="client">客户端对象</param>
 public bool CloseClient(SocketAsync <T> client)
 {
     if (this.AliveClients.Remove(client))
     {
         this.OnDisconnect(client);
         client.CloseSocket();
         this.clientPool.Add(client);
         return(true);
     }
     return(false);
 }
Beispiel #4
0
 /// <summary>
 /// 添加SocketAsync
 /// </summary>
 /// <param name="SocketAsync">SocketAsync对象</param>
 public void Add(SocketAsync <T> SocketAsync)
 {
     this.bag.Add(SocketAsync);
 }
 /// <summary>
 /// 通过使用默认的相等比较器对值进行比较得到集合的差集。
 /// </summary>
 /// <param name="client">客户端</param>
 /// <returns></returns>
 public IEnumerable <SocketAsync <T> > Except(SocketAsync <T> client)
 {
     return(this.Except(new[] { client }));
 }
Beispiel #6
0
 /// <summary>
 /// 当客户端连接时,将触发此方法
 /// </summary>
 /// <param name="client">客户端</param>
 protected virtual void OnConnect(SocketAsync <T> client)
 {
 }
Beispiel #7
0
 /// <summary>
 /// 客户端socket关闭
 /// </summary>
 /// <param name="client">客户端</param>
 private void OnClientDisconnect(SocketAsync <T> client)
 {
     this.CloseClient(client);
 }
Beispiel #8
0
 /// <summary>
 /// 当收到到数据包时,将触发此方法
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="packet">数据包</param>
 protected virtual void OnRecvComplete(SocketAsync <T> client, T packet)
 {
 }
Beispiel #9
0
 /// <summary>
 /// 使用Task来处理OnRecvComplete业务方法
 /// 重写此方法,使用LimitedTask来代替系统默认的Task可以控制并发数
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="packet">封包</param>
 protected virtual void OnRecvCompleteHandleWithTask(SocketAsync <T> client, T packet)
 {
     Task.Factory.StartNew(() => this.OnRecvComplete(client, packet));
 }
Beispiel #10
0
 /// <summary>
 /// 发送之前触发
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="packet">数据包</param>
 protected virtual void OnSend(SocketAsync <T> client, T packet)
 {
 }
Beispiel #11
0
 /// <summary>
 /// 当接收到远程端的数据时,将触发此方法
 /// 此方法用于处理和分析收到的数据
 /// 如果得到一个数据包,将触发OnRecvComplete方法
 /// [注]这里只需处理一个数据包的流程
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="recvBuilder">接收到的历史数据</param>
 /// <returns>如果不够一个数据包,则请返回null</returns>
 protected abstract T OnReceive(SocketAsync <T> client, ByteBuilder recvBuilder);