Esempio n. 1
0
        public int DequeuAllReceivedMessages()
        {
            var dequeuCount = 0;

            try
            {
                while (receivedQueue.Count > 0)
                {
                    var message = receivedQueue.Dequeue();
                    dequeuCount++;
                    if (!callbackManager.InvokeCallback(message))
                    {
                        MessageReceived.SafeInvoke(this, message);
                    }
                }
            }
            catch (Exception ex)
            {
                CustomTools.Console.DebugError("Client::DequeuAllReceivedMessages() Exception:", ex.Message);
            }
            return(dequeuCount);
        }
Esempio n. 2
0
        void ReadFromSource()
        {
            int nextPruneTick = Environment.TickCount + 60 * 1000;

            while (!stop)
            {
                string rawPacket;
                try
                {
                    rawPacket = reader.ReadLine();
                }
                catch (IOException ex)
                {
                    StreamError.SafeInvoke(this, new StreamErrorEventArgs(ex));
                    break;
                }
                catch (ObjectDisposedException)
                {
                    break;
                }

                log.Debug(rawPacket);

                var packet = Message.Parse(rawPacket);

                JToken rawTag;
                bool   hasTag = packet.TryGetValue("tag", out rawTag);

                string messageName = (packet["message"] ?? "").ToString();
                string replyName   = (packet["reply"] ?? "").ToString();
                if (messageName != "")
                {
                    MessageReceived.SafeInvoke(this, new MessageEventArgs(packet));
                }
                else if (replyName != "")
                {
                    if (CheckObjectFields("reply", packet, "tag", "result", "result_message"))
                    {
                        string tag      = packet["tag"].ToString();
                        var    callback = outstandingCallbacks.SingleOrDefault(_callback => _callback.Tag == tag);

                        if (callback.Callback != null)
                        {
                            callback.Callback(callback.MessageName, ReplyResult.Success, packet);
                            outstandingCallbacks.Remove(callback);
                        }
                    }
                    else
                    {
                        log.Debug("Discarding reply");
                    }
                }
                else
                {
                    log.Info("Received an invalid packet: not a message or a reply");
                }


                if (Environment.TickCount > nextPruneTick)
                {
                    PruneCallbacks();
                    nextPruneTick += 60 * 1000;
                }
            }

            foreach (var callback in outstandingCallbacks)
            {
                callback.Callback(callback.MessageName, ReplyResult.Fail, null);
            }
            outstandingCallbacks.Clear();
        }