|
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // sbque.cpp - TCP sequence block queue |
|
15 // |
|
16 |
|
17 #include "sbque.h" |
|
18 |
|
19 EXPORT_C SequenceBlock *SequenceBlockQueue::AddOrdered(TTcpSeqNum aLeft, TTcpSeqNum aRight) |
|
20 { |
|
21 SequenceBlock *current, *block = NULL; |
|
22 SequenceBlockQueueIter iter(*this); |
|
23 |
|
24 iter.SetToLast(); |
|
25 while (current = iter--, current != NULL) |
|
26 { |
|
27 // Find correct position. |
|
28 if (aRight < current->iLeft) |
|
29 continue; |
|
30 |
|
31 // No overlap? Insert new block here. |
|
32 if (aLeft > current->iRight) |
|
33 { |
|
34 if (block = new SequenceBlock(aLeft, aRight), block == NULL) |
|
35 return NULL; |
|
36 block->iLink.Enque(¤t->iLink); |
|
37 iCount++; |
|
38 iBytes += (aRight - aLeft); |
|
39 break; |
|
40 } |
|
41 |
|
42 // Already know about this block? |
|
43 if (aLeft >= current->iLeft && aRight <= current->iRight) |
|
44 { |
|
45 block = current; |
|
46 break; |
|
47 } |
|
48 |
|
49 // Our block partially overlaps one or more blocks. |
|
50 if (aLeft > current->iLeft) |
|
51 aLeft = current->iLeft; |
|
52 if (aRight < current->iRight) |
|
53 aRight = current->iRight; |
|
54 |
|
55 // Delete the previously known overlapping block. |
|
56 iBytes -= (current->iRight - current->iLeft); |
|
57 current->iLink.Deque(); |
|
58 delete current; |
|
59 iCount--; |
|
60 } |
|
61 |
|
62 // We haven't found or created a sequence block? It goes first then. |
|
63 if (!block) |
|
64 { |
|
65 block = new SequenceBlock(aLeft, aRight); |
|
66 if (block != NULL) |
|
67 { |
|
68 AddFirst(*block); |
|
69 iCount++; |
|
70 iBytes += (aRight - aLeft); |
|
71 } |
|
72 } |
|
73 |
|
74 // Return the contiguous block, which the new block is belonged to. |
|
75 return block; |
|
76 } |
|
77 |
|
78 EXPORT_C SequenceBlock *SequenceBlockQueue::AddUnordered(TTcpSeqNum aLeft, TTcpSeqNum aRight) |
|
79 { |
|
80 SequenceBlock *current, *block = NULL; |
|
81 SequenceBlockQueueIter iter(*this); |
|
82 |
|
83 while (current = iter++, current != NULL) |
|
84 { |
|
85 // Find overlapping blocks. |
|
86 if (aLeft > current->iRight || aRight < current->iLeft) |
|
87 continue; |
|
88 |
|
89 // Overlapping block exists. Extend our block accordingly. |
|
90 if (aLeft > current->iLeft) |
|
91 aLeft = current->iLeft; |
|
92 if (aRight < current->iRight) |
|
93 aRight = current->iRight; |
|
94 |
|
95 // Delete the previously known overlapping block. |
|
96 iBytes -= (current->iRight - current->iLeft); |
|
97 current->iLink.Deque(); |
|
98 delete current; |
|
99 iCount--; |
|
100 } |
|
101 |
|
102 // Insert new block first in the queue. |
|
103 if (block = new SequenceBlock(aLeft, aRight), block != NULL) |
|
104 { |
|
105 AddFirst(*block); |
|
106 iOrdered = !iCount; |
|
107 iCount++; |
|
108 iBytes += (aRight - aLeft); |
|
109 } |
|
110 |
|
111 // Return the contiguous block, to which the new block belongs. |
|
112 return block; |
|
113 } |
|
114 |
|
115 |
|
116 // |
|
117 // Find a block that contains the given sequence number |
|
118 // |
|
119 EXPORT_C SequenceBlock *SequenceBlockQueue::Find(TTcpSeqNum aSeq) |
|
120 { |
|
121 SequenceBlock *current; |
|
122 SequenceBlockQueueIter iter(*this); |
|
123 |
|
124 while (current = iter++, current != NULL) |
|
125 { |
|
126 // Find correct position. |
|
127 if (aSeq >= current->iRight) |
|
128 continue; |
|
129 |
|
130 // Found? |
|
131 if (aSeq >= current->iLeft) |
|
132 return current; |
|
133 |
|
134 // We can stop here if we know the queue is ordered. |
|
135 if (iOrdered) |
|
136 break; |
|
137 } |
|
138 |
|
139 return NULL; |
|
140 } |
|
141 |
|
142 // |
|
143 // This only makes sense with ordered queues. |
|
144 // |
|
145 EXPORT_C TInt SequenceBlockQueue::FindGap(TTcpSeqNum& aLeft, TTcpSeqNum& aRight) |
|
146 { |
|
147 ASSERT(iOrdered); |
|
148 |
|
149 SequenceBlock *current; |
|
150 SequenceBlockQueueIter iter(*this); |
|
151 TInt skipped = 0; |
|
152 |
|
153 while (current = iter++, current != NULL) |
|
154 { |
|
155 TTcpSeqNum right = current->iRight; |
|
156 TTcpSeqNum left = current->iLeft; |
|
157 if (aLeft >= right) |
|
158 { |
|
159 skipped += (right - left); |
|
160 continue; |
|
161 } |
|
162 |
|
163 if (aLeft >= left) |
|
164 { |
|
165 skipped += (right - aLeft); |
|
166 aLeft = right; |
|
167 continue; |
|
168 } |
|
169 |
|
170 aRight = left; |
|
171 return skipped; |
|
172 } |
|
173 |
|
174 return -1; |
|
175 } |
|
176 |
|
177 |
|
178 // |
|
179 // Remove all contiguous blocks with sequence numbers |
|
180 // less than aLeft. |
|
181 // |
|
182 EXPORT_C void SequenceBlockQueue::Prune(TTcpSeqNum aLeft) |
|
183 { |
|
184 SequenceBlock *current; |
|
185 SequenceBlockQueueIter iter(*this); |
|
186 |
|
187 while (current = iter++, current != NULL) |
|
188 { |
|
189 TTcpSeqNum right = current->iRight; |
|
190 TTcpSeqNum left = current->iLeft; |
|
191 |
|
192 // The whole block can go? |
|
193 if (aLeft >= right) |
|
194 { |
|
195 iBytes -= (right - left); |
|
196 current->iLink.Deque(); |
|
197 delete current; |
|
198 --iCount; |
|
199 continue; |
|
200 } |
|
201 |
|
202 // Trim left edge of block. |
|
203 if (aLeft > left) |
|
204 { |
|
205 iBytes -= (aLeft - left); |
|
206 current->iLeft = aLeft; |
|
207 } |
|
208 |
|
209 // We can stop here if we know the queue is ordered. |
|
210 if (iOrdered) |
|
211 return; |
|
212 } |
|
213 |
|
214 if (iCount <= 1) |
|
215 iOrdered = ETrue; |
|
216 } |
|
217 |
|
218 |
|
219 // |
|
220 // Remove contiguous blocks from the end of the queue |
|
221 // until at most aCount blocks remain. |
|
222 // |
|
223 EXPORT_C void SequenceBlockQueue::Limit(TInt aCount) |
|
224 { |
|
225 SequenceBlock *current; |
|
226 SequenceBlockQueueIter iter(*this); |
|
227 |
|
228 iter.SetToLast(); |
|
229 while (iCount > aCount) |
|
230 { |
|
231 current = iter--; |
|
232 iBytes -= (current->iRight - current->iLeft); |
|
233 current->iLink.Deque(); |
|
234 delete current; |
|
235 --iCount; |
|
236 } |
|
237 |
|
238 if (iCount <= 1) |
|
239 iOrdered = ETrue; |
|
240 } |
|
241 |
|
242 |
|
243 // |
|
244 // Clear the queue. |
|
245 // |
|
246 EXPORT_C void SequenceBlockQueue::Clear() |
|
247 { |
|
248 SequenceBlock *current; |
|
249 SequenceBlockQueueIter iter(*this); |
|
250 |
|
251 while (current = iter++, current != NULL) |
|
252 { |
|
253 current->iLink.Deque(); |
|
254 delete current; |
|
255 } |
|
256 iCount = 0; |
|
257 iBytes = 0; |
|
258 iOrdered = ETrue; |
|
259 } |