示例#1
0
文件: Program.cs 项目: hezlog/MQTT
        private static void c_OnUnsolicitedMessage(object sender, ClientCommandEventArgs e)
        {
            MqttCommand command = e.Command;

            var p = command as Publish;

            if (p != null)
            {
                var sb = new StringBuilder();
                if (p.Header.Duplicate)
                {
                    sb.Append("!!! DUPLICATE !!! - ");
                }
                sb.AppendFormat("TOPIC: {0}", p.Topic);
                sb.AppendLine();
                foreach (char c in Encoding.ASCII.GetChars(p.Message))
                {
                    if (!char.IsControl(c))
                    {
                        sb.Append(c);
                    }
                }

                sb.AppendLine();
                var result = sb.ToString();

                lock (conLock)
                {
                    Console.WriteLine(result);
                }
            }
        }
示例#2
0
文件: Client.cs 项目: hezlog/MQTT
        private void _broker_OnMessageReceived(object sender, ClientCommandEventArgs e)
        {
            MqttCommand command = e.Command;

            Debug.WriteLine("RECV: {0} ({1})", command.CommandMessage, command.MessageId);

            lock (_lastHeaderLock)
            {
                _lastHeard = DateTime.UtcNow;
            }

            switch (command.CommandMessage)
            {
            case CommandMessage.PUBACK:
            case CommandMessage.PUBCOMP:
            case CommandMessage.PUBREC:
            case CommandMessage.PUBREL:
            case CommandMessage.SUBACK:
            case CommandMessage.CONNACK:
            case CommandMessage.UNSUBACK:
                _manager.Deliver(command);
                break;

            case CommandMessage.PINGRESP:
                // ignore (we sent it) - eventually track
                break;

            default:
                _manager.StartNew(command, Notify);
                break;
            }
        }
示例#3
0
        private void ClientOnMessageReceived(object sender, ClientCommandEventArgs e)
        {
            MqttCommand command = e.Command;

            Debug.WriteLine("{0} : Recevied Message {1} id {2}", DateTime.Now.ToString("o"), command.CommandMessage,
                            command.MessageId);

            switch (command.CommandMessage)
            {
            case CommandMessage.PUBACK:
            case CommandMessage.PUBCOMP:
            case CommandMessage.PUBREC:
            case CommandMessage.PUBREL:
            case CommandMessage.SUBACK:
            case CommandMessage.CONNACK:
            case CommandMessage.UNSUBACK:
            case CommandMessage.PINGRESP:
                _manager.Deliver(command);
                break;

            default:
                _manager.StartNew(command, Notify);
                break;
            }
        }
示例#4
0
        private void CommandProc(string commandName, string raw, string[] tokens)
        {
            var            cleanedTokens = tokens.Skip(1).ToArray();
            IClientCommand cmd;

            try
            {
                cmd = _player.Server.ClientCommandHandler.Find(commandName) as IClientCommand;
            }
            catch (CommandNotFoundException e)
            {
                SendMessage(ChatColor.Red + e.Message);
                return;
            }
            try
            {
                //Event
                ClientCommandEventArgs e = new ClientCommandEventArgs(this, cmd, cleanedTokens);
                _player.Server.PluginManager.CallEvent(Event.PlayerCommand, e);
                if (e.EventCanceled)
                {
                    return;
                }
                cleanedTokens = e.Tokens;
                //End Event

                cmd.Use(this, commandName, cleanedTokens);
            }
            catch (Exception e)
            {
                SendMessage("There was an error while executing the command.");
                _player.Server.Logger.Log(e);
            }
        }
示例#5
0
        /// <summary>
        /// Closes the server.
        /// </summary>
        public void CloseServer()
        {
            ClientCommandEventArgs Args = new ClientCommandEventArgs(null,
                                                                     new CommandRecievedEventArgs((int)CommandEnum.CloseCommand, null, "*"));

            CommandRecieved?.Invoke(this, Args);
            com_server.ServerStop();
        }
示例#6
0
 private void OnPlayerPreCommand(ClientCommandEventArgs e)
 {
     foreach (EventListener bl in Plugins)
     {
         IPlayerListener pl = (IPlayerListener)bl.Listener;
         if (bl.Event == Event.PlayerPreCommand)
         {
             pl.OnPlayerPreCommand(e);
         }
     }
 }
示例#7
0
        void c_OnUnsolicitedMessage(object sender, ClientCommandEventArgs e)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (nextUpdate > DateTime.UtcNow)
                {
                    return;
                }

                nextUpdate = DateTime.UtcNow.AddSeconds(1);

                MqttCommand command = e.Command;

                Publish p = command as Publish;
                if (p != null)
                {
                    lock (this.textBlock1)
                    {
                        if (p.Header.Duplicate)
                        {
                            this.textBlock1.Text = "!!! DUPLICATE !!! - ";
                        }

                        StringBuilder sb = new StringBuilder();

                        sb.AppendFormat("TOPIC: {0}", p.Topic);
                        sb.AppendLine();
                        sb.AppendLine();

                        foreach (char c in UTF8Encoding.UTF8.GetChars(p.Message))
                        {
                            if (!char.IsControl(c))
                            {
                                sb.Append(c);
                            }
                        }


                        sb.AppendLine();
                        sb.AppendLine();

                        this.textBlock1.Text = sb.ToString();
                    }
                }
            });
        }
        /// <summary>
        /// method to be activated when command enters
        /// </summary>
        /// <param name="sender">the sender object</param>
        /// <param name="e"> args for the command</param>
        public void OnCommandRecieved(object sender, ClientCommandEventArgs e)
        {
            //check which command was given and execute in a new Task
            Task t = new Task(() =>
            {
                bool result;
                string output;
                try
                {
                    output = MyController.ExecuteCommand(e.Args.CommandID, e.Args.Args, out result);
                }
                catch
                {
                    output = "";
                }
                if (e.Args.CommandID == (int)CommandEnum.CloseCommand)
                {
                    if (e.Args.RequestDirPath == MyPath || String.Equals("*", e.Args.RequestDirPath))
                    {
                        //CommandDone?.Invoke(this, new CommandDoneEventArgs(e.Client, output));
                        // return;

                        for (int i = 0; i < MyDirectoryWWatcher.Length; i++)
                        {
                            MyDirectoryWWatcher[i].EnableRaisingEvents = false;
                            MyDirectoryWWatcher[i].Dispose();
                        }
                        UpdateLog("directory closed: " + MyPath, MessageTypeEnum.INFO);
                        MyLogging.Log("directory closed: " + MyPath, MessageTypeEnum.INFO);
                        DirectoryClosing?.Invoke(this, new DirectoryCloseEventArgs(null, null));
                        CommandDone?.Invoke(this, new CommandDoneEventArgs(e.Client, output));
                        return;
                    }
                }
                CommandDone?.Invoke(this, new CommandDoneEventArgs(e.Client, output));
            });

            t.Start();
        }
示例#9
0
 /// <summary>
 /// Passes a command to the client.
 /// </summary>
 /// <param name="sender">This server.</param>
 /// <param name="args">The commandargs to be passed to the client.</param>
 public void ClientCommand(object sender, ClientCommandEventArgs args)
 {
     CommandRecieved(this, args);
 }
示例#10
0
 public void OnPlayerPreCommand(ClientCommandEventArgs e)
 {
 }