Beispiel #1
0
        private void Process(Message msg)
        {
            if (!string.IsNullOrEmpty(msg.replyto))
            {
                if (msg.command != "pong")
                {
                    Log.Network(msg.command + " / replyto: " + msg.replyto);
                }
                // else { Log.Network(msg.command + " / replyto: " + msg.replyto);  }

                foreach (var qm in _messageQueue.ToList())
                {
                    if (qm != null && qm.msg.id == msg.replyto)
                    {
                        try
                        {
                            qm.reply = msg;
                            qm.autoReset.Set();
                            _messageQueue.Remove(qm);
                            break;
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "");
                        }
                    }
                }
            }
            else
            {
                if (msg.command != "ping" && msg.command != "refreshtoken")
                {
                    Log.Network(msg.command + " / " + msg.id);
                }
                // else { Log.Network(msg.command + " / replyto: " + msg.replyto); }
                switch (msg.command)
                {
                case "ping":
                    msg.reply("pong");
                    msg.SendMessage(this);
                    break;

                case "refreshtoken":
                    msg.reply();
                    var signin = JsonConvert.DeserializeObject <SigninMessage>(msg.data);
                    this.user = signin.user;
                    this.jwt  = signin.jwt;
                    // msg.SendMessage(this); no need to confirm
                    if (signin.websocket_package_size > 100)
                    {
                        this.websocket_package_size = signin.websocket_package_size;
                    }
                    break;

                case "queueclosed":
                    msg.reply();
                    QueueClosedMessage qc = null;
                    try
                    {
                        qc = JsonConvert.DeserializeObject <QueueClosedMessage>(msg.data);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.ToString());
                        msg.SendMessage(this);
                        break;
                    }
                    try
                    {
                        var e = new QueueMessageEventArgs();
                        OnQueueClosed?.Invoke(qc, e);
                        msg.data = JsonConvert.SerializeObject(qc);
                        if (e.isBusy)
                        {
                            msg.command = "error";
                            msg.data    = "Sorry, I'm bussy";
                            msg.SendMessage(this);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        msg.command = "error";
                        msg.data    = ex.ToString();
                    }
                    msg.SendMessage(this);
                    // if (string.IsNullOrEmpty(qm.replyto)) msg.SendMessage(this);
                    break;

                case "queuemessage":
                    msg.reply();
                    QueueMessage qm = null;
                    try
                    {
                        qm = JsonConvert.DeserializeObject <QueueMessage>(msg.data);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.ToString());
                        msg.SendMessage(this);
                        break;
                    }
                    try
                    {
                        var e = new QueueMessageEventArgs();
                        OnQueueMessage?.Invoke(qm, e);
                        msg.data = JsonConvert.SerializeObject(qm);
                        if (e.isBusy)
                        {
                            msg.command = "error";
                            msg.data    = "Sorry, I'm bussy";
                            msg.SendMessage(this);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        msg.command = "error";
                        msg.data    = ex.ToString();
                    }
                    msg.SendMessage(this);
                    // if (string.IsNullOrEmpty(qm.replyto)) msg.SendMessage(this);
                    break;

                case "watchevent":
                    msg.reply();
                    WatchEventMessage wem;
                    try
                    {
                        wem = JsonConvert.DeserializeObject <WatchEventMessage>(msg.data);
                        if (!string.IsNullOrEmpty(wem.id) && watches.ContainsKey(wem.id))
                        {
                            try
                            {
                                watches[wem.id](wem.id, wem.result);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex.ToString());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.ToString());
                        msg.SendMessage(this);
                        break;
                    }
                    //msg.SendMessage(this);
                    break;

                default:
                    Log.Error("Received unknown command '" + msg.command + "' with data: " + msg.data);
                    break;
                }
            }
        }
Beispiel #2
0
        private void Process(Message msg)
        {
            if (!string.IsNullOrEmpty(msg.replyto))
            {
                if (msg.command != "pong")
                {
                    Log.Verbose(msg.command + " / replyto: " + msg.replyto);
                }
                // else { Log.Verbose(msg.command + " / replyto: " + msg.replyto);  }

                foreach (var qm in _messageQueue.ToList())
                {
                    if (qm != null && qm.msg.id == msg.replyto)
                    {
                        try
                        {
                            qm.reply = msg;
                            qm.autoReset.Set();
                            _messageQueue.Remove(qm);
                            break;
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "");
                        }
                    }
                }
            }
            else
            {
                if (msg.command != "ping" && msg.command != "refreshtoken")
                {
                    Log.Verbose(msg.command + " / " + msg.id);
                }
                // else { Log.Verbose(msg.command + " / replyto: " + msg.replyto); }
                switch (msg.command)
                {
                case "ping":
                    msg.reply("pong");
                    msg.SendMessage(this);
                    break;

                case "refreshtoken":
                    msg.reply();
                    var singin = JsonConvert.DeserializeObject <SigninMessage>(msg.data);
                    this.user = singin.user;
                    this.jwt  = singin.jwt;
                    // msg.SendMessage(this); no need to confirm
                    break;

                case "queuemessage":
                    msg.reply();
                    QueueMessage qm = null;
                    try
                    {
                        qm = JsonConvert.DeserializeObject <QueueMessage>(msg.data);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.ToString());
                        msg.SendMessage(this);
                        break;
                    }
                    try
                    {
                        var e = new QueueMessageEventArgs();
                        OnQueueMessage?.Invoke(qm, e);
                        msg.data = JsonConvert.SerializeObject(qm);
                        if (e.isBusy)
                        {
                            msg.command = "error";
                            msg.data    = "Sorry, I'm bussy";
                            Log.Warning("Cannot invoke, I'm busy.");
                            msg.SendMessage(this);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        msg.command = "error";
                        msg.data    = ex.ToString();
                    }
                    msg.SendMessage(this);
                    break;

                default:
                    Log.Error("Received unknown command '" + msg.command + "' with data: " + msg.data);
                    break;
                }
            }
        }