ImFusion SDK 4.3
ConnectionFactory< Payload > Class Template Reference

#include <ImFusion/Stream/Network/ConnectionFactory.h>

The ConnectionFactory manages available connection protocols and creates new connections based on the provided configuration. More...

Detailed Description

template<typename Payload>
class ImFusion::Network::ConnectionFactory< Payload >

The ConnectionFactory manages available connection protocols and creates new connections based on the provided configuration.

Blueprints serve as predefined templates of type ConnectionBlueprint, describing how each connection should be established. The supported payloads are std::string and ImFusion's ByteBuffer class.

This factory enables dynamic support for different protocols (e.g., WebSocket, TCP, UDP) and modes (client or server) at runtime. If the protocol or mode is fix and known at compile time, the corresponding classes should be used directly, such as UDPSocket for UDP communication or WebsocketClient for connecting to a remote WebSocket server.

To register custom connection types in the factory, they should be added when loading a custom plugin, e.g. within the plugin constructor, or during application startup for custom applications.

Example:

// CustomPlugin.h - header implementing the ImFusionPlugin for a custom plugin
#include "MyCustomServer.h" // Include a custom connection type.
// This connection inherits from Network::TypedConnectionBase<std::string>
#include <ImFusion/Base/ImFusionPlugin.h>
#include <ImFusion/Stream/Network/ConnectionFactory.h>
CustomPlugin()
{
auto blueprint = ConnectionBlueprint<std::string>{"MyCustomServer", // Identifier for this connection
[]() { return Properties{{"ip", "127.0.0.1"}, {"port", 1234}}; }, // Default settings
[](const Properties& p) { // Connection creation function
auto hostIp = p.param<std::string>("ip");
auto hostPort = p.param<int>("port");
if (hostIp && hostPort)
{
auto server = std::make_unique<MyCustomServer>(hostIp.value(), hostPort.value());
server->run();
return server;
}
else
{
return std::unique_ptr<MyCustomServer>(nullptr);
}
}};
// ... (other plugin-relevant code)
}
static ConnectionFactory & instance()
Returns the singleton instance of the factory, ensuring global consistency throughout the program's e...
bool registerBlueprint(ConnectionBlueprint< Payload > &&blueprint)
Registers a new connection protocol.
Storage container for serialization of arbitrary types, internally backed by strings.
Definition Properties.h:50
T make_unique(T... args)
Defines the necessary information for registering a new connection type.
Definition ConnectionFactory.h:28

Public Member Functions

std::vector< ConnectionProtocolsupportedConnections () const
 Returns a list of the supported connection protocols.
 
bool registerBlueprint (ConnectionBlueprint< Payload > &&blueprint)
 Registers a new connection protocol.
 
std::optional< PropertiesconnectionProperties (const ConnectionProtocol &connectionProtocol) const
 Returns the properties of a connection protocol, including default values.
 
std::unique_ptr< Network::TypedConnectionBase< Payload > > createConnection (const ConnectionProtocol &connectionProtocol, const Properties &p) const
 Creates and returns a connection instance (for example, server or client) using the specified protocol and properties.
 

Static Public Member Functions

static ConnectionFactoryinstance ()
 Returns the singleton instance of the factory, ensuring global consistency throughout the program's execution (Singleton Pattern).
 

Member Function Documentation

◆ registerBlueprint()

template<typename Payload>
bool registerBlueprint ( ConnectionBlueprint< Payload > && blueprint)

Registers a new connection protocol.

If the same protocol is registered multiple times, the latest registration replaces the previous one.

Parameters
blueprintThe ConnectionBlueprint to register.
Returns
True if registration succeeds, false otherwise.

◆ connectionProperties()

template<typename Payload>
std::optional< Properties > connectionProperties ( const ConnectionProtocol & connectionProtocol) const

Returns the properties of a connection protocol, including default values.

Can also be used to verify whether a specific protocol is supported.

Returns
Properties associated with the connection protocol if found, otherwise an empty optional.

◆ createConnection()

template<typename Payload>
std::unique_ptr< Network::TypedConnectionBase< Payload > > createConnection ( const ConnectionProtocol & connectionProtocol,
const Properties & p ) const

Creates and returns a connection instance (for example, server or client) using the specified protocol and properties.

It typically establishes a connection (client) or starts to listen for incoming connections (server).

Returns
A valid connection object if successful, otherwise nullptr.

Example usage:

if (runInServerMode)
connection = ConnectionFactory<std::string>::instance().createConnection("Websocket Server", Properties{{"ip", "192.168.1.21"}, {"port", 5566}});
else
connection = ConnectionFactory<std::string>::instance().createConnection("Websocket Client", Properties{{"ip", "192.168.1.21"}, {"port", 5566}});
connection->signalNewMessage.connect([this](const std::shared_ptr<std::string>& message) { /* react to incoming messages */ });
connection->sendMessage("[START] : Hello World!");
// ... (continue communication)
std::unique_ptr< Network::TypedConnectionBase< Payload > > createConnection(const ConnectionProtocol &connectionProtocol, const Properties &p) const
Creates and returns a connection instance (for example, server or client) using the specified protoco...

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close