Permalink
Browse files

Improved waitFor and added waitForEmptySendQueue (to make sure all qu…

…eued messages are sent)
  • Loading branch information...
1 parent 9645a77 commit a39f4c266fad555ddb7cf69015982439b609eab3 @HakanL committed Nov 3, 2011
Showing with 35 additions and 3 deletions.
  1. +2 −1 .gitignore
  2. +33 −2 cometd/client/BayeuxClient.cs
View
@@ -2,4 +2,5 @@
*.suo
/cometd2.dll
/cometd2.pdb
-/obj
+/obj
+*.user
@@ -230,11 +230,15 @@ public State waitFor(int waitMs, ICollection<State> states)
DateTime stop = DateTime.Now.AddMilliseconds(waitMs);
int duration = waitMs;
+ State s = CurrentState;
+ if(states.Contains(s))
+ return s;
+
while (stateChanged.WaitOne(duration))
{
if (stateUpdateInProgress == 0)
{
- State s = CurrentState;
+ s = CurrentState;
if (states.Contains(s))
return s;
}
@@ -243,6 +247,10 @@ public State waitFor(int waitMs, ICollection<State> states)
if (duration <= 0) break;
}
+ s = CurrentState;
+ if (states.Contains(s))
+ return s;
+
return State.INVALID;
}
@@ -304,6 +312,29 @@ protected bool sendMessages(IList<IMutableMessage> messages)
}
}
+ /// <summary>
+ /// Wait for send queue to be emptied
+ /// </summary>
+ /// <param name="timeoutMS"></param>
+ /// <returns>true if queue is empty, false if timed out</returns>
+ public bool waitForEmptySendQueue(int timeoutMS)
+ {
+ if (messageQueue.Count == 0)
+ return true;
+
+ DateTime start = DateTime.Now;
+
+ while ((DateTime.Now - start).TotalMilliseconds < timeoutMS)
+ {
+ if (messageQueue.Count == 0)
+ return true;
+
+ System.Threading.Thread.Sleep(100);
+ }
+
+ return false;
+ }
+
private IList<IMutableMessage> takeMessages()
{
IList<IMutableMessage> queue = new List<IMutableMessage>(messageQueue);
@@ -602,7 +633,7 @@ protected void enqueueSend(IMutableMessage message)
private bool canSend()
{
- return !this.Batching && !isHandshaking(this.bayeuxClientState);
+ return !isDisconnected(this.bayeuxClientState) && !this.Batching && !isHandshaking(this.bayeuxClientState);
}
protected void failMessages(Exception x, IList<IMessage> messages)

0 comments on commit a39f4c2

Please sign in to comment.