Beispiel #1
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(e);
         if (!willRaiseEvent)
         {
             ProcessReceive(e);
         }
     }
     else
     {
         CloseClientSocket(e);
     }
 }
Beispiel #2
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            try
            {
                Interlocked.Increment(ref clientCount);
                // Get the socket for the accepted client connection and put it into the
                //ReadEventArg object user token
                SocketAsyncEventArgs readEventArgs = eventPool.Pop();
                AsyncUserToken       userToken     = (AsyncUserToken)readEventArgs.UserToken;
                userToken.Socket      = e.AcceptSocket;
                userToken.ConnectTime = DateTime.Now;
                userToken.Remote      = e.AcceptSocket.RemoteEndPoint;
                userToken.IPAddress   = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Address;

                lock (clients)
                {
                    clients.Add(userToken);
                }

                if (ClientNumberChange != null)
                {
                    ClientNumberChange(1, userToken);
                }

                if (!e.AcceptSocket.ReceiveAsync(readEventArgs))
                {
                    ProcessReceive(readEventArgs);
                }
            }
            catch (Exception me)
            {
                //TODO:LOG
                //RuncomLib.Log.LogUtils.Info(me.Message + "\r\n" + me.StackTrace);
            }

            // Accept the next connection request
            if (e.SocketError == SocketError.OperationAborted)
            {
                return;
            }
            StartAccept(e);
        }
Beispiel #3
0
        //关闭客户端
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = e.UserToken as AsyncUserToken;

            lock (clients) { clients.Remove(token); }
            //如果有事件,则调用事件,发送客户端数量变化通知
            if (ClientNumberChange != null)
            {
                ClientNumberChange(-1, token);
            }
            // close the socket associated with the client
            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            catch (Exception) { }
            token.Socket.Close();
            // decrement the counter keeping track of the total number of clients connected to the server
            Interlocked.Decrement(ref clientCount);
            this.maxNumerAcceptedClients.Release();
            // Free the SocketAsyncEventArg so they can be reused by another client
            e.UserToken = new AsyncUserToken();
            eventPool.Push(e);
        }
Beispiel #4
0
 private void ServerOnClientNumberChange(int num, AsyncUserToken user)
 {
     Console.WriteLine($"client num changed:{num}");
 }