Ejemplo n.º 1
0
        private void WriteExceptionErrorToClient(TcpServerConnection client, Exception ex)
        {
            string returnMsg = TcpAppCommandStatus.ERR + " " + ex.Message;
            TcpAppServerConnection appServerClient = AppClients.FirstOrDefault(x => x.Connection == client);
            string clientName = appServerClient == null?client.ClientIPAddress.ToString() : appServerClient.Name;

            Debug.WriteLine("AppServer[" + clientName + "]-Error: " + returnMsg);
            client.WriteLineToClient(returnMsg);
        }
Ejemplo n.º 2
0
        private void WriteResultToClient(TcpAppServerConnection client, TcpAppInputCommand input)
        {
            string returnMsg = input.Status.ToString() + " ";

            if (!string.IsNullOrEmpty(input.OutputMessage))
            {
                returnMsg += input.OutputMessage;
            }
            Debug.WriteLine("AppServer[" + client.Name + "]-TX: " + returnMsg);
            client.Connection.WriteLineToClient(returnMsg);
        }
Ejemplo n.º 3
0
 private TcpAppServerConnection AddClientToAppClientsList(TcpServerConnection client)
 {
     lock (AppClients)
     {
         TcpAppServerConnection result = new TcpAppServerConnection()
         {
             Connection = client, Name = "#" + client.ClientIPAddress.ToString()
         };
         AppClients.Add(result);
         client.ProcessReceivedMessageCallback = Client_ProcessReceivedMessage;
         return(result);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="sender"></param>
 public TcpAppServerEventArgs(TcpAppServerConnection sender)
 {
     Client = sender;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="sender"></param>
 public TcpAppServerExEventArgs(TcpAppServerConnection sender) : base(sender)
 {
 }
Ejemplo n.º 6
0
        private void Client_ProcessReceivedMessage(TcpServerConnection client, string message, byte[] messageBytes)
        {
            //Parse and Execute Commands
            string[] cmdArg = TcpAppCommon.ParseCommand(message.Trim());
            try
            {
                //Register Client Connection
                TcpAppServerConnection ptrClient = AppClients.FirstOrDefault(x => x.Connection == client);
                if (ptrClient == null)
                {
                    //Reconstruct device which had already signed out
                    ptrClient = AddClientToAppClientsList(client);
                }
                Debug.WriteLine("AppServer[" + ptrClient.Name + "]-RX: " + message);

                TcpAppInputCommand inputCommand = TcpAppCommon.CreateInputCommand(Commands, cmdArg);
                if (inputCommand != null)
                {
                    inputCommand.AppClient = ptrClient;
                }
                else//Command keyword not exist
                {
                    //Check if command keyword is alias name
                    ITcpAppServerPlugin plugin = _Plugins.FirstOrDefault(x => string.Compare(x.Alias, cmdArg[0], true) == 0);
                    if (plugin != null)
                    {
                        //Execute plugin command
                        inputCommand           = plugin.GetPluginCommand(cmdArg.Skip(1).ToArray());
                        inputCommand.AppClient = ptrClient;
                        BeforeExecutePluginCommand?.Invoke(this, new TcpAppServerExEventArgs(ptrClient)
                        {
                            Plugin = plugin
                        });
                    }
                    else
                    {
                        //Error - Unrecognized command.
                        inputCommand = new TcpAppInputCommand()
                        {
                            Status        = TcpAppCommandStatus.ERR,
                            OutputMessage = "Invalid Command " + cmdArg[0]
                        };
                        WriteResultToClient(ptrClient, inputCommand);
                        return;
                    }
                }

                //Verify Client had signed in.
                if (!inputCommand.AppClient.SignedIn && !inputCommand.Command.IsSystemCommand)
                {
                    throw new Exception("Client not signed in! Execute SignIn first.");
                }

                if (inputCommand.Command.UseMessageQueue && !inputCommand.Command.IsSystemCommand)
                {
                    //Single thread execution, post message to message queue.
                    lock (CommandQueue)
                    {
                        inputCommand.ID = inputCommand.GetHashCode();
                        if (ptrClient.NextQueuedCommand == null)
                        {
                            ptrClient.NextQueuedCommand = inputCommand;                                      //Set pointer to next queued command.
                        }
                        //Add Command to Queue
                        CommandQueue.Add(inputCommand);
                        CommandQueueWaitSignal?.Set();
                        inputCommand.OutputMessage = inputCommand.ID.ToString();
                        inputCommand.Status        = TcpAppCommandStatus.QUEUED;
                    }
                }
                else if (inputCommand.Command.IsSystemCommand)
                {
                    inputCommand.ExecuteCallback();
                }
                else
                {
                    //Execute command, wait until return
                    if (ExecutionTimeout == 0)
                    {
                        inputCommand.ExecuteCallback();
                    }
                    //Execute command, terminate on timeout
                    else
                    {
                        ptrClient.ExecuteCommandAsync(inputCommand, ExecutionTimeout);
                    }
                }

                WriteResultToClient(ptrClient, inputCommand); //Send result back to client.
            }
            catch (Exception ex)
            {
                WriteExceptionErrorToClient(client, ex);
            }
        }