ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
lib/network/websocket/client.c
Go to the documentation of this file.
1
12#include <ascii-chat/network/websocket/client.h>
13#include <ascii-chat/common.h>
14#include <ascii-chat/log/logging.h>
15#include <ascii-chat/platform/abstraction.h>
16#include <ascii-chat/network/acip/transport.h>
17
18#include <string.h>
19#include <stdatomic.h>
20
24websocket_client_t *websocket_client_create(void) {
25 websocket_client_t *client = SAFE_MALLOC(sizeof(websocket_client_t), websocket_client_t *);
26 if (!client) {
27 log_error("Failed to allocate websocket_client_t");
28 return NULL;
29 }
30
31 // Zero-initialize all fields
32 memset(client, 0, sizeof(*client));
33
34 // Initialize connection state
35 atomic_store(&client->connection_active, false);
36 atomic_store(&client->connection_lost, false);
37 client->transport = NULL;
38
39 log_debug("WebSocket client created");
40
41 return client;
42}
43
47void websocket_client_destroy(websocket_client_t **client_ptr) {
48 if (!client_ptr || !*client_ptr) {
49 return; // No-op if NULL
50 }
51
52 websocket_client_t *client = *client_ptr;
53
54 log_debug("Destroying WebSocket client");
55
56 // Close and destroy transport if it exists
57 if (client->transport) {
58 acip_transport_destroy(client->transport);
59 client->transport = NULL;
60 }
61
62 SAFE_FREE(*client_ptr);
63}
64
68bool websocket_client_is_active(const websocket_client_t *client) {
69 if (!client) {
70 return false;
71 }
72 return atomic_load(&client->connection_active);
73}
74
78bool websocket_client_is_lost(const websocket_client_t *client) {
79 if (!client) {
80 return false;
81 }
82 return atomic_load(&client->connection_lost);
83}
84
88void websocket_client_signal_lost(websocket_client_t *client) {
89 if (!client) {
90 return;
91 }
92 atomic_store(&client->connection_lost, true);
93 atomic_store(&client->connection_active, false);
94 log_debug("WebSocket connection marked as lost");
95}
96
100void websocket_client_close(websocket_client_t *client) {
101 if (!client) {
102 return;
103 }
104
105 log_debug("Closing WebSocket client");
106
107 if (client->transport) {
108 acip_transport_close(client->transport);
109 }
110
111 atomic_store(&client->connection_active, false);
112}
113
117void websocket_client_shutdown(websocket_client_t *client) {
118 if (!client) {
119 return;
120 }
121
122 log_debug("Shutting down WebSocket client");
123
124 // Force close the transport
125 if (client->transport) {
126 acip_transport_close(client->transport);
127 }
128
129 atomic_store(&client->connection_active, false);
130 atomic_store(&client->connection_lost, true);
131}
132
141acip_transport_t *websocket_client_connect(websocket_client_t *client, const char *url,
142 struct crypto_context_t *crypto_ctx) {
143 if (!client || !url) {
144 log_error("Invalid arguments to websocket_client_connect");
145 return NULL;
146 }
147
148 log_info("Connecting WebSocket client to %s", url);
149
150 // Store URL for reference
151 strncpy(client->url, url, sizeof(client->url) - 1);
152 client->url[sizeof(client->url) - 1] = '\0';
153
154 // Create transport using ACIP layer
155 acip_transport_t *transport = acip_websocket_client_transport_create(url, crypto_ctx);
156 if (!transport) {
157 log_error("Failed to create WebSocket transport");
158 atomic_store(&client->connection_lost, true);
159 return NULL;
160 }
161
162 // Store transport and mark as active
163 client->transport = transport;
164 atomic_store(&client->connection_active, true);
165 atomic_store(&client->connection_lost, false);
166
167 log_info("WebSocket client connected to %s", url);
168
169 return transport;
170}
171
175acip_transport_t *websocket_client_get_transport(const websocket_client_t *client) {
176 if (!client) {
177 return NULL;
178 }
179 return client->transport;
180}
bool websocket_client_is_active(const websocket_client_t *client)
Check if connection is currently active.
void websocket_client_shutdown(websocket_client_t *client)
Shutdown connection forcefully.
acip_transport_t * websocket_client_connect(websocket_client_t *client, const char *url, struct crypto_context_t *crypto_ctx)
Establish WebSocket connection to server.
bool websocket_client_is_lost(const websocket_client_t *client)
Check if connection was lost.
websocket_client_t * websocket_client_create(void)
Create and initialize WebSocket client.
void websocket_client_close(websocket_client_t *client)
Close connection gracefully.
void websocket_client_destroy(websocket_client_t **client_ptr)
Destroy WebSocket client and free resources.
acip_transport_t * websocket_client_get_transport(const websocket_client_t *client)
Get active transport instance.
void websocket_client_signal_lost(websocket_client_t *client)
Signal that connection was lost.
void acip_transport_destroy(acip_transport_t *transport)
acip_transport_t * acip_websocket_client_transport_create(const char *url, crypto_context_t *crypto_ctx)
Create WebSocket client transport.