File: DeliveryStrategy.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (202 lines) | stat: -rw-r--r-- 5,335 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
    using System;
    using System.Collections.Generic;
    using System.Runtime;
    using System.Threading;

    abstract class DeliveryStrategy<ItemType> : IDisposable
        where ItemType : class, IDisposable
    {
        InputQueueChannel<ItemType> channel;
        Action dequeueCallback;
        int quota;

        public DeliveryStrategy(InputQueueChannel<ItemType> channel, int quota)
        {
            if (quota <= 0)
            {
                throw Fx.AssertAndThrow("Argument quota must be positive.");
            }

            this.channel = channel;
            this.quota = quota;
        }

        protected InputQueueChannel<ItemType> Channel
        {
            get
            {
                return this.channel;
            }
        }

        public Action DequeueCallback
        {
            get
            {
                return this.dequeueCallback;
            }
            set
            {
                this.dequeueCallback = value;
            }
        }

        public abstract int EnqueuedCount
        {
            get;
        }

        protected int Quota
        {
            get
            {
                return this.quota;
            }
        }

        public abstract bool CanEnqueue(Int64 sequenceNumber);

        public virtual void Dispose()
        {
        }

        public abstract bool Enqueue(ItemType item, Int64 sequenceNumber);
    }

    class OrderedDeliveryStrategy<ItemType> : DeliveryStrategy<ItemType>
        where ItemType : class, IDisposable
    {
        bool isEnqueueInOrder;
        Dictionary<Int64, ItemType> items;
        Action<object> onDispatchCallback;
        Int64 windowStart;

        public OrderedDeliveryStrategy(
            InputQueueChannel<ItemType> channel,
            int quota,
            bool isEnqueueInOrder)
            : base(channel, quota)
        {
            this.isEnqueueInOrder = isEnqueueInOrder;
            this.items = new Dictionary<Int64, ItemType>();
            this.windowStart = 1;
        }

        public override int EnqueuedCount
        {
            get
            {
                return this.Channel.InternalPendingItems + this.items.Count;
            }
        }

        Action<object> OnDispatchCallback
        {
            get
            {
                if (this.onDispatchCallback == null)
                {
                    this.onDispatchCallback = this.OnDispatch;
                }

                return this.onDispatchCallback;
            }
        }

        public override bool CanEnqueue(long sequenceNumber)
        {
            if (this.EnqueuedCount >= this.Quota)
            {
                return false;
            }

            if (this.isEnqueueInOrder && (sequenceNumber > this.windowStart))
            {
                return false;
            }

            return (this.Channel.InternalPendingItems + sequenceNumber - this.windowStart < this.Quota);
        }

        public override bool Enqueue(ItemType item, long sequenceNumber)
        {
            if (sequenceNumber > this.windowStart)
            {
                this.items.Add(sequenceNumber, item);
                return false;
            }

            this.windowStart++;

            while (this.items.ContainsKey(this.windowStart))
            {
                if (this.Channel.EnqueueWithoutDispatch(item, this.DequeueCallback))
                {
                    ActionItem.Schedule(this.OnDispatchCallback, null);
                }

                item = this.items[this.windowStart];
                this.items.Remove(this.windowStart);
                this.windowStart++;
            }

            return this.Channel.EnqueueWithoutDispatch(item, this.DequeueCallback);
        }

        static void DisposeItems(Dictionary<Int64, ItemType>.Enumerator items)
        {
            if (items.MoveNext())
            {
                using (ItemType item = items.Current.Value)
                {
                    DisposeItems(items);
                }
            }
        }

        public override void Dispose()
        {
            DisposeItems(this.items.GetEnumerator());
            this.items.Clear();

            base.Dispose();
        }

        void OnDispatch(object state)
        {
            this.Channel.Dispatch();
        }
    }

    class UnorderedDeliveryStrategy<ItemType> : DeliveryStrategy<ItemType>
        where ItemType : class, IDisposable
    {
        public UnorderedDeliveryStrategy(InputQueueChannel<ItemType> channel, int quota)
            : base(channel, quota)
        {
        }

        public override int EnqueuedCount
        {
            get
            {
                return this.Channel.InternalPendingItems;
            }
        }

        public override bool CanEnqueue(Int64 sequenceNumber)
        {
            return (this.EnqueuedCount < this.Quota);
        }

        public override bool Enqueue(ItemType item, long sequenceNumber)
        {
            return this.Channel.EnqueueWithoutDispatch(item, this.DequeueCallback);
        }
    }
}