Ejemplo n.º 1
0
        internal AsyncUserToken FindByUID(string uid)
        {
            if (uid == string.Empty || uid == "")
            {
                return(null);
            }
            AsyncUserToken asyncUserToken = null;

            if (this.OnlineUID.Any(key => key == uid))
            {
                asyncUserToken = Busypool[uid];
            }
            return(asyncUserToken);
        }
Ejemplo n.º 2
0
        private void CloseClientSocket(AsyncUserToken token)
        {
            // close the socket associated with the client
            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception) { }
            token.Socket.Close();

            // decrement the counter keeping track of the total number of clients connected to the server
            Interlocked.Decrement(ref m_totalConnectedSockets);
            _previouseSemaphore = m_maxNumberAcceptedClients.Release();

            m_userTokenPool.Push(token);
        }
Ejemplo n.º 3
0
 // This method is invoked when an asynchronous send operation completes.
 // The method issues another receive on the socket to read any additional
 // data sent from the client
 //
 // <param name="e"></param>
 private void ProcessSend(SocketAsyncEventArgs e)
 {
     if (e.SocketError == SocketError.Success)
     {
         // done echoing data back to the client
         AsyncUserToken token = (AsyncUserToken)e.UserToken;
         // read the next block of data send from the client
         bool willRaiseEvent = token.Socket.ReceiveAsync(token.ReceiveSaea);
         if (!willRaiseEvent)
         {
             ProcessReceive(token.ReceiveSaea);
         }
     }
     else
     {
         CloseClientSocket(e.UserToken as AsyncUserToken);
     }
 }
Ejemplo n.º 4
0
        internal AsyncUserToken Pop(string uid)
        {
            if (uid == string.Empty || uid == "")
            {
                return(null);
            }
            AsyncUserToken asyncUserToken = null;

            lock (this.Pool)
            {
                asyncUserToken = this.Pool.Pop();
            }
            asyncUserToken.UID       = uid;
            asyncUserToken.Available = true;    //mark the state of pool is not the initial step
            if (!Busypool.TryAdd(uid, asyncUserToken))
            {
                _logger.Error("Cannot add to Busy pool, client:{0}, already connected", uid);
            }
            return(asyncUserToken);
        }
Ejemplo n.º 5
0
        // Initializes the server by preallocating reusable buffers and
        // context objects.  These objects do not need to be preallocated
        // or reused, but it is done this way to illustrate how the API can
        // easily be used to create reusable objects to increase server performance.
        //
        public void Init()
        {
            // Allocates one large byte buffer which all I/O operations use a piece of.  This gaurds
            // against memory fragmentation
            m_bufferManager.InitBuffer();

            // preallocate pool of SocketAsyncEventArgs objects
            AsyncUserToken asyncUserToken;

            for (int i = 0; i < m_maxConnections; i++)
            {
                //Pre-allocate a set of reusable asyncUserToken
                asyncUserToken = new AsyncUserToken {
                    ReceiveSaea = new TCCSocketAsyncEventArgs(), SendSaea = new TCCSocketAsyncEventArgs()
                };
                asyncUserToken.ReceiveSaea.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                asyncUserToken.SendSaea.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);

                //只给接收的SocketAsyncEventArgs设置缓冲区
                m_bufferManager.SetBuffer(asyncUserToken.ReceiveSaea);

                // add asyncUserToken to the pool
                m_userTokenPool.Push(asyncUserToken);
            }

            //Print Serever status
            Thread prinThread = new Thread(() =>
            {
                while (true)
                {
                    PrintCurrentConnections();
                    Thread.Sleep(1000);
                }
            })
            {
                IsBackground = true
            };

            prinThread.Start();
        }