Skip to content

Commit

Permalink
Added HeartBeatMonitor and using keepalive
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTaoLuo committed Jul 18, 2013
1 parent d2ca5c3 commit 3351de7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client.Cpp/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pplx::task<void> Connection::Start(shared_ptr<IClientTransport> transport)
return mConnectTask;
}

mMonitor = HeartBeatMonitor(shared_from_this(), shared_ptr<recursive_mutex>(&mStateLock, [](recursive_mutex *l){}));
pTransport = transport;
mConnectTask = Negotiate(transport);

Expand Down Expand Up @@ -197,6 +198,11 @@ pplx::task<void> Connection::StartTransport()
lock_guard<recursive_mutex> lock(mStateLock);
mConnectingMessageBuffer.Drain();
}

if (pKeepAliveData != nullptr && pTransport->SupportsKeepAlive())
{
mMonitor.Start();
}
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client.Cpp/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace MicrosoftAspNetSignalRClientCpp
mutex mStartLock;
ConnectionState mState;
ConnectingMessageBuffer mConnectingMessageBuffer;
HeartBeatMonitor mMonitor;
shared_ptr<KeepAliveData> pKeepAliveData;
shared_ptr<IClientTransport> pTransport;
unique_ptr<pplx::cancellation_token_source> pDisconnectCts;
Expand Down Expand Up @@ -126,5 +127,6 @@ namespace MicrosoftAspNetSignalRClientCpp
friend class TransportAbortHandler;
friend class ServerSentEventsTransport;
friend class TransportHelper;
friend class HeartBeatMonitor;
};
} // namespace MicrosoftAspNetSignalRClientCpp
56 changes: 55 additions & 1 deletion src/Microsoft.AspNet.SignalR.Client.Cpp/HeartBeatMonitor.cpp
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
10 changes: 8 additions & 2 deletions src/Microsoft.AspNet.SignalR.Client.Cpp/HeartBeatMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ namespace MicrosoftAspNetSignalRClientCpp
class HeartBeatMonitor
{
public:
HeartBeatMonitor(shared_ptr<Connection> connection, shared_ptr<mutex> connectionStateLock);
HeartBeatMonitor();
HeartBeatMonitor(shared_ptr<Connection> connection, shared_ptr<recursive_mutex> connectionStateLock);
~HeartBeatMonitor();

void Start();

private:
bool mHasBeenWarned;
bool mTimedOut;
pplx::details::timer_t mTimer;
shared_ptr<mutex> pConnectionStateLock;
shared_ptr<recursive_mutex> pConnectionStateLock;
shared_ptr<Connection> pConnection;

static void Beat(void* state);
void Beat(int timeElapsed);
};
}

0 comments on commit 3351de7

Please sign in to comment.