Try to prevent websocket connections from pinging out.

This commit is contained in:
JoelKatz
2013-02-21 14:22:08 -08:00
parent 6357a906d3
commit 9508a4a7d6

View File

@@ -5,6 +5,7 @@
#include "../json/value.h" #include "../json/value.h"
#include <boost/weak_ptr.hpp> #include <boost/weak_ptr.hpp>
#include <boost/asio.hpp>
#include "WSDoor.h" #include "WSDoor.h"
#include "Application.h" #include "Application.h"
@@ -53,7 +54,7 @@ public:
WSConnection(WSServerHandler<endpoint_type>* wshpHandler, const connection_ptr& cpConnection) 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) mPingTimer(cpConnection->get_io_service()), mPinged(false)
{ {
mRemoteIP = cpConnection->get_socket().lowest_layer().remote_endpoint().address().to_string(); mRemoteIP = cpConnection->get_socket().lowest_layer().remote_endpoint().address().to_string();
cLog(lsDEBUG) << "Websocket connection from " << mRemoteIP; cLog(lsDEBUG) << "Websocket connection from " << mRemoteIP;
@@ -62,6 +63,7 @@ public:
void preDestroy() void preDestroy()
{ // sever connection { // sever connection
mPingTimer.cancel();
mConnection.reset(); mConnection.reset();
} }
@@ -143,10 +145,10 @@ public:
bool onPingTimer() bool onPingTimer()
{ {
if (mPinged) if (mPinged)
return true; return true; // causes connection to close
mPinged = true; mPinged = true;
setPingTimer(); setPingTimer();
return false; return false; // causes ping to be sent
} }
void onPong() void onPong()
@@ -154,8 +156,11 @@ public:
mPinged = false; mPinged = false;
} }
static void pingTimer(weak_connection_ptr c, WSServerHandler<endpoint_type>* h) static void pingTimer(weak_connection_ptr c, WSServerHandler<endpoint_type>* h, const boost::system::error_code& e)
{ {
if (e)
return;
connection_ptr ptr = c.lock(); connection_ptr ptr = c.lock();
if (ptr) if (ptr)
h->pingTimer(ptr); h->pingTimer(ptr);
@@ -164,10 +169,10 @@ public:
void setPingTimer() void setPingTimer()
{ {
mPingTimer.expires_from_now(boost::posix_time::seconds(WEBSOCKET_PING_FREQUENCY)); mPingTimer.expires_from_now(boost::posix_time::seconds(WEBSOCKET_PING_FREQUENCY));
mPingTimer.async_wait(boost::bind(&WSConnection<endpoint_type>::pingTimer, mConnection, mHandler)); mPingTimer.async_wait(boost::bind(
&WSConnection<endpoint_type>::pingTimer, mConnection, mHandler, boost::asio::placeholders::error));
} }
}; };
// vim:ts=4 // vim:ts=4