Example #1
0
        private async Task PingLoop()
        {
            byte[] buffer = new byte[1024];
            while (isConnected)
            {
                await Task.Delay(10000);

                var msg = new Message("ping");
                msg.SendMessage(this);
            }
        }
Example #2
0
        public async Task <Message> SendMessage(Message msg)
        {
            var qm = new QueuedMessage(msg);

            _messageQueue.Add(qm);
            using (qm.autoReset = new AutoResetEvent(false))
            {
                msg.SendMessage(this);
                await qm.autoReset.WaitOneAsync();
            }
            return(qm.reply);
        }
Example #3
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;
                }
            }
        }
Example #4
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;
                }
            }
        }
Example #5
0
        public async Task <Message> SendMessage(Message msg)
        {
            int retries = 0;
            var qm      = new QueuedMessage(msg);

            lock (_messageQueue)
            {
                _messageQueue.Add(qm);
            }
            try
            {
                using (qm.autoReset = new AutoResetEvent(false))
                {
                    while (qm.reply == null)
                    {
                        if (retries > 0)
                        {
                            lock (_messageQueue)
                            {
                                _messageQueue.Remove(qm);
                                _messageQueue.Add(qm);
                            }

                            // Log.Warning("Retrying " + qm.msg.id + " " + qm.msg.command);
                            msg.SendMessage(this);
                        }
                        else
                        {
                            msg.SendMessage(this);
                        }
                        await qm.autoReset.WaitOneAsync(Config.local.network_message_timeout);

                        if (qm.reply == null || (!string.IsNullOrEmpty(qm.reply.data) && qm.reply.data.Contains("\"error\":\"jwt must be provided\"")))
                        {
                            qm.autoReset.Reset();
                            retries++;
                            qm.reply = null;

                            if (msg.command == "insertorupdateone")
                            {
                                var data  = JObject.Parse(msg.data);
                                var _id   = data["item"].Value <string>("_id");
                                var state = data["item"].Value <string>("state");
                                if (state == "running" || state == "idle")
                                {
                                    throw new Exception("Failed updating object");
                                }
                                Log.Warning("Message timed out " + qm.msg.id + " " + qm.msg.command + " id:" + _id + " state: " + state);
                            }
                            else
                            {
                                Log.Warning("Message timed out " + qm.msg.id + " " + qm.msg.command);
                            }
                        }
                        else
                        {
                            retries = 0;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
            if (retries > 0)
            {
                Log.Error("Gave up on " + qm.msg.id + " " + qm.msg.command);
            }
            return(qm.reply as Message);
        }