ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
transport.c File Reference

Go to the source code of this file.

Data Structures

struct  tcp_transport_data_t
 TCP transport implementation data. More...
 

Functions

acip_transport_tacip_tcp_transport_create (socket_t sockfd, crypto_context_t *crypto_ctx)
 Create TCP transport from existing socket.
 
void acip_transport_destroy (acip_transport_t *transport)
 Destroy transport and free all resources.
 

Function Documentation

◆ acip_tcp_transport_create()

acip_transport_t * acip_tcp_transport_create ( socket_t  sockfd,
crypto_context_t crypto_ctx 
)

Create TCP transport from existing socket.

Parameters
sockfdConnected socket descriptor
crypto_ctxOptional crypto context (may be NULL)
Returns
Transport instance or NULL on error
Note
Caller retains socket ownership (transport doesn't close on destroy)

Definition at line 208 of file tcp/transport.c.

208 {
209 if (sockfd == INVALID_SOCKET_VALUE) {
210 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket descriptor");
211 return NULL;
212 }
213
214 // Allocate transport structure
216 if (!transport) {
217 SET_ERRNO(ERROR_MEMORY, "Failed to allocate TCP transport");
218 return NULL;
219 }
220
221 // Allocate TCP-specific data
223 if (!tcp_data) {
224 SAFE_FREE(transport);
225 SET_ERRNO(ERROR_MEMORY, "Failed to allocate TCP transport data");
226 return NULL;
227 }
228
229 // Initialize TCP data
230 tcp_data->sockfd = sockfd;
231 tcp_data->is_connected = true;
232
233 // Initialize transport
234 transport->methods = &tcp_methods;
235 transport->crypto_ctx = crypto_ctx;
236 transport->impl_data = tcp_data;
237
238 log_debug("Created TCP transport for socket %d (crypto: %s)", sockfd, crypto_ctx ? "enabled" : "disabled");
239
240 return transport;
241}
#define SAFE_FREE(ptr)
Definition common.h:320
#define SAFE_MALLOC(size, cast)
Definition common.h:208
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ERROR_MEMORY
Definition error_codes.h:53
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
#define INVALID_SOCKET_VALUE
Invalid socket value (POSIX: -1)
Definition socket.h:52
Transport instance structure.
Definition transport.h:169
void * impl_data
Transport-specific state.
Definition transport.h:172
const acip_transport_methods_t * methods
Method table (virtual functions)
Definition transport.h:170
crypto_context_t * crypto_ctx
Optional encryption context.
Definition transport.h:171
TCP transport implementation data.
socket_t sockfd
Socket descriptor (NOT owned - don't close)
bool is_connected
Connection state.

References acip_transport::crypto_ctx, ERROR_INVALID_PARAM, ERROR_MEMORY, acip_transport::impl_data, INVALID_SOCKET_VALUE, tcp_transport_data_t::is_connected, log_debug, acip_transport::methods, SAFE_FREE, SAFE_MALLOC, SET_ERRNO, and tcp_transport_data_t::sockfd.

Referenced by server_connection_establish(), and server_main().

◆ acip_transport_destroy()

void acip_transport_destroy ( acip_transport_t transport)

Destroy transport and free all resources.

Parameters
transportTransport to destroy (may be NULL)
Note
Calls close() first if still connected
Frees the transport structure itself
Safe to pass NULL

Definition at line 247 of file tcp/transport.c.

247 {
248 if (!transport) {
249 return;
250 }
251
252 // Close if still connected
253 if (transport->methods && transport->methods->close && transport->methods->is_connected &&
254 transport->methods->is_connected(transport)) {
255 transport->methods->close(transport);
256 }
257
258 // Call custom destroy implementation if provided
259 if (transport->methods && transport->methods->destroy_impl) {
260 transport->methods->destroy_impl(transport);
261 }
262
263 // Free implementation data
264 if (transport->impl_data) {
265 SAFE_FREE(transport->impl_data);
266 }
267
268 // Free transport structure
269 SAFE_FREE(transport);
270
271 log_debug("Destroyed ACIP transport");
272}
bool(* is_connected)(acip_transport_t *transport)
Check if transport is connected.
Definition transport.h:149
asciichat_error_t(* close)(acip_transport_t *transport)
Close this transport.
Definition transport.h:122
void(* destroy_impl)(acip_transport_t *transport)
Custom destroy implementation (optional)
Definition transport.h:160

References acip_transport_methods_t::close, acip_transport_methods_t::destroy_impl, acip_transport::impl_data, acip_transport_methods_t::is_connected, log_debug, acip_transport::methods, and SAFE_FREE.

Referenced by connection_context_cleanup(), server_connection_close(), server_connection_set_transport(), and server_main().