Added CEvent semaphore to PostToServer to sequence posts

This commit is contained in:
Heath Volmer
2018-01-02 19:58:39 -07:00
parent 026ab438ad
commit 0fb946c7d5
3 changed files with 11 additions and 6 deletions

View File

@@ -23,7 +23,7 @@ namespace PepperDash.Essentials
/// <summary> /// <summary>
/// Prevents post operations from stomping on each other and getting lost /// Prevents post operations from stomping on each other and getting lost
/// </summary> /// </summary>
CMutex PostLock = new CMutex(); CEvent PostLockEvent = new CEvent(true, true);
CotijaConfig Config; CotijaConfig Config;
@@ -211,12 +211,14 @@ namespace PepperDash.Essentials
/// <param name="o">object to be serialized and sent in post body</param> /// <param name="o">object to be serialized and sent in post body</param>
public void PostToServer(EssentialsRoomBase room, JObject o) public void PostToServer(EssentialsRoomBase room, JObject o)
{ {
var go = PostLock.WaitForMutex(2000); var ready = PostLockEvent.Wait(2000);
if (!go) if (!ready)
{ {
Debug.Console(0, this, "Mutex not released after 2 seconds. Skipping message"); Debug.Console(1, this, "PostToServer failed to enter after 2 seconds. Ignoring");
return; return;
} }
PostLockEvent.Reset();
try try
{ {
if (Client == null || NeedNewClient) if (Client == null || NeedNewClient)
@@ -242,20 +244,23 @@ namespace PepperDash.Essentials
Debug.Console(1, this, "POST result: {0}", err); Debug.Console(1, this, "POST result: {0}", err);
if (err == HTTP_CALLBACK_ERROR.COMPLETED) if (err == HTTP_CALLBACK_ERROR.COMPLETED)
{
Debug.Console(1, this, "Status Response Code: {0}", r.Code); Debug.Console(1, this, "Status Response Code: {0}", r.Code);
PostLockEvent.Set();
}
else else
{ {
// Try again. This client is hosed. // Try again. This client is hosed.
NeedNewClient = true; NeedNewClient = true;
PostLockEvent.Set();
PostToServer(room, o); PostToServer(room, o);
} }
PostLock.ReleaseMutex();
}); });
} }
catch(Exception e) catch(Exception e)
{ {
Debug.Console(1, this, "Error Posting to Server: {0}", e); Debug.Console(1, this, "Error Posting to Server: {0}", e);
PostLock.ReleaseMutex(); PostLockEvent.Set();
} }
} }