Skip to content

Commit

Permalink
Max connections error (#123)
Browse files Browse the repository at this point in the history
* Output a better error code for max connections

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Documentation

Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Nate Koenig <nate@openrobotics.org>
  • Loading branch information
nkoenig and Nate Koenig authored Jul 16, 2021
1 parent be46811 commit 605614a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
30 changes: 17 additions & 13 deletions plugins/websocket_server/WebsocketServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,6 @@ int rootCallback(struct lws *_wsi,
// std::lock_guard<std::mutex> mainLock(self->mutex);
switch (_reason)
{
// Filter network connections.
case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
// Prevent too many connections.
if (self->maxConnections >= 0 &&
self->connections.size()+1 > self->maxConnections)
{
ignerr << "Skipping new connection, limit of "
<< self->maxConnections << " has been reached\n";
// Return non-zero to close the connection.
return -1;
}
break;

// Open connections.
case LWS_CALLBACK_ESTABLISHED:
igndbg << "LWS_CALLBACK_ESTABLISHED\n";
Expand Down Expand Up @@ -300,6 +287,23 @@ int rootCallback(struct lws *_wsi,
// Handle incoming messages
case LWS_CALLBACK_RECEIVE:
igndbg << "LWS_CALLBACK_RECEIVE\n";

// Prevent too many connections.
if (self->maxConnections >= 0 &&
self->connections.size()+1 > self->maxConnections)
{
ignerr << "Skipping new connection, limit of "
<< self->maxConnections << " has been reached\n";

// This will return an error code of 1008 with a reason of
// "max_connections".
std::string reason = "max_connections";
lws_close_reason(_wsi, LWS_CLOSE_STATUS_POLICY_VIOLATION,
reinterpret_cast<unsigned char *>(reason.data()), reason.size());

// Return non-zero to close the connection.
return -1;
}
self->OnMessage(fd, std::string((const char *)_in));
break;

Expand Down
9 changes: 8 additions & 1 deletion plugins/websocket_server/WebsocketServer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
Expand Down Expand Up @@ -52,6 +52,13 @@ namespace ignition
/// "auth" call on the websocket. If the <authorization_key> is set, then
/// the connection can provide that key.
///
/// * <max_connections> : An integer that specifies the maximum number
/// of active websocket connections. A value less than zero indicates an
/// unlimited number, this is the default. A websocket client error
/// code of 1008 along with a reason set to "max_connections" will be
/// returned if a new connection is rejected due to the max connection
/// threshold.
///
/// * <ssl> : Element that contains SSL configuration. For testing
/// purposes you can create self-signed SSL certificates. Run
///
Expand Down

0 comments on commit 605614a

Please # to comment.