ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
connection_factory.c File Reference

Shared connection factory for TCP and WebSocket endpoints. More...

Go to the source code of this file.

Functions

asciichat_error_t connection_factory_open (const char *name, const char *input, uint16_t default_port, crypto_context_t *crypto_ctx, acip_transport_t **transport_out, connection_endpoint_t *endpoint_out)
 

Detailed Description

Shared connection factory for TCP and WebSocket endpoints.

Definition in file connection_factory.c.

Function Documentation

◆ connection_factory_open()

asciichat_error_t connection_factory_open ( const char *  name,
const char *  input,
uint16_t  default_port,
crypto_context_t *  crypto_ctx,
acip_transport_t **  transport_out,
connection_endpoint_t *  endpoint_out 
)

Open a transport for a resolved endpoint.

The factory hides the backend-specific setup details:

  • TCP endpoints are resolved and connected with timeout handling
  • WebSocket endpoints use the WebSocket transport constructor
Parameters
nameTransport/debug name
inputOriginal endpoint string
default_portFallback port for bare hostnames
crypto_ctxOptional crypto context to attach to the transport
transport_outReceives the connected transport on success
endpoint_outOptional normalized endpoint output
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 75 of file connection_factory.c.

77 {
78 if (!name || !input || !transport_out) {
79 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid connection factory parameters");
80 }
81
82 *transport_out = NULL;
83
84 connection_endpoint_t endpoint = {0};
85 asciichat_error_t resolve_result = connection_endpoint_resolve(input, default_port, &endpoint);
86 if (resolve_result != ASCIICHAT_OK) {
87 return resolve_result;
88 }
89
90 acip_transport_t *transport = NULL;
91
92 switch (endpoint.protocol) {
94 transport = acip_websocket_client_transport_create(name, endpoint.input, crypto_ctx);
95 if (!transport) {
96 return SET_ERRNO(ERROR_NETWORK_CONNECT, "Failed to create WebSocket transport");
97 }
98 break;
99
101 resolve_result = connection_factory_open_tcp(name, &endpoint, crypto_ctx, &transport);
102 if (resolve_result != ASCIICHAT_OK) {
103 return resolve_result;
104 }
105 break;
106
107 default:
108 return SET_ERRNO(ERROR_INVALID_PARAM, "Unsupported endpoint protocol");
109 }
110
111 if (endpoint_out) {
112 *endpoint_out = endpoint;
113 }
114 *transport_out = transport;
115 return ASCIICHAT_OK;
116}
asciichat_error_t connection_endpoint_resolve(const char *input, uint16_t default_port, connection_endpoint_t *endpoint_out)
@ CONNECTION_ENDPOINT_TCP
@ CONNECTION_ENDPOINT_WEBSOCKET
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_NETWORK_CONNECT
Definition error_codes.h:79
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
Transport instance structure.
Definition transport.h:214
connection_endpoint_protocol_t protocol
acip_transport_t * acip_websocket_client_transport_create(const char *name, const char *url, crypto_context_t *crypto_ctx)
Create WebSocket client transport.

References acip_websocket_client_transport_create(), ASCIICHAT_OK, connection_endpoint_resolve(), CONNECTION_ENDPOINT_TCP, CONNECTION_ENDPOINT_WEBSOCKET, ERROR_INVALID_PARAM, ERROR_NETWORK_CONNECT, connection_endpoint_t::input, connection_endpoint_t::protocol, and SET_ERRNO.

Referenced by server_connection_establish().