Exemplo n.º 1
0
 /// <summary>
 /// OnCloseAll is summoned by the CloseAll event.
 /// The method close the client handler.
 /// </summary>
 /// <param name="sender">the object that invoke the event</param>
 public void OnCloseAll(object sender, EventArgs args)
 {
     m_client.Close();
     // clear NewMessage event invocation list
     NewMessage = null;
     ClientClose.Invoke(this, null);
 }
Exemplo n.º 2
0
        //关闭客户端
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            if (e.UserToken is UserToken token)
            {
                lock (ClientList)
                {
                    ClientList.Remove(token);
                }

                // close the socket associated with the client
                try
                {
                    token.Socket.Shutdown(SocketShutdown.Send);
                    ClientClose?.Invoke(token);
                }
                catch (Exception ex)
                {
                    Error?.Invoke(ex);
                }

                token.Socket.Close();
                _maxNumberAcceptedClients.Release();
                // Free the SocketAsyncEventArg so they can be reused by another client
                e.UserToken = new UserToken();
                _pool.Push(e);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The method makes the HandleClient starts handle
        /// the communication with the client.
        /// </summary>
        public void HandleClient()
        {
            // a communication task
            Task task = new Task(() =>
            {
                using (NetworkStream stream = m_client.GetStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        // a loop that continue wile communication open
                        while (m_client.Connected)
                        {
                            try
                            {
                                // read incomming message or feedback
                                string commandLine = reader.ReadLine();
                                // invoke NewMessage event
                                NewMessage.Invoke(this, commandLine);
                            }
                            catch (Exception e)
                            {
                                break;
                            }
                        }
                    }
                // close communication (if still open)
                m_client.Close();
                ClientClose.Invoke(this, null);
            });

            task.Start();
        }
Exemplo n.º 4
0
        /// <summary>
        /// The method makes the HandleClient starts handle
        /// the communication with the client.
        /// </summary>
        public void HandleClient()
        {
            // a communication task
            Task task = new Task(() =>
            {
                using (NetworkStream stream = m_client.GetStream())
                    using (BinaryReader reader = new BinaryReader(stream))
                        using (BinaryWriter writer = new BinaryWriter(stream))
                        {
                            // a loop that continue wile communication open
                            while (m_client.Connected)
                            {
                                try
                                {
                                    // read incomming message or feedback
                                    string commandLine = reader.ReadString();
                                    // check if got a new message, or just feedback
                                    if (commandLine != "ping")
                                    {
                                        // invoke NewMessage event
                                        NewMessage.Invoke(this, commandLine);
                                    }
                                    lock (m_messages)
                                    {
                                        // check if there is a message to send
                                        if ((m_messages == null) || (m_messages.Count <= 0))
                                        {
                                            writer.Write("ping");                             // send ping for feedback
                                        }
                                        else
                                        {
                                            // send the message
                                            writer.Write(m_messages[0]);
                                            m_messages.RemoveAt(0);
                                        }
                                    }
                                    writer.Flush();
                                }
                                catch (Exception)
                                {
                                    break;
                                }
                            }
                        }
                // close communication (if still open)
                m_client.Close();
                ClientClose.Invoke(this, null);
            });

            task.Start();
        }
Exemplo n.º 5
0
 /// <summary>
 /// OnCloseAll is summoned by the CloseAll event.
 /// The method close the client handler.
 /// </summary>
 /// <param name="sender">the object that invoke the event</param>
 public void OnCloseAll(object sender, EventArgs args)
 {
     m_client.Close();
     ClientClose.Invoke(this, null);
 }