Skip to content

Commit be351dd

Browse files
author
Henrik Grotle
committed
batching in .net20 client
1 parent 6092e7e commit be351dd

1 file changed

Lines changed: 67 additions & 26 deletions

File tree

net20/SMSClient/SMSClient.cs

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public void Remove(int key)
5959
Dictionary.Remove(key);
6060
}
6161

62+
internal MessageCollection Clone() {
63+
var clone = new MessageCollection();
64+
foreach (int i in Dictionary.Keys)
65+
{
66+
clone.Add(i, (Message)Dictionary[i]);
67+
}
68+
return clone;
69+
}
70+
6271
}
6372

6473
#endregion
@@ -141,8 +150,9 @@ public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToRe
141150
}
142151

143152
#endregion
144-
145-
private string _Username;
153+
154+
private int _batchSize = 100;
155+
private string _Username;
146156
private string _Password;
147157
private string _PrimaryGateway;
148158
private string _SecondaryGateway;
@@ -167,6 +177,20 @@ public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToRe
167177
/// </summary>
168178
public DeliveryReportCollection DeliveryReports { get; private set; }
169179

180+
private MessageCollection _messagesToSend;
181+
182+
public int BatchSize
183+
{
184+
get
185+
{
186+
return _batchSize;
187+
}
188+
set
189+
{
190+
_batchSize = value;
191+
}
192+
}
193+
170194
#region Accessors
171195
/// <summary>
172196
/// Username on SMS Gateway
@@ -265,26 +289,34 @@ public bool SendMessagesBySocket(string url, int port)
265289
/// </summary>
266290
public void SendMessages()
267291
{
292+
if (_messagesToSend == null || _messagesToSend.Count == 0)
293+
{
294+
_messagesToSend = Messages.Clone();
295+
}
268296

269-
XmlDocument doc = GetDocumentXml();
270-
XmlDocument docResponse = null;
271-
try
272-
{
273-
docResponse = HttpPost(doc, _PrimaryGateway);
274-
}
275-
catch(Exception e)
276-
{
277-
// Failed to post using primary gateway, let's try secondary if given
278-
if(_SecondaryGateway != null && _SecondaryGateway.Length > 0)
279-
{
280-
docResponse = HttpPost(doc, _SecondaryGateway);
281-
}
282-
else
283-
{
284-
throw;
285-
}
286-
}
287-
CheckResponse(docResponse);
297+
while (_messagesToSend.Count > 0)
298+
{
299+
300+
XmlDocument doc = GetDocumentXml();
301+
XmlDocument docResponse = null;
302+
try
303+
{
304+
docResponse = HttpPost(doc, _PrimaryGateway);
305+
}
306+
catch(Exception e)
307+
{
308+
// Failed to post using primary gateway, let's try secondary if given
309+
if(_SecondaryGateway != null && _SecondaryGateway.Length > 0)
310+
{
311+
docResponse = HttpPost(doc, _SecondaryGateway);
312+
}
313+
else
314+
{
315+
throw;
316+
}
317+
}
318+
CheckResponse(docResponse);
319+
}
288320
}
289321

290322
/// <summary>
@@ -326,11 +358,20 @@ private XmlDocument GetDocumentXml()
326358
if(_AffiliateProgram != null && _AffiliateProgram.Length > 0)
327359
elmSession.AppendChild(CreateElement(doc, "AP", _AffiliateProgram));
328360

329-
XmlElement elmMsgList = doc.CreateElement("MSGLST");
330-
foreach(int i in Messages.Keys)
331-
{
332-
elmMsgList.AppendChild(GetMessageXml(doc, Messages[i], i));
333-
}
361+
int _currentBatchSize = 0;
362+
XmlElement elmMsgList = doc.CreateElement("MSGLST");
363+
364+
int[] keys = new int[_messagesToSend.Keys.Count];
365+
_messagesToSend.Keys.CopyTo(keys, 0);
366+
367+
foreach (int i in keys)
368+
{
369+
elmMsgList.AppendChild(GetMessageXml(doc, _messagesToSend[i], i));
370+
_messagesToSend.Remove(i);
371+
_currentBatchSize++;
372+
if (_batchSize > 0 && _currentBatchSize == _batchSize)
373+
break;
374+
}
334375
elmSession.AppendChild(elmMsgList);
335376
doc.AppendChild(elmSession);
336377

0 commit comments

Comments
 (0)