morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::net::SocketServer Class Reference

Raw-socket WebSocket server front for morph::backend::RemoteServer. More...

#include <socket_server.hpp>

Public Types

using Config = SocketServerConfig
 Alias for the config struct.

Public Member Functions

 SocketServer (::morph::exec::IoLoop &loop, ::morph::backend::RemoteServer &server, std::uint16_t port, Config cfg={})
 Constructs the server on loop; does not start listening.
 SocketServer (::morph::backend::RemoteServer &server, std::uint16_t port, Config cfg={})
 Constructs the server on a loop of its own, created by the first listen(); does not start listening.
 SocketServer (const SocketServer &)=delete
SocketServer & operator= (const SocketServer &)=delete
 SocketServer (SocketServer &&)=delete
SocketServer & operator= (SocketServer &&)=delete
 ~SocketServer ()
 Stops accepting and closes every client connection.
bool listen ()
 Starts listening for incoming WebSocket connections.
std::uint16_t port () const
 Returns the port the server is currently bound to.
void close ()
 Stops accepting new connections and closes every client connection.

Detailed Description

Raw-socket WebSocket server front for morph::backend::RemoteServer.

Mirrors morph::qt::QtWebSocketServer's behavior without Qt: for each accepted connection it performs the RFC 6455 handshake, then reads framed text messages and forwards each to RemoteServer::handle(); the reply is written back to the originating socket, or dropped if the connection is gone by the time the reply is ready.

Threading
Owns no thread. The listener, every accepted connection and every reply write live on an exec::IoLoop — the application's one loop, shared with every other socket and timer built on it. RemoteServer::handle() replies arrive on the server's worker pool and are posted to the loop, which queues them on the connection's writer.

Cross-thread surface: listen() and close(), which run on the loop and wait for it (inline when already there), and port(), an atomic read.

Lifetime
Holds RemoteServer& _server by reference, exactly like QtWebSocketServer — the server's owning shared_ptr must outlive this object (see docs/spec/core/backend.md's Lifetime & ownership section).

Definition at line 106 of file socket_server.hpp.

Member Typedef Documentation

◆ Config

Alias for the config struct.

Definition at line 109 of file socket_server.hpp.

Constructor & Destructor Documentation

◆ SocketServer() [1/2]

morph::net::SocketServer::SocketServer ( ::morph::exec::IoLoop & loop,
::morph::backend::RemoteServer & server,
std::uint16_t port,
Config cfg = {} )
inline

Constructs the server on loop; does not start listening.

Parameters
loopThe application's I/O loop. Borrowed: it must outlive this server.
serverRemoteServer instance that processes incoming messages. Borrowed, not owned: it must outlive this server (see docs/spec/concurrency_and_lifetimes.md, "Destruction ordering").
portTCP port to listen on. Pass 0 to let the OS pick a free port.
cfgBacklog and timeout tuning. Default: 64-connection backlog, 10s handshake timeout, 30s send timeout.

Definition at line 121 of file socket_server.hpp.

◆ SocketServer() [2/2]

morph::net::SocketServer::SocketServer ( ::morph::backend::RemoteServer & server,
std::uint16_t port,
Config cfg = {} )
inline

Constructs the server on a loop of its own, created by the first listen(); does not start listening.

Parameters
serverRemoteServer instance that processes incoming messages. Borrowed, not owned: it must outlive this server.
portTCP port to listen on. Pass 0 to let the OS pick a free port.
cfgBacklog and timeout tuning.

Definition at line 131 of file socket_server.hpp.

◆ ~SocketServer()

morph::net::SocketServer::~SocketServer ( )
inline

Stops accepting and closes every client connection.

Closes on the loop and waits for it (inline when already there), then cuts the loop's tie to RemoteServer, so a connection's flow that resumes later touches neither this object nor the server.

Definition at line 145 of file socket_server.hpp.

Member Function Documentation

◆ listen()

bool morph::net::SocketServer::listen ( )
inline

Starts listening for incoming WebSocket connections.

Binds 127.0.0.1:port and starts the accept flow, on the loop. With no loop of its own yet, creates one first; a process out of descriptors cannot, and gets false.

Returns
true if the server bound the requested port and is accepting.

Definition at line 160 of file socket_server.hpp.

◆ port()

std::uint16_t morph::net::SocketServer::port ( ) const
inlinenodiscard

Returns the port the server is currently bound to.

Returns
Bound TCP port (OS-assigned when constructed with port 0), or 0 when not listening.

Definition at line 178 of file socket_server.hpp.

◆ close()

void morph::net::SocketServer::close ( )
inline

Stops accepting new connections and closes every client connection.

Releases the listening socket, so port() reads 0 afterwards and the bound port is free to rebind — the same post-close observation QtWebSocketServer makes. Reclaims every connection's models (RemoteServer::closeConnection) before it returns.

Runs on the loop and waits for it, inline when already there. Idempotent, and safe to call from several threads at once on a live object: each call is one loop task, and the loop runs them one at a time. Racing a call against the destructor is still the caller's problem (see docs/spec/concurrency_and_lifetimes.md, "Destruction ordering").

Definition at line 193 of file socket_server.hpp.


The documentation for this class was generated from the following file: