A NATS message is an object encapsulating a subject, optional reply payload, and subscription information, sent or received by teh client application.
Пример #1
1
 protected internal virtual bool processMsg(Msg msg)
 {
     return true;
 }
Пример #2
0
 public void PassiveWorker(object state)
 {
     while (this._n < this._ctx.LoopCount)
     {
         Original.Msg m     = this._subPassive.NextMessage();
         bool         match = this._ctx.DeserializeAndCheckSimpleJson(m.Data, this._n);
         Debug.Assert(match);
         ++_n;
     }
     this._cde.Signal();
 }
Пример #3
0
        void IReqRepImpl.SlaveEndPointWorker(SlaveClientContext ctx)
        {
            var conn = (Original.IConnection)ctx.Connection;
            var sub  = (Original.ISyncSubscription)ctx.Subscription;

            while (ctx.IsComplete == false)
            {
                Original.Msg   m       = sub.NextMessage();
                ISlaveEndPoint ep      = ctx.EndPoints[m.Subject];
                var            segment = ep.GetResponse(m.Data);
                ctx.AddBytes(m.Data.Length + segment.Count);

                byte[] ba = new byte[segment.Count];
                Buffer.BlockCopy(segment.Array, segment.Offset, ba, 0, segment.Count);
                conn.Publish(m.Reply, ba);
            }
        }
Пример #4
0
        internal protected override bool processMsg(Msg msg)
        {
            Connection localConn;
            EventHandler<MsgHandlerEventArgs> localHandler;
            long localMax;

            lock (mu)
            {
                if (closed)
                    return false;

                localConn = conn;
                localHandler = MessageHandler;
                localMax = max;
            }

            // the message handler has not been setup yet, drop the 
            // message.
            if (MessageHandler == null)
                return true;

            if (conn == null)
                return false;

            long d = tallyDeliveredMessage(msg);
            if (localMax <= 0 || d <= localMax)
            {
                msgHandlerArgs.msg = msg;
                try
                {
                    localHandler(this, msgHandlerArgs);
                }
                catch (Exception) { }

                if (d == max)
                {
                    unsubscribe(false);
                    conn = null;
                }
            }

            return true;
        }
Пример #5
0
        void IReqRepImpl.MasterEndPointWorker(object state)
        {
            var ep   = (IMasterEndPoint)state;
            var conn = (Original.IConnection)ep.MasterClient.Connection;
            int n    = ep.MasterClient.LoopCount;

            while (--n >= 0)
            {
                if (n == 0)
                {
                    ep.Release();
                }
                var    segment = ep.GetRequest();
                byte[] ba      = new byte[segment.Count];
                Buffer.BlockCopy(segment.Array, segment.Offset, ba, 0, segment.Count);
                Original.Msg m = conn.Request(ep.TargetSubject, ba);

                ep.ValidateResponse(m.Data);
            }
        }
Пример #6
0
        internal protected override bool processMsg(Msg msg)
        {
            Connection localConn;
            EventHandler<MsgHandlerEventArgs> localHandler;
            long localMax;

            lock (mu)
            {
                localConn = this.conn;
                localHandler = MessageHandler;
                localMax = this.max;
            }

            // the message handler has not been setup yet, drop the 
            // message.
            if (MessageHandler == null)
                return true;

            if (conn == null)
                return false;

            long d = Interlocked.Increment(ref delivered);
            if (localMax <= 0 || d <= localMax)
            {
                msgHandlerArgs.msg = msg;
                try
                {
                    localHandler(this, msgHandlerArgs);
                }
                catch (Exception) { }

                if (d == max)
                {
                    Unsubscribe();
                    this.conn = null;
                }
            }

            return true;
        }
Пример #7
0
        private bool compare(Msg a, Msg b)
        {
            if (a.Subject.Equals(b.Subject) == false)
                return false;

            if (a.Reply != null && a.Reply.Equals(b.Reply))
            {
                return false;
            }

            return compare(a.Data, b.Data);
        }
Пример #8
0
 private bool compare(byte[] payload, Msg m)
 {
     return compare(payload, m.Data);
 }
Пример #9
0
 public void Publish(Msg msg)
 {
     publish(msg.Subject, msg.Reply, msg.Data);
 }
Пример #10
0
 // returns false if the message could not be added because
 // the channel is full, true if the message was added
 // to the channel.
 internal bool addMessage(Msg msg, int maxCount)
 {
     if (mch != null)
     {
         if (mch.Count >= maxCount)
         {
             return false;
         }
         else
         {
             sc = false;
             mch.add(msg);
         }
     }
     return true;
 }
Пример #11
0
        // returns false if the message could not be added because
        // the channel is full, true if the message was added
        // to the channel.
        internal bool addMessage(Msg msg, int maxCount)
        {
            // Subscription internal stats
	        pMsgs++;
	        if (pMsgs > pMsgsMax)
            {
		        pMsgsMax = pMsgs;
            }
	
	        pBytes += msg.Data.Length;
	        if (pBytes > pBytesMax)
            {
		        pBytesMax = pBytes;
            }
	
            // Check for a Slow Consumer
	        if (pMsgs > pMsgsLimit || pBytes > pBytesLimit)
            {
                // slow consumer
                handleSlowConsumer(msg);
                return false;
            }

            if (mch != null)
            {
                if (mch.Count >= maxCount)
                {
                    handleSlowConsumer(msg);
                    return false;
                }
                else
                {
                    sc = false;
                    mch.add(msg);
                }
            }
            return true;
        }
Пример #12
0
        protected long tallyDeliveredMessage(Msg msg)
        {
            lock (mu)
            {
                delivered++;
                pBytes -= msg.Data.Length;
                pMsgs--;

                return delivered;
            }
        }
Пример #13
0
 private void handleSlowConsumer(Msg msg)
 {
     dropped++;
     conn.processSlowConsumer(this);
     pMsgs--;
     pBytes -= msg.Data.Length;
 }