Esempio n. 1
0
        /// <inheritdoc />
        public void Send(byte[] data, ISession session, EndPoint ep)
        {
            MockQueueItem item = new MockQueueItem(MockQueueItem.QueueType.NetworkSend, data);

            item.Destination = ep;
            item.Source      = LocalEndPoint;

            MessageDecoder decoder = new Spec.MessageDecoder18(data);

            if (decoder.IsRequest)
            {
                item.Request = decoder.DecodeRequest();
            }
            else if (decoder.IsResponse)
            {
                item.Response = decoder.DecodeResponse();
            }
            else if (decoder.IsEmpty)
            {
                item.EmptyMessage = decoder.DecodeEmptyMessage();
            }
            else
            {
                throw new Exception("UNKNOWN MESSAGE TYPE");
            }

            DeliveryInstrutions rule = DeliveryInstrutions.Deliver;

            if (DeliveryRules != null && deliveryPoint < DeliveryRules.Length)
            {
                int defer = -1;
                switch (DeliveryRules[deliveryPoint])
                {
                case DeliveryInstrutions.Deliver:
                    break;

                case DeliveryInstrutions.DeferBy1:
                case DeliveryInstrutions.DeferBy2:
                case DeliveryInstrutions.DeferBy3:
                case DeliveryInstrutions.DeferBy4:
                case DeliveryInstrutions.DeferBy5:
                    defer = DeliveryRules[deliveryPoint] - DeliveryInstrutions.DeferBy1 + 1;
                    rule  = DeliveryInstrutions.Omit;
                    break;

                case DeliveryInstrutions.DupAndDeferBy0:
                case DeliveryInstrutions.DupAndDeferBy1:
                case DeliveryInstrutions.DupAndDeferBy2:
                case DeliveryInstrutions.DupAndDeferBy3:
                case DeliveryInstrutions.DupAndDeferBy4:
                    defer = DeliveryRules[deliveryPoint] - DeliveryInstrutions.DupAndDeferBy0;
                    break;
                }

                if (defer >= 0)
                {
                    int insertAt = 0;
                    foreach (TransportItem t in DelayList)
                    {
                        if (t.DelayCount > defer)
                        {
                            break;
                        }

                        insertAt += 1;
                    }

                    DelayList.Insert(insertAt, new TransportItem(item, defer));
                }
            }

            if (rule == DeliveryInstrutions.Deliver)
            {
                Pump.Queue.Enqueue(item);
            }

            if (DelayList.Count > 0)
            {
                int removeCount = 0;
                foreach (TransportItem t in DelayList)
                {
                    t.DelayCount -= 1;
                    if (t.DelayCount == 0)
                    {
                        Pump.Queue.Enqueue(t.Item);
                        removeCount += 1;
                    }
                }

                if (removeCount > 0)
                {
                    DelayList.RemoveRange(0, removeCount);
                }
            }
        }