Symple module

ServerPeer

Per-connection state for a connected Symple peer.

ServerPeer

#include <icy/symple/server.h>

Per-connection state for a connected Symple peer.

Created by the server after successful authentication. Holds the peer data, room memberships, and a reference to the underlying WebSocket connection for sending.

Public Methods

ReturnNameDescription
ServerPeerConstructs a peer bound to the given server-side connection.
voidsendSerialises and sends a JSON message over the WebSocket connection. Logs a warning if the send fails; does not throw.
voidsendSerializedSends a pre-serialized JSON payload over the WebSocket connection. Use this on fanout paths that already serialized once.
voidjoinAdds this peer to the named room (local tracking only).
voidleaveRemoves this peer from the named room (local tracking only).
voidleaveAllRemoves this peer from all rooms (local tracking only).
Peer &peer inlineReturns a mutable reference to the peer data object.
const Peer &peer const inlineReturns a const reference to the peer data object.
std::stringid constReturns the session ID assigned to this peer.
const std::unordered_set< std::string > &rooms const inlineReturns the set of room names this peer is currently joined to.
boolauthenticated const inlineReturns true if the peer has completed authentication.
voidsetAuthenticated inlineMarks the peer as authenticated or unauthenticated.
voidsetPeer inlineReplaces the peer's data object.
http::ServerConnection &connection inlineReturns a reference to the underlying server connection.
boolcheckRate inlinePer-peer rate limiter. Returns false if message should be dropped.
voidsetRateLimit inlineConfigures the per-peer rate limit.

ServerPeer

ServerPeer(http::ServerConnection & conn)

Constructs a peer bound to the given server-side connection.

Parameters

  • conn The underlying WebSocket server connection.

send

void send(const json::Value & msg)

Serialises and sends a JSON message over the WebSocket connection. Logs a warning if the send fails; does not throw.

Parameters

  • msg JSON value to send.

sendSerialized

void sendSerialized(const char * data, size_t len)

Sends a pre-serialized JSON payload over the WebSocket connection. Use this on fanout paths that already serialized once.


join

void join(const std::string & room)

Adds this peer to the named room (local tracking only).

Parameters

  • room Room name to join.

leave

void leave(const std::string & room)

Removes this peer from the named room (local tracking only).

Parameters

  • room Room name to leave.

leaveAll

void leaveAll()

Removes this peer from all rooms (local tracking only).


peer

inline

inline Peer & peer()

Returns a mutable reference to the peer data object.


peer

const inline

inline const Peer & peer() const

Returns a const reference to the peer data object.


id

const

std::string id() const

Returns the session ID assigned to this peer.


rooms

const inline

inline const std::unordered_set< std::string > & rooms() const

Returns the set of room names this peer is currently joined to.


authenticated

const inline

inline bool authenticated() const

Returns true if the peer has completed authentication.


setAuthenticated

inline

inline void setAuthenticated(bool v)

Marks the peer as authenticated or unauthenticated.

Parameters

  • v True to mark as authenticated.

setPeer

inline

inline void setPeer(const Peer & p)

Replaces the peer's data object.

Parameters

  • p New peer data.

connection

inline

inline http::ServerConnection & connection()

Returns a reference to the underlying server connection.


checkRate

inline

inline bool checkRate()

Per-peer rate limiter. Returns false if message should be dropped.


setRateLimit

inline

inline void setRateLimit(double rate, double seconds)

Configures the per-peer rate limit.

Parameters

  • rate Maximum messages allowed per window.

  • seconds Duration of the rate window in seconds.

Private Attributes

ReturnNameDescription
http::ServerConnection &_conn
Peer_peer
std::unordered_set< std::string >_rooms
RateLimiter_rateLimiter100 messages per 10 seconds default
bool_authenticated

_conn

http::ServerConnection & _conn

_peer

Peer _peer

_rooms

std::unordered_set< std::string > _rooms

_rateLimiter

RateLimiter _rateLimiter {100.0, 10.0}

100 messages per 10 seconds default


_authenticated

bool _authenticated = false