ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3#ifndef PROTOCORE_CLIENT_H
4#define PROTOCORE_CLIENT_H
5
6/**
7 * @file client.h
8 * @brief Layer 4 outbound TCP client transport - the client-side peer of the
9 * (server) transport in tcp.cpp.
10 *
11 * A small fixed pool of outbound connections so the application's clients
12 * (services/net/http_client, services/iot/mqtt, services/net/ws_client) no longer each own a
13 * private raw-lwIP TCP stack at L7. As with the server transport, every raw
14 * `tcp_*()` call is marshaled into `tcpip_thread` via `tcpip_api_call()`, so the
15 * main-loop/worker task never races the stack. All storage is static (no heap).
16 *
17 * The receive ring carries **wire bytes**: for a plaintext connection those are
18 * the application bytes; for a TLS connection they are ciphertext and the caller
19 * layers the shared client-TLS session (`pc_tls_client_session_*`) on top, pointing its
20 * BIO at pc_client_send() / pc_client_read().
21 *
22 * The core is non-blocking (read/available/send), so it suits both a polling
23 * client loop (MQTT, WebSocket) and a blocking request (HTTP) that drives its own
24 * deadline. Only pc_client_open() blocks, on DNS + connect.
25 */
26
27#include "protocore_config.h"
28#include <stddef.h>
29#include <stdint.h>
30
31/**
32 * @brief Resolve @p host (dotted-quad fast path, else DNS) and connect to
33 * @p host : @p port, blocking up to @p timeout_ms.
34 * @return A client id in [0, PC_CLIENT_CONNS) on success, or < 0 on failure
35 * (pool full, DNS failure, connect timeout/refused).
36 */
37int pc_client_open(const char *host, uint16_t port, uint32_t timeout_ms);
38
39/** @brief True once the TCP handshake has completed for @p cid. */
40bool pc_client_connected(int cid);
41
42/** @brief True once the peer closed (FIN) or the connection errored. */
43bool pc_client_is_closed(int cid);
44
45/** @brief Queue @p len wire bytes for transmission (marshaled tcp_write + output). */
46bool pc_client_send(int cid, const void *data, size_t len);
47
48/** @brief Wire bytes currently buffered and ready to read. */
49size_t pc_client_available(int cid);
50
51/** @brief Drain up to @p cap buffered wire bytes into @p buf; returns the count. */
52size_t pc_client_read(int cid, uint8_t *buf, size_t cap);
53
54/** @brief Tear down the connection (marshaled) and return the slot to the pool. */
55void pc_client_close(int cid);
56
57#endif // PROTOCORE_CLIENT_H
bool pc_client_is_closed(int cid)
True once the peer closed (FIN) or the connection errored.
Definition client.cpp:362
size_t pc_client_read(int cid, uint8_t *buf, size_t cap)
Drain up to cap buffered wire bytes into buf; returns the count.
Definition client.cpp:374
int pc_client_open(const char *host, uint16_t port, uint32_t timeout_ms)
Resolve host (dotted-quad fast path, else DNS) and connect to host : port, blocking up to timeout_ms.
Definition client.cpp:354
void pc_client_close(int cid)
Tear down the connection (marshaled) and return the slot to the pool.
Definition client.cpp:378
bool pc_client_send(int cid, const void *data, size_t len)
Queue len wire bytes for transmission (marshaled tcp_write + output).
Definition client.cpp:366
bool pc_client_connected(int cid)
True once the TCP handshake has completed for cid.
Definition client.cpp:358
size_t pc_client_available(int cid)
Wire bytes currently buffered and ready to read.
Definition client.cpp:370
User-facing configuration for ProtoCore.