Ejemplo n.º 1
0
        /// <summary>
        /// 线程处理接收事件
        /// </summary>
        /// <param name="state"></param>
        private void AnalyzeThrd(object state)
        {
            ConnCache      connCache = (ConnCache)state;
            AsyncUserToken userToken = connCache.UserToken;

            lock (userToken.AnalyzeLock)
            {
                lock (userToken.ReceiveBuffer)
                {
                    userToken.ReceiveBuffer.AddRange(connCache.RecvBuffer);
                }
                Handle(userToken);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 关闭所有连接
 /// </summary>
 public static void CloseAllConnection()
 {
     try
     {
         var count = SemaphoreSlim.CurrentCount;
         if (count > 0)
         {
             SemaphoreSlim.Release(count);
         }
         SemaphoreSlim.WaitAsync();
         foreach (var conn in ConnCache.Values)
         {
             if (conn.IsOpen)
             {
                 conn.Dispose();
             }
         }
         ConnCache.Clear();
     }
     finally
     {
         SemaphoreSlim.Release();
     }
 }
Ejemplo n.º 3
0
        private void ProcessReceive(AsyncUserToken userToken)
        {
            SocketAsyncEventArgs e = userToken.SAEA_Receive;

            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                //if (userToken.userInfo != null)
                //{
                //    userToken.userInfo.heartbeatTime = DateTime.Now;
                //}
                string sClientIP = ((IPEndPoint)userToken.ConnectSocket.RemoteEndPoint).Address.ToString();
                //try
                {
                    byte[] copy = new byte[e.BytesTransferred];
                    Array.Copy(e.Buffer, e.Offset, copy, 0, e.BytesTransferred);

                    if (!userToken.ConnectSocket.ReceiveAsync(e))
                    {
                        ProcessReceive(userToken);
                    }

                    ConnCache connCache = new ConnCache(copy, userToken);
                    //ConnCache connCache = new ConnCache(e.Buffer, userToken);
                    //处理线程
                    ThreadPool.QueueUserWorkItem(new WaitCallback(AnalyzeThrd), connCache);
                }
                //catch (Exception error)
                //{
                //    Log4Debug(error.Message);
                //}
            }
            else
            {
                //CloseClientSocket(userToken);
            }
        }