Skip to content

Commit

Permalink
[Core/WebSocketLink] : Enable unsollicited 'Pong' and rename enum item (
Browse files Browse the repository at this point in the history
#1784)

* [WebSocket/WebSocketLink] : Enable unsollicited 'Pong' and rename enum item

- Rename 'WEBSERVER' to 'WEBSERVICE' as both client and server utilize state information

- Unsollicited 'Pong' may act as a heart beat

* [WebSocket/WebSocketLink] : METROL-1087

* [WebSocket/WebSocketLink] : rename forgotten 'WEBSERVER' to 'WEBSERVICE'

---------

Co-authored-by: Pierre Wielders <pierre@wielders.net>
  • Loading branch information
msieben and pwielders authored Nov 7, 2024
1 parent 1ecc540 commit 5712a36
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions Source/websocket/WebSocketLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace Web {
CLOSE = 0x08,
PING = 0x09,
PONG = 0x0A,
// Reserved ranges
// 0x3-0x7
// 0xB-0xF
// Following are outside reseved 4-bit ranges
VIOLATION = 0x10, // e.g. a control package without a FIN flag
TOO_BIG = 0x20, // Protocol max support for 2^16 message per chunk
INCONSISTENT = 0x30 // e.g. Protocol defined as Text, but received a binary.
Expand Down Expand Up @@ -182,13 +186,15 @@ namespace Web {
class WebSocketLinkType {
public:
enum EnumlinkState : uint8_t {
WEBSERVER = 0x01,
WEBSERVICE = 0x01,
UPGRADING = 0x02,
WEBSOCKET = 0x04,
SUSPENDED = 0x08,
ACTIVITY = 0x10
};

DEPRECATED constexpr static EnumlinkState WEBSERVER { EnumlinkState::WEBSERVICE };

typedef WebSocketLinkType<LINK, INBOUND, OUTBOUND, ALLOCATOR> ParentClass;

private:
Expand Down Expand Up @@ -373,7 +379,7 @@ PUSH_WARNING(DISABLE_WARNING_THIS_IN_MEMBER_INITIALIZER_LIST)
, _handler(binary, masking)
, _parent(parent)
, _adminLock()
, _state(WEBSERVER)
, _state(WEBSERVICE)
, _serializerImpl(*this, queueSize)
, _deserialiserImpl(*this, queueSize)
, _path()
Expand All @@ -390,7 +396,7 @@ PUSH_WARNING(DISABLE_WARNING_THIS_IN_MEMBER_INITIALIZER_LIST)
, _handler(binary, masking)
, _parent(parent)
, _adminLock()
, _state(WEBSERVER)
, _state(WEBSERVICE)
, _serializerImpl(*this, queueSize)
, _deserialiserImpl(*this, allocator)
, _path()
Expand Down Expand Up @@ -419,7 +425,7 @@ POP_WARNING()
}
bool IsWebServer() const
{
return ((State() & WEBSERVER) != 0);
return ((State() & WEBSERVICE) != 0);
}
bool IsUpgrading() const
{
Expand Down Expand Up @@ -477,6 +483,18 @@ POP_WARNING()

ACTUALLINK::Trigger();
}
void Pong()
{
_pingFireTime = Core::Time::Now().Ticks();

_adminLock.Lock();

_handler.Pong();

_adminLock.Unlock();

ACTUALLINK::Trigger();
}
bool Masking() const
{
return (_handler.Masking());
Expand Down Expand Up @@ -775,7 +793,7 @@ POP_WARNING()
// Multiple message might be coming in, protect the state before we make assumptions on it value.
_adminLock.Lock();

if ((_state & WEBSERVER) == 0) {
if ((_state & WEBSERVICE) == 0) {
_webSocketMessage->ErrorCode = Web::STATUS_INTERNAL_SERVER_ERROR;
_webSocketMessage->Message = _T("State of the link can not be upgraded.");
} else {
Expand All @@ -794,7 +812,7 @@ POP_WARNING()
_parent.StateChange();

if (_webSocketMessage->ErrorCode != Web::STATUS_SWITCH_PROTOCOL) {
_state = (_state & 0xF0) | WEBSERVER;
_state = (_state & 0xF0) | WEBSERVICE;
_path.clear();
_query.clear();
_protocol.Clear();
Expand Down Expand Up @@ -845,7 +863,7 @@ POP_WARNING()

_adminLock.Lock();

if ((_state & WEBSERVER) != 0) {
if ((_state & WEBSERVICE) != 0) {
result = true;
_state = (_state & 0xF0) | UPGRADING;
_origin = (origin.empty() ? ACTUALLINK::LocalId() : origin);
Expand Down Expand Up @@ -1076,6 +1094,10 @@ POP_WARNING()
{
_channel.Ping();
}
void Pong()
{
_channel.Pong();
}
void Trigger()
{
_channel.Trigger();
Expand Down Expand Up @@ -1272,6 +1294,10 @@ POP_WARNING()
{
_channel.Ping();
}
void Pong()
{
_channel.Pong();
}

virtual bool IsIdle() const = 0;
virtual void StateChange() = 0;
Expand Down Expand Up @@ -1430,6 +1456,10 @@ POP_WARNING()
{
_channel.Ping();
}
void Pong()
{
_channel.Pong();
}

virtual bool IsIdle() const = 0;
virtual void StateChange() = 0;
Expand Down

0 comments on commit 5712a36

Please sign in to comment.