コード例 #1
0
ファイル: WebSocketClient.cs プロジェクト: mdda/openrpa
        public async Task ProcessQueue()
        {
            try
            {
                //await ReceiveSemaphore.WaitAsync();
                await ProcessingSemaphore.WaitAsync();

                if (_receiveQueue == null)
                {
                    return;
                }
                List <string> ids = new List <string>();
                for (var i = 0; i < _receiveQueue.Count; i++)
                {
                    if (_receiveQueue[i] != null)
                    {
                        string id = _receiveQueue[i].id;
                        if (!ids.Contains(id))
                        {
                            ids.Add(id);
                        }
                    }
                }
                // ids = (from m in _receiveQueue group m by new { m.id } into mygroup select mygroup.Key.id).ToList();
                foreach (var id in ids)
                {
                    SocketMessage        first = null;
                    List <SocketMessage> msgs  = null;
                    lock (_receiveQueue)
                    {
                        first = _receiveQueue.ToList().Where((x) => x.id == id).First();
                        msgs  = _receiveQueue.ToList().Where((x) => x.id == id).ToList();
                    }
                    if (first.count == msgs.Count)
                    {
                        string data = "";
                        foreach (var m in msgs.OrderBy((y) => y.index))
                        {
                            data += m.data;
                        }
                        var result = new Message(first, data);
                        Process(result);
                        foreach (var m in msgs.OrderBy((y) => y.index).ToList())
                        {
                            _receiveQueue.Remove(m);
                        }
                    }
                }
            }
            finally
            {
                //ReceiveSemaphore.Release();
            }

            try
            {
                // await SendSemaphore.WaitAsync();
                foreach (var msg in _sendQueue.ToList())
                {
                    if (await SendString(JsonConvert.SerializeObject(msg), src.Token))
                    {
                        _sendQueue.Remove(msg);
                    }
                }
            }
            finally
            {
                //SendSemaphore.Release();
                ProcessingSemaphore.Release();
            }
        }
コード例 #2
0
ファイル: WebSocketClient.cs プロジェクト: mdda/openrpa
        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;
                }
            }
        }
コード例 #3
0
ファイル: SocketCommand.cs プロジェクト: vantk85/openrpa
 public SocketCommand()
 {
     msg = new Message("ping");
 }