General-purpose TCP socket wrapper with optional TLS support via mbedTLS (altcp). More...
#include <Tcp.h>
Public Member Functions | |
Tcp () | |
Tcp (int sockfd) | |
For accepted sockets. | |
~Tcp () | |
Tcp (const Tcp &)=delete | |
Tcp & | operator= (const Tcp &)=delete |
Tcp (Tcp &&other) noexcept | |
Tcp & | operator= (Tcp &&other) noexcept |
std::string | getPeerIp () const |
void | setRootCACertificate (const std::string &pem) |
Set the Root CA certificate to be used for client TLS connections (PEM format). | |
void | setServerTlsConfig (const std::string &certPem, const std::string &keyPem) |
Set the certificate and key to use for server-side TLS (PEM format). | |
bool | connect (const char *host, int port, bool useTls=false) |
Connect to a remote host. | |
int | send (const char *buffer, size_t size) |
Send data over the connection. | |
int | recv (char *buffer, size_t size, uint32_t timeout_ms) |
Receive data from the connection. | |
int | close () |
Close the connection and free resources. | |
bool | bindAndListen (int port) |
Bind and listen on a port for incoming connections (for server use). | |
bool | bindAndListenPlain (int port) |
bool | bindAndListenTls (int port) |
Tcp * | accept () |
Accept a new incoming connection (for server use). | |
bool | isValid () const |
Check if the socket is valid. | |
bool | isConnected () const |
Check if the socket is connected. | |
int | getSocketFd () const |
Get the raw socket file descriptor (may be -1 for TLS-only connection). | |
void | setHostname (const char *name) |
const char * | getHostname () const |
Private Member Functions | |
bool | connectPlain (const ip_addr_t &ip, int port) |
bool | connectTls (const ip_addr_t &ip, int port) |
Static Private Member Functions | |
static err_t | tlsRecvCallback (void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) |
static err_t | acceptCallback (void *arg, struct altcp_pcb *new_conn, err_t err) |
Private Attributes | |
int | sockfd = -1 |
bool | connected = false |
bool | use_tls = false |
bool | is_server_socket = false |
int | connectResult = ERR_OK |
std::string | root_ca_cert |
struct pbuf * | recv_buffer = nullptr |
Buffer for TLS receive. | |
size_t | recv_offset = 0 |
std::string | server_tls_cert |
std::string | server_tls_key |
struct altcp_tls_config * | server_tls_config = nullptr |
TaskHandle_t | connectingTask = nullptr |
Task handle for async operations. | |
TaskHandle_t | waiting_task = nullptr |
Task handle for async operations. | |
altcp_pcb * | pending_client = nullptr |
For TLS: set by acceptCallback. | |
char | hostname [64] = {0} |
Hostname for TLS connections. | |
Static Private Attributes | |
static constexpr size_t | MAX_TLS_SEGMENT_SIZE = 1460 |
Tcp::Tcp | ( | ) |
|
explicit |
Tcp::~Tcp | ( | ) |
|
delete |
|
noexcept |
Tcp * Tcp::accept | ( | ) |
Definition at line 491 of file Tcp.cpp.
References connected, NotifyAccept, pending_client, sockfd, Tcp(), TRACE, use_tls, and waiting_task.
Referenced by HttpServer::run().
|
staticprivate |
Definition at line 471 of file Tcp.cpp.
References NotifyAccept, and pending_client.
bool Tcp::bindAndListen | ( | int | port | ) |
Definition at line 542 of file Tcp.cpp.
References bindAndListenPlain(), bindAndListenTls(), and server_tls_config.
Referenced by HttpServer::initListener().
bool Tcp::bindAndListenPlain | ( | int | port | ) |
Definition at line 552 of file Tcp.cpp.
References connected, sockfd, TRACE, and use_tls.
Referenced by bindAndListen().
bool Tcp::bindAndListenTls | ( | int | port | ) |
int Tcp::close | ( | ) |
Definition at line 443 of file Tcp.cpp.
References connected, recv_buffer, recv_offset, sockfd, use_tls, and waiting_task.
Referenced by HttpServer::handleClient(), HttpServer::startHandlingClient(), and ~Tcp().
bool Tcp::connect | ( | const char * | host, |
int | port, | ||
bool | useTls = false |
||
) |
host | Hostname or IP address. |
port | Port number. |
Definition at line 116 of file Tcp.cpp.
References connectPlain(), connectTls(), hostname, resolveHostnameBlocking(), and use_tls.
|
private |
Definition at line 133 of file Tcp.cpp.
References connected, sockfd, TRACE, and use_tls.
Referenced by connect().
|
private |
|
inline |
std::string Tcp::getPeerIp | ( | ) | const |
|
inline |
Definition at line 124 of file Tcp.h.
References sockfd.
Referenced by getClientIpFromTcp(), HttpResponse::getSocket(), and HttpRequest::receive().
|
inline |
Definition at line 119 of file Tcp.h.
References connected.
Referenced by MultipartParser::sendHttpResponse().
|
inline |
Definition at line 54 of file Tcp.cpp.
References sockfd.
int Tcp::recv | ( | char * | buffer, |
size_t | size, | ||
uint32_t | timeout_ms | ||
) |
Definition at line 389 of file Tcp.cpp.
References NotifyRecv, recv_buffer, recv_offset, sockfd, TRACE, use_tls, and waiting_task.
Referenced by HttpRequest::appendRemainingBody(), MultipartParser::handleMultipart(), HttpParser::receiveChunkedBodyToFile(), HttpParser::receiveChunkedBodyToString(), HttpParser::receiveFixedLengthBodyToFile(), HttpParser::receiveFixedLengthBodyToString(), HttpParser::receiveHeaderAndLeftover(), HttpParser::receiveUnknownLengthBodyToString(), and HttpRequest::receiveUntilHeadersComplete().
int Tcp::send | ( | const char * | buffer, |
size_t | size | ||
) |
Definition at line 279 of file Tcp.cpp.
References connected, HTTP_BUFFER_SIZE, sockfd, STREAM_SEND_DELAY_MS, TRACE, use_tls, and warning().
Referenced by HttpResponse::send(), HttpResponse::sendHeaders(), MultipartParser::sendHttpResponse(), HttpResponse::start(), and HttpResponse::writeChunk().
|
inline |
void Tcp::setRootCACertificate | ( | const std::string & | pem | ) |
void Tcp::setServerTlsConfig | ( | const std::string & | certPem, |
const std::string & | keyPem | ||
) |
|
staticprivate |
Definition at line 351 of file Tcp.cpp.
References NotifyRecv.
|
private |
Definition at line 143 of file Tcp.h.
Referenced by accept(), bindAndListenPlain(), close(), connectPlain(), isConnected(), and send().
|
private |
Definition at line 170 of file Tcp.h.
Referenced by connect(), getHostname(), and setHostname().
|
staticconstexprprivate |
|
private |
Definition at line 168 of file Tcp.h.
Referenced by accept(), and acceptCallback().
|
private |
|
private |
|
private |
Definition at line 161 of file Tcp.h.
Referenced by bindAndListen().
|
private |
Definition at line 142 of file Tcp.h.
Referenced by accept(), bindAndListenPlain(), close(), connectPlain(), getPeerIp(), getSocketFd(), isValid(), operator=(), recv(), and send().
|
private |
Definition at line 144 of file Tcp.h.
Referenced by accept(), bindAndListenPlain(), close(), connect(), connectPlain(), recv(), and send().
|
private |