예제 #1
0
        // Queue for sending.
        public void SendQueued(Packet cpPacket)
        {
            IPacketCache cache = this.Cache.Request(cpPacket);

            if (cache == null)
            {
                // QueueUnqueuePacket
                Packet cpNullPacket = null;

                if (cpPacket.OriginatedFromServer == true && cpPacket.IsResponse == true)
                {
                    this.SendAsync(cpPacket);
                }
                else
                {
                    // Null return because we're not popping a packet, just checking to see if this one needs to be queued.
                    if (this.QueueUnqueuePacket(true, cpPacket, out cpNullPacket) == false)
                    {
                        // No need to queue, queue is empty.  Send away..
                        this.SendAsync(cpPacket);
                    }

                    // Shutdown if we're just waiting for a response to an old packet.
                    this.RestartConnectionOnQueueFailure();
                }
            }
            else if (this.PacketCacheIntercept != null)
            {
                Packet cloned = (Packet)cache.Response.Clone();
                cloned.SequenceNumber = cpPacket.SequenceNumber;

                // Fake a response to this packet
                this.PacketCacheIntercept(this, cpPacket, cloned);
            }
        }
예제 #2
0
        public IPacketCache Request(Packet request)
        {
            IPacketCache cache = null;

            if (request.IsResponse == false)
            {
                var key = request.ToString();

                lock (this.CacheLock) {
                    // Have we got it cached and is it valid?
                    if (this.Cache.ContainsKey(key) == true)
                    {
                        if (this.Cache[key].Response != null)
                        {
                            // Yes, we do. Return this.
                            cache = this.Cache[key];
                        }
                        // else return null
                    }
                    // No, check if we should cache it.
                    else
                    {
                        this.CacheIfApplicable(key, request);
                    }
                }
            }

            return(cache);
        }
예제 #3
0
        public void Response(Packet response)
        {
            if (response.IsResponse == true)
            {
                lock (this.CacheLock) {
                    IPacketCache cache = this.Cache.Where(item => item.Value.Request.SequenceNumber == response.SequenceNumber).Select(item => item.Value).FirstOrDefault();

                    if (cache != null)
                    {
                        cache.Response = response;
                    }
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Checks if a packet should be cached, if so it will cache on a key
        /// </summary>
        /// <param name="key">The key to cache on</param>
        /// <param name="request">The request being made to the server</param>
        /// <returns>A new cache object if the request was cachable, false otherwise.</returns>
        protected IPacketCache CacheIfApplicable(String key, Packet request)
        {
            IPacketCache cache = null;

            var config = this.Configurations.FirstOrDefault(c => c.Matching.IsMatch(key));

            if (config != null)
            {
                cache = new PacketCache()
                {
                    Request = request,
                    Expiry  = DateTime.Now.Add(config.Ttl)
                };

                this.Cache.Add(key, cache);
            }

            return(cache);
        }