-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HeartBeatMonitor and using keepalive
- Loading branch information
Showing
4 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 55 additions & 1 deletion
56
src/Microsoft.AspNet.SignalR.Client.Cpp/HeartBeatMonitor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,68 @@ | ||
#include "Connection.h" | ||
#include "HeartBeatMonitor.h" | ||
|
||
namespace MicrosoftAspNetSignalRClientCpp | ||
{ | ||
|
||
HeartBeatMonitor::HeartBeatMonitor(shared_ptr<Connection> connection, shared_ptr<mutex> connectionStateLock) | ||
HeartBeatMonitor::HeartBeatMonitor() | ||
{ | ||
} | ||
|
||
HeartBeatMonitor::HeartBeatMonitor(shared_ptr<Connection> connection, shared_ptr<recursive_mutex> connectionStateLock) | ||
{ | ||
pConnection = connection; | ||
pConnectionStateLock = connectionStateLock; | ||
} | ||
|
||
HeartBeatMonitor::~HeartBeatMonitor() | ||
{ | ||
} | ||
|
||
void HeartBeatMonitor::Start() | ||
{ | ||
pConnection->UpdateLaskKeepAlive(); | ||
mHasBeenWarned = false; | ||
mTimedOut = false; | ||
int period = pConnection->GetKeepAliveData()->GetCheckInterval()*1000; | ||
mTimer.start(pConnection->GetKeepAliveData()->GetCheckInterval()*1000, true, Beat, this); | ||
} | ||
|
||
void HeartBeatMonitor::Beat(void* state) | ||
{ | ||
auto monitor = static_cast<HeartBeatMonitor*>(state); | ||
monitor->Beat((int)difftime(time(0), monitor->pConnection->GetKeepAliveData()->GetLastKeepAlive())); | ||
} | ||
|
||
void HeartBeatMonitor::Beat(int timeElapsed) | ||
{ | ||
lock_guard<recursive_mutex> lock(*pConnectionStateLock.get()); | ||
|
||
if (pConnection->GetState() == ConnectionState::Connected) | ||
{ | ||
if (timeElapsed >= pConnection->GetKeepAliveData()->GetTimeout()) | ||
{ | ||
if (!mTimedOut) | ||
{ | ||
//trace | ||
mTimedOut = true; | ||
pConnection->GetTransport()->LostConnection(pConnection); | ||
} | ||
} | ||
else if (timeElapsed >= pConnection->GetKeepAliveData()->GetTimeout()) | ||
{ | ||
if (!mHasBeenWarned) | ||
{ | ||
//trace | ||
mHasBeenWarned = true; | ||
pConnection->OnConnectionSlow(); | ||
} | ||
} | ||
else | ||
{ | ||
mHasBeenWarned = false; | ||
mTimedOut = false; | ||
} | ||
} | ||
} | ||
|
||
} // namespace MicrosoftAspNetSignalRClientCpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters