/
singlwolf
/
Radiola-2S3
Обзор
Документация
Войти
/
singlwolf
/
Radiola-2S3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/AsyncClient_SSL/src/AsyncSSLClient.h
231 строка
7 KB
SinglWolf
v1.0.6
15 апр 2026, 05:04
15 апр 2026, 05:04
55b96c5
Код
Авторство
О чём код?
#ifndef ASYNC_SSL_CLIENT_H #define ASYNC_SSL_CLIENT_H #if !defined(ESP32) #error This AsyncTCP_SSL library is supporting only ESP32 #endif ///////////////////////////////////////////////// #define ASYNC_SSL_CLIENT_VERSION "AsyncSSLClient v0.0.1" #define ASYNC_SSL_CLIENT_VERSION_MAJOR 0 #define ASYNC_SSL_CLIENT_VERSION_MINOR 0 #define ASYNC_SSL_CLIENT_VERSION_PATCH 1 #define ASYNC_SSL_CLIENT_VERSION_INT 0000001 ///////////////////////////////////////////////// #include "IPAddress.h" #include <functional> #include <string> extern "C" { // #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "lwip/pbuf.h" } /***************************************************** // Defined in tools/sdk/esp32s3/include/lwip/lwip/src/include/lwip/tcpbase.h enum tcp_state { CLOSED = 0, LISTEN = 1, SYN_SENT = 2, SYN_RCVD = 3, ESTABLISHED = 4, FIN_WAIT_1 = 5, FIN_WAIT_2 = 6, CLOSE_WAIT = 7, CLOSING = 8, LAST_ACK = 9, TIME_WAIT = 10 }; *****************************************************/ // #define INVALID_CLOSED_SLOT (-1) ////////////////////////////////////////////////////////////////////////////////////////// #define CONFIG_ASYNC_SSL_CLIENT_STACK (2 * 8192) // #define CONFIG_ACLIENT_DBG 1 #ifndef CONFIG_ACLIENT_DBG #define CONFIG_ACLIENT_DBG (0) #endif #ifndef CONFIG_ASYNC_SSL_CLIENT_PRIORITY #define CONFIG_ASYNC_SSL_CLIENT_PRIORITY (6) #endif #ifndef ASYNC_SSL_QUEUE_LENGTH #define ASYNC_SSL_QUEUE_LENGTH (128) #endif #ifndef CONFIG_ASYNC_SSL_CLIENT_RUNNING_CORE #define CONFIG_ASYNC_SSL_CLIENT_RUNNING_CORE (0) #endif #define CONFIG_ASYNC_SSL_CLIENT_USE_WDT 1 ///////////////////////////////////////////////// #define ASYNC_SSL_CLIENT_MAX_ACK_TIME 5000 #define ASYNC_SSL_CLIENT_MAX_RX_TIME 5 #define ASYNC_SSL_CLIENT_WRITE_FLAG_COPY 0x01 // will allocate new buffer to hold the data while sending (else will hold reference to the data given) #define ASYNC_SSL_CLIENT_WRITE_FLAG_MORE 0x02 // will not send PSH flag, meaning that there should be more data to be sent before the application should react. #define ASYNC_SSL_CLIENT_HANDSHAKE_TIMEOUT 5000 // timeout to complete SSL handshake ////////////////////////////////////////////////////////////////////////////////////////////// class AsyncSSLClient; using AcConnectHandler_ = std::function<void(void *, AsyncSSLClient *)>; using AcAckHandler_ = std::function<void(void *, AsyncSSLClient *, size_t len, uint32_t time)>; using AcErrorHandler_ = std::function<void(void *, AsyncSSLClient *, int8_t error)>; using AcDataHandler_ = std::function<void(void *, AsyncSSLClient *, void *data, size_t len)>; // using AcPacketHandler_ = std::function<void(void *, AsyncSSLClient *, struct pbuf *pb)>; // using AcTimeoutHandler_ = std::function<void(void *, AsyncSSLClient *, uint32_t time)>; ///////////////////////////////////////////////// struct tcp_pcb; struct ip_addr; class AsyncSSLTCP_detail; ////////////////////////////////////////////////////////////////////////////////////////////// class AsyncSSLClient { public: AsyncSSLClient(tcp_pcb *pcb = nullptr); virtual ~AsyncSSLClient(); AsyncSSLClient(const AsyncSSLClient &) = delete; AsyncSSLClient &operator=(const AsyncSSLClient &) = delete; bool connect(const IPAddress &ip, uint16_t port, bool secure = false); bool connect(const char *host, uint16_t port, bool secure = false); void close(bool now = false); void stop(); int8_t abort(); bool free(); bool canSend(); // ack is not pending size_t add(const char *data, size_t size, uint8_t apiflags = ASYNC_SSL_CLIENT_WRITE_FLAG_COPY); // add for sending bool send(); // send all data added with the method above virtual size_t space(); // space available in the TCP window // write equals add()+send() virtual size_t write(const char *data); virtual size_t write(const char *data, size_t size, uint8_t apiflags = ASYNC_SSL_CLIENT_WRITE_FLAG_COPY); // only when canSend() == true uint8_t state(); bool connecting(); bool connected(); bool disconnecting(); bool disconnected(); // bool freeable(); // disconnected or disconnecting uint16_t getMss(); uint32_t getRxTimeout(); void setRxTimeout(uint32_t timeout); // no RX data timeout for the connection in seconds uint32_t getAckTimeout(); void setAckTimeout(uint32_t timeout); // no ACK timeout for the last sent packet in milliseconds void setNoDelay(bool nodelay); bool getNoDelay(); uint32_t getRemoteAddress(); uint16_t getRemotePort(); uint32_t getLocalAddress(); uint16_t getLocalPort(); // compatibility IPAddress remoteIP(); uint16_t remotePort(); IPAddress localIP(); uint16_t localPort(); void onConnect(AcConnectHandler_ cb, void *arg = nullptr); // on successful connect void onDisconnect(AcConnectHandler_ cb, void *arg = nullptr); // disconnected void onAck(AcAckHandler_ cb, void *arg = nullptr); // ack received void onError(AcErrorHandler_ cb, void *arg = nullptr); // unsuccessful connect or error void onData(AcDataHandler_ cb, void *arg = nullptr); // data received (called if onPacket is not used) // void onPacket(AcPacketHandler_ cb, void *arg = nullptr); // data received // void onTimeout(AcTimeoutHandler_ cb, void *arg = nullptr); // ack timeout void onPoll(AcConnectHandler_ cb, void *arg = nullptr); // every 125ms when connected // void ackPacket(struct pbuf *pb); // ack pbuf from onPacket size_t ack(size_t len); // ack data that you have not acked using the method below void ackLater() { _ack_pcb = false; // will not ack the current packet. Call from onData } const char *errorToString(int8_t error); const char *stateToString(); tcp_pcb *pcb() { return _pcb; } protected: friend class AsyncSSLTCP_detail; tcp_pcb *_pcb; std::string _hostname; AcConnectHandler_ _on_connect_cb; void *_on_connect_cb_arg; AcConnectHandler_ _on_disconn_cb; void *_on_disconn_cb_arg; AcAckHandler_ _on_ask_cb; void *_on_ask_cb_arg; AcErrorHandler_ _on_error_cb; void *_on_error_cb_arg; AcDataHandler_ _on_data_cb; void *_on_data_cb_arg; // AcPacketHandler_ _on_packet_cb; // void *_on_packet_cb_arg; // AcTimeoutHandler_ _on_timeout_cb; // void *_on_timeout_cb_arg; AcConnectHandler_ _on_poll_cb; void *_on_poll_cb_arg; bool _pcb_busy; uint32_t _pcb_sent_at; bool _ack_pcb; uint32_t _rx_ack_len; uint32_t _rx_last_packet; uint32_t _rx_since_timeout; uint32_t _ack_timeout; uint16_t _connect_port; ////// SSL bool _pcb_secure; bool _handshake_done; ////// int8_t _close(); int8_t _connected(void *pcb, int8_t err); int8_t _recv(tcp_pcb *pcb, pbuf *pb, int8_t err); int8_t _recv_ssl(tcp_pcb *pcb, pbuf *pb, int8_t err); void _error(int8_t err); int8_t _poll(tcp_pcb *pcb); int8_t _sent(tcp_pcb *pcb, uint16_t len); int8_t _fin(tcp_pcb *pcb, int8_t err); int8_t _lwip_fin(tcp_pcb *pcb, int8_t err); void _dns_found(struct ip_addr *ipaddr); }; extern AsyncSSLClient asyncClient; ////////////////////////////////////////////////////////////////////////////////////////////// #endif /* ASYNC_SSL_CLIENT_H */