ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
connection_endpoint.h File Reference

Shared endpoint normalization for TCP and WebSocket connections. More...

Go to the source code of this file.

Data Structures

struct  connection_endpoint_t
 

Enumerations

enum  connection_endpoint_protocol_t { CONNECTION_ENDPOINT_TCP = 0 , CONNECTION_ENDPOINT_WEBSOCKET = 1 }
 

Functions

asciichat_error_t connection_endpoint_resolve (const char *input, uint16_t default_port, connection_endpoint_t *endpoint_out)
 
const char * connection_endpoint_protocol_name (connection_endpoint_protocol_t protocol)
 

Detailed Description

Shared endpoint normalization for TCP and WebSocket connections.

This helper removes repeated URL/scheme parsing across client and discovery connection setup code. Callers get one normalized endpoint description and can then choose the appropriate transport path.

Definition in file connection_endpoint.h.

Enumeration Type Documentation

◆ connection_endpoint_protocol_t

Enumerator
CONNECTION_ENDPOINT_TCP 
CONNECTION_ENDPOINT_WEBSOCKET 

Definition at line 16 of file connection_endpoint.h.

Function Documentation

◆ connection_endpoint_protocol_name()

const char * connection_endpoint_protocol_name ( connection_endpoint_protocol_t  protocol)

Definition at line 12 of file connection_endpoint.c.

12 {
13 switch (protocol) {
15 return "TCP";
17 return "WebSocket";
18 default:
19 return "Unknown";
20 }
21}

References CONNECTION_ENDPOINT_TCP, and CONNECTION_ENDPOINT_WEBSOCKET.

Referenced by connection_attempt_tcp().

◆ connection_endpoint_resolve()

asciichat_error_t connection_endpoint_resolve ( const char *  input,
uint16_t  default_port,
connection_endpoint_t *  endpoint_out 
)

Resolve an input string into a normalized TCP or WebSocket endpoint.

Supported inputs:

  • ws://host[:port]
  • wss://host[:port]
  • tcp://host[:port]
  • bare hostnames or host:port pairs

Bare hostnames are treated as TCP endpoints and use default_port when the input does not specify a port.

Definition at line 39 of file connection_endpoint.c.

40 {
41 if (!input || !*input || !endpoint_out) {
42 return SET_ERRNO(ERROR_INVALID_PARAM, "input or endpoint_out is NULL");
43 }
44
45 url_parts_t parts = {0};
46 asciichat_error_t result;
47
48 if (url_is_websocket(input)) {
49 result = url_parse(input, &parts);
50 if (result != ASCIICHAT_OK) {
51 return result;
52 }
53 result = connection_endpoint_from_parts(endpoint_out, &parts, CONNECTION_ENDPOINT_WEBSOCKET, default_port, input);
54 url_parts_destroy(&parts);
55 return result;
56 }
57
58 if (url_is_tcp(input)) {
59 result = url_parse(input, &parts);
60 if (result != ASCIICHAT_OK) {
61 return result;
62 }
63 result = connection_endpoint_from_parts(endpoint_out, &parts, CONNECTION_ENDPOINT_TCP, default_port, input);
64 url_parts_destroy(&parts);
65 return result;
66 }
67
68 /* Unsupported explicit scheme. Bare hostnames are allowed and have no "://". */
69 if (strstr(input, "://") != NULL) {
70 return SET_ERRNO(ERROR_INVALID_PARAM, "Unsupported connection scheme: %s", input);
71 }
72
73 result = url_parse(input, &parts);
74 if (result != ASCIICHAT_OK) {
75 return result;
76 }
77
78 result = connection_endpoint_from_parts(endpoint_out, &parts, CONNECTION_ENDPOINT_TCP, default_port, input);
79 url_parts_destroy(&parts);
80 return result;
81}
#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
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
void url_parts_destroy(url_parts_t *parts)
Free parsed URL components.
Definition url.c:281
bool url_is_websocket(const char *url)
Check if a URL string is a valid WebSocket URL.
Definition url.c:307
asciichat_error_t url_parse(const char *url, url_parts_t *parts_out)
Parse HTTP(S), WebSocket, and TCP URL into components.
Definition url.c:166
bool url_is_tcp(const char *url)
Check if a URL string is a valid TCP URL.
Definition url.c:342
Structure containing parsed URL components.
Definition url.h:57

References ASCIICHAT_OK, CONNECTION_ENDPOINT_TCP, CONNECTION_ENDPOINT_WEBSOCKET, ERROR_INVALID_PARAM, SET_ERRNO, url_is_tcp(), url_is_websocket(), url_parse(), and url_parts_destroy().

Referenced by connection_attempt_tcp(), connection_factory_open(), and server_connection_establish().