Whitespace: clean WebSockets to 80 columns.

This commit is contained in:
Tom Ritchford
2014-12-07 14:00:10 -05:00
committed by Nik Bougalis
parent 85fc9e4ecf
commit 69f5c6987a
4 changed files with 85 additions and 45 deletions

View File

@@ -28,7 +28,8 @@ namespace ripple {
WSConnection::WSConnection (HTTP::Port const& port,
Resource::Manager& resourceManager, Resource::Consumer usage,
InfoSub::Source& source, bool isPublic,
beast::IP::Endpoint const& remoteAddress, boost::asio::io_service& io_service)
beast::IP::Endpoint const& remoteAddress,
boost::asio::io_service& io_service)
: InfoSub (source, usage)
, port_(port)
, m_resourceManager (resourceManager)
@@ -54,7 +55,8 @@ void WSConnection::onPong (std::string const&)
m_sentPing = false;
}
void WSConnection::rcvMessage (message_ptr msg, bool& msgRejected, bool& runQueue)
void WSConnection::rcvMessage (
message_ptr msg, bool& msgRejected, bool& runQueue)
{
ScopedLockType sl (m_receiveQueueMutex);
@@ -65,7 +67,8 @@ void WSConnection::rcvMessage (message_ptr msg, bool& msgRejected, bool& runQueu
return;
}
if ((m_receiveQueue.size () >= 1000) || (msg->get_payload().size() > 1000000))
if ((m_receiveQueue.size () >= 1000) ||
(msg->get_payload().size() > 1000000))
{
msgRejected = true;
runQueue = false;
@@ -156,7 +159,8 @@ Json::Value WSConnection::invokeCommand (Json::Value& jvRequest)
}
Resource::Charge loadType = Resource::feeReferenceRPC;
RPCHandler mRPCHandler (m_netOPs, std::dynamic_pointer_cast<InfoSub> (this->shared_from_this ()));
RPCHandler mRPCHandler (m_netOPs, std::dynamic_pointer_cast<InfoSub> (
this->shared_from_this ()));
Json::Value jvResult (Json::objectValue);
Role const role = port_.allow_admin ? adminRole (port_, jvRequest,
@@ -168,7 +172,8 @@ Json::Value WSConnection::invokeCommand (Json::Value& jvRequest)
}
else
{
jvResult[jss::result] = mRPCHandler.doCommand (jvRequest, role, loadType);
jvResult[jss::result] = mRPCHandler.doCommand (
jvRequest, role, loadType);
}
getConsumer().charge (loadType);
@@ -191,15 +196,15 @@ Json::Value WSConnection::invokeCommand (Json::Value& jvRequest)
}
else
{
jvResult[jss::status] = jss::success;
jvResult[jss::status] = jss::success;
}
if (jvRequest.isMember (jss::id))
{
jvResult[jss::id] = jvRequest[jss::id];
jvResult[jss::id] = jvRequest[jss::id];
}
jvResult[jss::type] = jss::response;
jvResult[jss::type] = jss::response;
return jvResult;
}

View File

@@ -45,7 +45,8 @@ protected:
WSConnection (HTTP::Port const& port,
Resource::Manager& resourceManager, Resource::Consumer usage,
InfoSub::Source& source, bool isPublic,
beast::IP::Endpoint const& remoteAddress, boost::asio::io_service& io_service);
beast::IP::Endpoint const& remoteAddress,
boost::asio::io_service& io_service);
WSConnection(WSConnection const&) = delete;
WSConnection& operator= (WSConnection const&) = delete;
@@ -101,12 +102,15 @@ private:
weak_connection_ptr m_connection;
public:
WSConnectionType (Resource::Manager& resourceManager, InfoSub::Source& source,
server_type& serverHandler, connection_ptr const& cpConnection)
WSConnectionType (Resource::Manager& resourceManager,
InfoSub::Source& source,
server_type& serverHandler,
connection_ptr const& cpConnection)
: WSConnection (
serverHandler.port(),
resourceManager,
resourceManager.newInboundEndpoint (cpConnection->get_socket ().remote_endpoint ()),
resourceManager.newInboundEndpoint (
cpConnection->get_socket ().remote_endpoint ()),
source,
serverHandler.getPublic (),
cpConnection->get_socket ().remote_endpoint (),
@@ -204,7 +208,8 @@ public:
m_pingTimer.async_wait (ptr->get_strand ().wrap (
std::bind (&WSConnectionType <endpoint_type>::pingTimer,
m_connection, &m_serverHandler, beast::asio::placeholders::error)));
m_connection, &m_serverHandler,
beast::asio::placeholders::error)));
}
}
};

View File

@@ -85,7 +85,8 @@ private:
{
ScopedLockType lock (m_endpointLock);
m_endpoint = std::make_shared<websocketpp::server_autotls> (handler);
m_endpoint = std::make_shared<websocketpp::server_autotls> (
handler);
}
// Call the main-event-loop of the websocket server.
@@ -95,9 +96,11 @@ private:
}
catch (websocketpp::exception& e)
{
WriteLog (lsWARNING, WSDoor) << "websocketpp exception: " << e.what ();
WriteLog (lsWARNING, WSDoor) << "websocketpp exception: "
<< e.what ();
// temporary workaround for websocketpp throwing exceptions on access/close races
// temporary workaround for websocketpp throwing exceptions on
// access/close races
for (;;)
{
// https://github.com/zaphoyd/websocketpp/issues/98
@@ -108,7 +111,8 @@ private:
}
catch (websocketpp::exception& e)
{
WriteLog (lsWARNING, WSDoor) << "websocketpp exception: " << e.what ();
WriteLog (lsWARNING, WSDoor) << "websocketpp exception: "
<< e.what ();
}
}
}
@@ -169,4 +173,3 @@ make_WSDoor (HTTP::Port const& port, Resource::Manager& resourceManager,
}
}

View File

@@ -107,40 +107,47 @@ public:
}
catch (...)
{
cpClient->close (websocketpp::close::status::value (crTooSlow), std::string ("Client is too slow."));
cpClient->close (websocketpp::close::status::value (crTooSlow),
std::string ("Client is too slow."));
}
}
static void ssendb (connection_ptr cpClient, std::string const& strMessage, bool broadcast)
static void ssendb (connection_ptr cpClient, std::string const& strMessage,
bool broadcast)
{
try
{
WriteLog (broadcast ? lsTRACE : lsDEBUG, WSServerHandlerLog) << "Ws:: Sending '" << strMessage << "'";
WriteLog (broadcast ? lsTRACE : lsDEBUG, WSServerHandlerLog)
<< "Ws:: Sending '" << strMessage << "'";
cpClient->send (strMessage);
}
catch (...)
{
cpClient->close (websocketpp::close::status::value (crTooSlow), std::string ("Client is too slow."));
cpClient->close (websocketpp::close::status::value (crTooSlow),
std::string ("Client is too slow."));
}
}
void send (connection_ptr cpClient, message_ptr mpMessage)
{
cpClient->get_strand ().post (std::bind (
&WSServerHandler<endpoint_type>::ssend, cpClient, mpMessage));
cpClient->get_strand ().post (
std::bind (
&WSServerHandler<endpoint_type>::ssend,
cpClient, mpMessage));
}
void send (connection_ptr cpClient, std::string const& strMessage, bool broadcast)
void send (connection_ptr cpClient, std::string const& strMessage,
bool broadcast)
{
cpClient->get_strand ().post (std::bind (
&WSServerHandler<endpoint_type>::ssendb, cpClient, strMessage, broadcast));
cpClient->get_strand ().post (
std::bind (
&WSServerHandler<endpoint_type>::ssendb, cpClient, strMessage,
broadcast));
}
void send (connection_ptr cpClient, Json::Value const& jvObj, bool broadcast)
{
// WriteLog (lsDEBUG, WSServerHandlerLog) << "Ws:: Object '" << jfwWriter.write(jvObj) << "'";
send (cpClient, to_string (jvObj), broadcast);
}
@@ -164,7 +171,9 @@ public:
try
{
WriteLog (lsDEBUG, WSServerHandlerLog) <<
"Ws:: ping_out(" << cpClient->get_socket ().remote_endpoint ().to_string () << ")";
"Ws:: ping_out(" <<
cpClient->get_socket ().remote_endpoint ().to_string () <<
")";
}
catch (...)
{
@@ -198,12 +207,16 @@ public:
{
std::pair <typename MapType::iterator, bool> const result (
mMap.emplace (cpClient,
std::make_shared < WSConnectionType <endpoint_type> > (std::ref(m_resourceManager),
std::ref (m_source), std::ref(*this), std::cref(cpClient))));
std::make_shared < WSConnectionType <endpoint_type> > (
std::ref(m_resourceManager),
std::ref (m_source),
std::ref(*this),
std::cref(cpClient))));
assert (result.second);
(void) result.second;
WriteLog (lsDEBUG, WSServerHandlerLog) <<
"Ws:: on_open(" << cpClient->get_socket ().remote_endpoint() << ")";
"Ws:: on_open(" <<
cpClient->get_socket ().remote_endpoint() << ")";
}
catch (...)
{
@@ -245,7 +258,8 @@ public:
void doClose (connection_ptr const& cpClient, char const* reason)
{
// we cannot destroy the connection while holding the map lock or we deadlock with pubLedger
// we cannot destroy the connection while holding the map lock or we
// deadlock with pubLedger
wsc_ptr ptr;
{
ScopedLockType sl (mLock);
@@ -257,7 +271,8 @@ public:
{
WriteLog (lsDEBUG, WSServerHandlerLog) <<
"Ws:: " << reason << "(" <<
cpClient->get_socket ().remote_endpoint() << ") not found";
cpClient->get_socket ().remote_endpoint() <<
") not found";
}
catch (...)
{
@@ -265,7 +280,9 @@ public:
return;
}
ptr = it->second; // prevent the WSConnection from being destroyed until we release the lock
ptr = it->second;
// prevent the WSConnection from being destroyed until we release
// the lock
mMap.erase (it);
}
ptr->preDestroy (); // Must be done before we return
@@ -280,8 +297,10 @@ public:
}
// Must be done without holding the websocket send lock
getApp().getJobQueue ().addJob (jtCLIENT, "WSClient::destroy",
std::bind (&WSConnectionType <endpoint_type>::destroy, ptr));
getApp().getJobQueue ().addJob (
jtCLIENT,
"WSClient::destroy",
std::bind (&WSConnectionType <endpoint_type>::destroy, ptr));
}
void on_message (connection_ptr cpClient, message_ptr mpMessage)
@@ -305,7 +324,8 @@ public:
try
{
WriteLog (lsDEBUG, WSServerHandlerLog) <<
"Ws:: Rejected(" << cpClient->get_socket().remote_endpoint() <<
"Ws:: Rejected(" <<
cpClient->get_socket().remote_endpoint() <<
") '" << mpMessage->get_payload () << "'";
}
catch (...)
@@ -357,7 +377,8 @@ public:
std::placeholders::_1, cpClient));
}
bool do_message (Job& job, const connection_ptr& cpClient, const wsc_ptr& conn, const message_ptr& mpMessage)
bool do_message (Job& job, const connection_ptr& cpClient,
const wsc_ptr& conn, const message_ptr& mpMessage)
{
Json::Value jvRequest;
Json::Reader jrReader;
@@ -377,11 +398,13 @@ public:
Json::Value jvResult (Json::objectValue);
jvResult[jss::type] = jss::error;
jvResult[jss::error] = "wsTextRequired"; // We only accept text messages.
jvResult[jss::error] = "wsTextRequired";
// We only accept text messages.
send (cpClient, jvResult, false);
}
else if (!jrReader.parse (mpMessage->get_payload (), jvRequest) || jvRequest.isNull () || !jvRequest.isObject ())
else if (!jrReader.parse (mpMessage->get_payload (), jvRequest) ||
jvRequest.isNull () || !jvRequest.isObject ())
{
Json::Value jvResult (Json::objectValue);
@@ -431,13 +454,17 @@ public:
if (!serverOkay (reason))
{
cpClient->set_body (std::string ("<HTML><BODY>Server cannot accept clients: ") + reason + "</BODY></HTML>");
cpClient->set_body (
"<HTML><BODY>Server cannot accept clients: " +
reason + "</BODY></HTML>");
return false;
}
cpClient->set_body (
"<!DOCTYPE html><html><head><title>" SYSTEM_NAME " Test</title></head>"
"<body><h1>" SYSTEM_NAME " Test</h1><p>This page shows http(s) connectivity is working.</p></body></html>");
"<!DOCTYPE html><html><head><title>" SYSTEM_NAME " Test</title>"
"</head><body><h1>" SYSTEM_NAME " Test</h1>"
"<p>This page shows that http(s) connectivity is working."
"</p></body></html>");
return true;
}
};