Ping websocket connections every two minutes.

Detect and close non-responsive connections.
UNTESTED
This commit is contained in:
JoelKatz
2012-12-25 10:19:24 -08:00
parent 7c13c57638
commit 9124c91884
4 changed files with 88 additions and 15 deletions

View File

@@ -8,13 +8,17 @@
#include "WSDoor.h"
#include "Application.h"
#include "Log.h"
#include "NetworkOPs.h"
#include "CallRPC.h"
#include "InstanceCounter.h"
#include "Log.h"
DEFINE_INSTANCE(WebSocketConnection);
#ifndef WEBSOCKET_PING_FREQUENCY
#define WEBSOCKET_PING_FREQUENCY 120
#endif
template <typename endpoint_type>
class WSServerHandler;
//
@@ -33,9 +37,12 @@ public:
protected:
typedef void (WSConnection::*doFuncPtr)(Json::Value& jvResult, Json::Value &jvRequest);
WSServerHandler<endpoint_type>* mHandler;
weak_connection_ptr mConnection;
NetworkOPs& mNetwork;
WSServerHandler<endpoint_type>* mHandler;
weak_connection_ptr mConnection;
NetworkOPs& mNetwork;
boost::asio::deadline_timer mPingTimer;
bool mPinged;
public:
// WSConnection()
@@ -43,17 +50,11 @@ public:
// mConnection(connection_ptr()) { ; }
WSConnection(WSServerHandler<endpoint_type>* wshpHandler, const connection_ptr& cpConnection)
: mHandler(wshpHandler), mConnection(cpConnection), mNetwork(theApp->getOPs()) { ; }
: mHandler(wshpHandler), mConnection(cpConnection), mNetwork(theApp->getOPs()),
mPingTimer(theApp->getAuxService()), mPinged(false)
{ setPingTimer(); }
virtual ~WSConnection()
{
mNetwork.unsubTransactions(this);
mNetwork.unsubRTTransactions(this);
mNetwork.unsubLedger(this);
mNetwork.unsubServer(this);
mNetwork.unsubAccount(this, mSubAccountInfo, true);
mNetwork.unsubAccount(this, mSubAccountInfo, false);
}
virtual ~WSConnection() { ; }
// Implement overridden functions from base class:
void send(const Json::Value& jvObj)
@@ -109,6 +110,34 @@ public:
return jvResult;
}
bool onPingTimer()
{
if (mPinged)
return true;
mPinged = true;
setPingTimer();
return false;
}
void onPong()
{
mPinged = false;
}
static void pingTimer(weak_connection_ptr c, WSServerHandler<endpoint_type>* h)
{
connection_ptr ptr = c.lock();
if (ptr)
h->pingTimer(ptr);
}
void setPingTimer()
{
mPingTimer.expires_from_now(boost::posix_time::seconds(WEBSOCKET_PING_FREQUENCY));
mPingTimer.async_wait(boost::bind(&WSConnection<endpoint_type>::pingTimer, mConnection, mHandler));
}
};