updated uuid libs

This commit is contained in:
Proddy
2023-01-05 15:05:30 +01:00
parent 6370296c53
commit 3b196fc90d
27 changed files with 2013 additions and 1411 deletions

View File

@@ -67,7 +67,7 @@ TelnetService::TelnetService(std::shared_ptr<uuid::console::Commands> commands,
TelnetService::TelnetService(uint16_t port, std::shared_ptr<uuid::console::Commands> commands, unsigned int context, unsigned int flags)
: TelnetService(port,
[commands, context, flags](Stream & stream, const IPAddress & addr __attribute__((unused)), uint16_t port __attribute__((unused)))
-> std::shared_ptr<uuid::console::Shell> { return std::make_shared<uuid::console::StreamConsole>(commands, stream, context, flags); }) {
-> std::shared_ptr<uuid::console::Shell> { return std::make_shared<uuid::console::Shell>(stream, commands, context, flags); }) {
}
TelnetService::TelnetService(shell_factory_function shell_factory)
@@ -101,14 +101,13 @@ size_t TelnetService::maximum_connections() const {
void TelnetService::maximum_connections(size_t count) {
maximum_connections_ = std::max((size_t)1, count);
while (connections_.size() > maximum_connections_) {
if (connections_.size() > maximum_connections_) {
size_t stop = connections_.size() - maximum_connections_;
for (auto it = connections_.begin(); it != connections_.end();) {
if (it->active()) {
it->stop();
it = connections_.erase(it);
break;
} else {
it = connections_.erase(it);
if (it->stop()) {
if (--stop == 0)
break;
}
}
}
@@ -143,21 +142,21 @@ void TelnetService::loop() {
if (client) {
if (connections_.size() >= maximum_connections_) {
#if UUID_TELNET_HAVE_WIFICLIENT_REMOTE
logger_.info(F("New connection from [%s]:%u rejected (connection limit reached)"),
logger_.info("New connection from [%s]:%u rejected (connection limit reached)",
uuid::printable_to_string(client.remoteIP()).c_str(),
client.remotePort());
#else
logger_.info(F("New connection rejected (connection limit reached)"));
logger_.info("New connection rejected (connection limit reached)");
#endif
client.println(F("Maximum connection limit reached"));
client.println("Maximum connection limit reached");
client.stop();
} else {
#if UUID_TELNET_HAVE_WIFICLIENT_REMOTE
logger_.info(F("New connection from [%s]:%u accepted"), uuid::printable_to_string(client.remoteIP()).c_str(), client.remotePort());
logger_.info("New connection from [%s]:%u accepted", uuid::printable_to_string(client.remoteIP()).c_str(), client.remotePort());
#endif
connections_.emplace_back(shell_factory_, std::move(client), initial_idle_timeout_, write_timeout_);
#if !(UUID_TELNET_HAVE_WIFICLIENT_REMOTE)
logger_.info(F("New connection %p accepted"), &connections_.back());
logger_.info("New connection %p accepted", &connections_.back());
#endif
}
}
@@ -194,34 +193,37 @@ TelnetService::Connection::Connection(shell_factory_function & shell_factory, Wi
shell->idle_timeout(idle_timeout);
shell->start();
shell_ = shell;
} else {
shell_ = nullptr;
}
}
bool TelnetService::Connection::active() {
return shell_.use_count() > 1;
}
bool TelnetService::Connection::loop() {
if (active()) {
if (!shell_.expired()) {
if (!client_.connected()) {
shell_->stop();
auto shell = shell_.lock();
if (shell) {
shell->stop();
}
}
return true;
} else {
#if UUID_TELNET_HAVE_WIFICLIENT_REMOTE
logger_.info(F("Connection from [%s]:%u closed"), uuid::printable_to_string(addr_).c_str(), port_);
logger_.info("Connection from [%s]:%u closed", uuid::printable_to_string(addr_).c_str(), port_);
#else
logger_.info(F("Connection %p closed"), this);
logger_.info("Connection %p closed", this);
#endif
return false;
}
}
void TelnetService::Connection::stop() {
if (shell_) {
shell_->stop();
bool TelnetService::Connection::stop() {
auto shell = shell_.lock();
if (shell) {
shell->stop();
return true;
} else {
return false;
}
}

View File

@@ -392,36 +392,30 @@ class TelnetService {
Connection(shell_factory_function & shell_factory, WiFiClient && client, unsigned long idle_timeout, unsigned long write_timeout);
~Connection() = default;
/**
* Check if the shell is still active.
*
* @return Active status of the shell.
* @since 0.1.0
*/
bool active();
/**
* Stop the shell if the client is not connected.
*
* @return Active status of the shell.
* @return True if the shell had not already stopped.
* @since 0.1.0
*/
bool loop();
/**
* Stop the shell.
*
* @since 0.1.0
* @since 0.2.0
*/
void stop();
bool stop();
private:
Connection(const Connection &) = delete;
Connection & operator=(const Connection &) = delete;
WiFiClient client_; /*!< Client connection. @since 0.1.0 */
TelnetStream stream_; /*!< Telnet stream for the connection. @since 0.1.0 */
std::shared_ptr<uuid::console::Shell> shell_; /*!< Shell for connection. @since 0.1.0 */
IPAddress addr_; /*!< Remote address of connection. @since 0.1.0 */
uint16_t port_; /*!< Remote port of connection. @since 0.1.0 */
WiFiClient client_; /*!< Client connection. @since 0.1.0 */
TelnetStream stream_; /*!< Telnet stream for the connection. @since 0.1.0 */
std::weak_ptr<uuid::console::Shell> shell_; /*!< Shell for connection. @since 0.2.0 */
IPAddress addr_; /*!< Remote address of connection. @since 0.1.0 */
uint16_t port_; /*!< Remote port of connection. @since 0.1.0 */
};
TelnetService(const TelnetService &) = delete;