1#ifndef _PAINLESS_MESH_CONNECTION_HPP_
2#define _PAINLESS_MESH_CONNECTION_HPP_
23static const uint32_t TCP_CLIENT_CLEANUP_DELAY_MS = 1000;
32static const uint32_t TCP_CLIENT_DELETION_SPACING_MS = 1000;
68 using namespace logger;
72 Log(CONNECTION,
"%s: No scheduler available, deleting AsyncClient immediately (risky)\n", logPrefix);
78 uint32_t currentTime = millis();
82 uint32_t baseDelay = TCP_CLIENT_CLEANUP_DELAY_MS;
86 uint32_t targetDeletionTime = currentTime + baseDelay;
98 int32_t timeUntilSlot = (int32_t)(nextAvailableSlot - targetDeletionTime);
99 if (timeUntilSlot > 0 && timeUntilSlot < (int32_t)(1U << 30)) {
101 targetDeletionTime = nextAvailableSlot;
108 uint32_t actualDelay = targetDeletionTime - currentTime;
116 Log(CONNECTION,
"%s: Scheduling AsyncClient deletion in %u ms (spaced from previous deletions)\n",
117 logPrefix, actualDelay);
120 Task* cleanupTask =
new Task(actualDelay * TASK_MILLISECOND, TASK_ONCE, [client, logPrefix]() {
121 using namespace logger;
122 Log(CONNECTION,
"%s: Deferred cleanup of AsyncClient executing now\n", logPrefix);
136 cleanupTask->setSelfDestruct(
true);
138 scheduler->addTask(*cleanupTask);
139 cleanupTask->enableDelayed();
151 :
public std::enable_shared_from_this<BufferedConnection> {
165 using namespace logger;
203 auto self = this->shared_from_this();
205 if (!self->sentBuffer.empty() && self->client->canSend()) {
206 auto ret = self->writeNext();
208 self->sentBufferTask.forceNextIteration();
210 self->sentBufferTask.delay(100 * TASK_MILLISECOND);
217 if (!self->receiveBuffer.empty()) {
218 TSTRING frnt = self->receiveBuffer.front();
219 self->receiveBuffer.pop_front();
220 if (!self->receiveBuffer.empty())
221 self->readBufferTask.forceNextIteration();
222 if (self->receiveCallback) self->receiveCallback(frnt);
230 self->sentBufferTask.forceNextIteration();
236 self->receiveBuffer.push(
static_cast<const char *
>(data), len,
239 self->client->ack(len);
240 self->readBufferTask.forceNextIteration();
330 if (len > snd_len) len = snd_len;
336 if (written == len) {
342 if (msgPriority <= 1) {
349 }
else if (written == 0) {
362 template <
typename T>
364 return std::static_pointer_cast<T>(shared_from_this());
Definition asynctcp.hpp:31
size_t space()
Definition asynctcp.hpp:129
bool connected()
Definition asynctcp.hpp:111
void onAck(AcAckHandler cb, void *arg=0)
Definition asynctcp.hpp:86
size_t write(const void *data, size_t len, size_t copy=ASYNC_WRITE_FLAG_COPY)
Definition asynctcp.hpp:50
void onData(AcDataHandler cb, void *arg=0)
Definition asynctcp.hpp:96
void onDisconnect(AcConnectHandler cb, void *arg=0)
Definition asynctcp.hpp:81
void send()
Definition asynctcp.hpp:68
void close(bool now=true)
Definition asynctcp.hpp:115
void onError(AcErrorHandler cb, void *arg=0)
Definition asynctcp.hpp:91
ReceivedBuffer handles cstrings and stores them as strings.
Definition buffer.hpp:28
void clear()
Definition buffer.hpp:85
SentBuffer stores messages (strings) and allows them to be read in any length with priority-based sch...
Definition buffer.hpp:137
bool pushWithPriority(const T &message, uint8_t priorityLevel)
Definition buffer.hpp:178
bool push(const T &message, bool priority=false)
Definition buffer.hpp:154
size_t requestLength(size_t buffer_length)
Definition buffer.hpp:206
void clear()
Definition buffer.hpp:301
const char * readPtr(size_t length)
Definition buffer.hpp:250
bool empty()
Definition buffer.hpp:299
void freeRead()
Definition buffer.hpp:265
uint8_t getLastReadPriority() const
Definition buffer.hpp:315
void remote(const char *format...)
Definition logger.hpp:160
Definition connection.hpp:151
void initialize(Scheduler *scheduler)
Definition connection.hpp:199
bool write(const TSTRING &data, bool priority=false)
Definition connection.hpp:277
std::function< void()> disconnectCallback
Definition connection.hpp:319
painlessmesh::buffer::ReceiveBuffer< TSTRING > receiveBuffer
Definition connection.hpp:321
BufferedConnection(AsyncClient *client)
Definition connection.hpp:162
~BufferedConnection()
Definition connection.hpp:164
Task readBufferTask
Definition connection.hpp:360
bool mConnected
Definition connection.hpp:313
AsyncClient * client
Definition connection.hpp:315
Scheduler * mScheduler
Definition connection.hpp:316
bool writeWithPriority(const TSTRING &data, uint8_t priorityLevel)
Definition connection.hpp:295
void close()
Definition connection.hpp:248
painlessmesh::buffer::SentBuffer< TSTRING > sentBuffer
Definition connection.hpp:322
void onReceive(std::function< void(TSTRING)> callback)
Definition connection.hpp:306
bool writeNext()
Definition connection.hpp:324
std::shared_ptr< T > shared_from(T *derived)
Definition connection.hpp:363
std::function< void(TSTRING)> receiveCallback
Definition connection.hpp:318
bool connected()
Definition connection.hpp:310
void onDisconnect(std::function< void()> callback)
Definition connection.hpp:302
Task sentBufferTask
Definition connection.hpp:359
String TSTRING
Definition configuration.hpp:132
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2
uint32_t lastScheduledDeletionTime
Definition connection.cpp:6
painlessmesh::buffer::temp_buffer_t shared_buffer
Definition connection.cpp:7
void scheduleAsyncClientDeletion(Scheduler *scheduler, AsyncClient *client, const char *logPrefix)
Definition connection.hpp:67
size_t length
Definition buffer.hpp:20
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2