LibJuno 1.0.4
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
udp_linux.h File Reference

Linux/POSIX-specific derivation and initialiser for the UDP module. More...

#include "udp_api.h"
#include <sys/socket.h>
#include <netinet/in.h>
Include dependency graph for udp_linux.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct JUNO_UDP_LINUX_TAG JUNO_UDP_LINUX_T
 

Functions

struct JUNO_UDP_LINUX_TAG JUNO_MODULE_DERIVE (JUNO_UDP_ROOT_T, int _iSockFd;struct sockaddr_in _tAddr;)
 Linux POSIX derivation of the UDP module.
 
union JUNO_UDP_TAG JUNO_MODULE (JUNO_UDP_API_T, JUNO_UDP_ROOT_T, JUNO_UDP_LINUX_T tLinux;)
 Type-safe polymorphic handle for a UDP module instance.
 
JUNO_STATUS_T JunoUdp_LinuxInit (JUNO_UDP_T *ptUdp, const JUNO_UDP_CFG_T *ptCfg, JUNO_FAILURE_HANDLER_T pfcnFailureHandler, void *pvFailureUserData)
 Initialise a Linux UDP module instance and open the socket (RAII).
 

Detailed Description

Linux/POSIX-specific derivation and initialiser for the UDP module.

This header is the platform layer for the UDP socket module. It extends the freestanding interface (udp_api.h) with Linux/POSIX-specific state (socket file descriptor, peer/bind address) and provides a RAII initialiser that wires the vtable and opens the socket in a single call.

Do not include this header in freestanding translation units. It pulls in <sys/socket.h> and <netinet/in.h> which are POSIX-only.

Typical usage:

#include "udp_linux.h"
static JUNO_UDP_T tUdp = {0};
JUNO_UDP_CFG_T tCfg = { "127.0.0.1", 5000, false };
JUNO_STATUS_T tStatus = JunoUdp_LinuxInit(&tUdp, &tCfg, NULL, NULL);
if (tStatus != JUNO_STATUS_SUCCESS) { // handle error }
UDP_THREAD_MSG_T tMsg = {0};
tUdp.tRoot.ptApi->Send(&tUdp.tRoot, &tMsg);
tUdp.tRoot.ptApi->Free(&tUdp.tRoot); // closes socket
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:59
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:51
Configuration passed to JunoUdp_LinuxInit to open and configure a UDP socket.
Definition udp_api.h:131
Fixed-size UDP datagram message transferred between sender and receiver.
Definition udp_api.h:101
union JUNO_UDP_TAG JUNO_UDP_T
Forward declaration of the UDP module union type.
Definition udp_api.h:81
Linux/POSIX-specific derivation and initialiser for the UDP module.
JUNO_STATUS_T JunoUdp_LinuxInit(JUNO_UDP_T *ptUdp, const JUNO_UDP_CFG_T *ptCfg, JUNO_FAILURE_HANDLER_T pfcnFailureHandler, void *pvFailureUserData)
Initialise a Linux UDP module instance and open the socket (RAII).
Definition linux_udp_impl.cpp:264

Typedef Documentation

◆ JUNO_UDP_LINUX_T

typedef struct JUNO_UDP_LINUX_TAG JUNO_UDP_LINUX_T

Function Documentation

◆ JUNO_MODULE()

union JUNO_UDP_TAG JUNO_MODULE ( JUNO_UDP_API_T  ,
JUNO_UDP_ROOT_T  ,
JUNO_UDP_LINUX_T tLinux;   
)

Type-safe polymorphic handle for a UDP module instance.

Defined here (rather than in udp_api.h) because the union body requires JUNO_UDP_LINUX_T to be complete, and that type includes POSIX fields (struct sockaddr_in) that are not freestanding-compatible.

Callers allocate this union (stack or static) and pass it to JunoUdp_LinuxInit, which wires the vtable and opens the socket. All subsequent API calls use &tUdp.tRoot.

◆ JUNO_MODULE_DERIVE()

struct JUNO_UDP_LINUX_TAG JUNO_MODULE_DERIVE ( JUNO_UDP_ROOT_T  ,
int _iSockFd;struct sockaddr_in _tAddr;   
)

Linux POSIX derivation of the UDP module.

Embeds JUNO_UDP_ROOT_T as its first member (tRoot, via JUNO_MODULE_DERIVE), enabling safe up-cast to the root for vtable dispatch. Owns the POSIX socket state that cannot be expressed in freestanding-compatible types and therefore cannot reside in the root.

Members:

  • _iSockFd — POSIX socket file descriptor; -1 when closed/invalid.
  • _tAddr — Peer address (sender) or bind address (receiver), populated by JunoUdp_LinuxInit.

Callers allocate a JUNO_UDP_T union and pass it to JunoUdp_LinuxInit; they need not interact with this type directly.

◆ JunoUdp_LinuxInit()

JUNO_STATUS_T JunoUdp_LinuxInit ( JUNO_UDP_T ptUdp,
const JUNO_UDP_CFG_T ptCfg,
JUNO_FAILURE_HANDLER_T  pfcnFailureHandler,
void *  pvFailureUserData 
)

Initialise a Linux UDP module instance and open the socket (RAII).

Wires the internal Linux vtable (g_junoUdpLinuxApi) into the module, stores the failure handler, and immediately opens a POSIX UDP socket configured according to ptCfg. If bIsReceiver is true the socket is bound to the local port; otherwise it is connected to the remote address.

Callers do NOT pass ptApi — the vtable is selected internally.

On failure the socket is not opened and the module is left in a safe state with _iSockFd = -1.

Initialisation sequence:

  1. Guard ptUdp and ptCfg (returns JUNO_STATUS_NULLPTR_ERROR if NULL).
  2. Call JunoUdp_Init with the internal Linux vtable.
  3. Create a POSIX UDP socket.
  4. Bind (receiver) or connect (sender) the socket per ptCfg.
  5. Store the socket fd and address in ptUdp->tLinux.
  6. Return JUNO_STATUS_SUCCESS.
Parameters
ptUdpCaller-owned module union storage; must be non-NULL.
ptCfgSocket configuration (address, port, role); must be non-NULL.
pfcnFailureHandlerDiagnostic callback invoked before any error return; may be NULL.
pvFailureUserDataOpaque user data pointer passed to the failure handler; may be NULL.
Returns
JUNO_STATUS_SUCCESS on success; non-zero on failure (e.g., socket creation error).

Wires s_tJunoUdpLinuxApi into the root via JunoUdp_Init, stores the failure handler, initialises _iSockFd to -1, then calls OpenSocket to create and bind/connect the POSIX socket.

On failure the socket is not opened and the module is left in a safe state with _iSockFd = -1.

Parameters
ptUdpCaller-owned module union storage; must be non-NULL.
ptCfgSocket configuration (address, port, role); must be non-NULL.
pfcnFailureHandlerDiagnostic callback invoked before any error return; may be NULL.
pvFailureUserDataOpaque user data pointer passed to the failure handler; may be NULL.
Returns
JUNO_STATUS_SUCCESS on success; non-zero on failure.