Skip to content

Commit

Permalink
Use version 3.x.x of uuid-console
Browse files Browse the repository at this point in the history
  • Loading branch information
nomis committed Dec 4, 2022
1 parent 4c08d31 commit 3ba72c7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
11 changes: 11 additions & 0 deletions docs/changelog.rst
Expand Up @@ -4,6 +4,17 @@ Change log
Unreleased_
-----------

Changed
~~~~~~~

* Use version 3.x.x of ``uuid-console``.

Fixed
~~~~~

* Potential for ``TelnetStream`` to be destroyed before the ``Shell``
using it stops when lowering the maximum number of connections.

0.1.5_ |--| 2022-11-26
----------------------

Expand Down
2 changes: 1 addition & 1 deletion library.json
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"nomis/uuid-common": "^1.2.0",
"nomis/uuid-log": ">=2.3.0,<4",
"nomis/uuid-console": ">=1.0.1,<3"
"nomis/uuid-console": "^3.0.0"
},
"build": {
"flags": "-Wall -Wextra",
Expand Down
40 changes: 21 additions & 19 deletions src/telnet.cpp
Expand Up @@ -78,7 +78,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);
return std::make_shared<uuid::console::Shell>(stream, commands, context, flags);
}) {

}
Expand Down Expand Up @@ -115,14 +115,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;
}
}
}
Expand Down Expand Up @@ -205,19 +204,17 @@ TelnetService::Connection::Connection(shell_factory_function &shell_factory, WiF
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 {
Expand All @@ -230,9 +227,14 @@ bool TelnetService::Connection::loop() {
}
}

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;
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/uuid/telnet.h
Expand Up @@ -389,13 +389,6 @@ 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.
*
Expand All @@ -406,17 +399,18 @@ class TelnetService {
/**
* Stop the shell.
*
* @since 0.1.0
* @return True if the shell had not already stopped.
* @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 */
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 */
};
Expand Down

0 comments on commit 3ba72c7

Please sign in to comment.