Exemple #1
0
        /// <summary>
        /// Handles the client.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="stream">The stream.</param>
        public void HandleClient(TcpClient client, NetworkStream stream)
        {
            m_logging.Log("start listening to new client...", Logging.Modal.MessageTypeEnum.INFO);
            new Task(() =>
            {
                StreamReader reader = new StreamReader(stream);
                StreamWriter writer = new StreamWriter(stream);
                while (true)
                {
                    try
                    {
                        bool res;
                        string commandLine = reader.ReadLine();
                        while (reader.Peek() > 0)
                        {
                            commandLine += reader.ReadLine();
                        }
                        if (commandLine != null)
                        {
                            CommandRecievedEventArgs command = JsonConvert.DeserializeObject <CommandRecievedEventArgs>(commandLine);

                            if (command.CommandID.Equals((int)CommandEnum.CloseGui))
                            {
                                string[] args = { JsonConvert.SerializeObject(client) };
                                CommandRecievedEventArgs closeCommand = new CommandRecievedEventArgs((int)CommandEnum.CloseGui, args, "");
                                m_controller.ExecuteCommand(closeCommand.CommandID, closeCommand.Args, out res);
                                m_logging.Log("Client disconnected", Logging.Modal.MessageTypeEnum.WARNING);
                                break;
                            }
                            string result = m_controller.ExecuteCommand(command.CommandID, command.Args, out res);
                            try
                            {
                                //  Thread.Sleep(1000);
                                mtx.WaitOne();
                                writer.WriteLine(result);
                                writer.Flush();
                                mtx.ReleaseMutex();
                            } catch (Exception e)
                            {
                                m_logging.Log("Client disconnected", Logging.Modal.MessageTypeEnum.WARNING);
                                RemoveClient?.Invoke(client);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                        return;
                    }
                }
            }).Start();
        }
Exemple #2
0
        private void Remove(WebNotifyConnection client)
        {
            try
            {
                connections.Remove(client);
                client?.Dispose();
            }
            catch (Exception ex)
            {
                Helper.OnException(ex);
            }

            RemoveClient?.Invoke(this, new WebNotifyEventArgs(client));
        }
Exemple #3
0
 private void OnRemoveClient(SocketConnection sender)
 {
     RemoveClient?.Invoke(sender);
 }