Skip to content

Commit

Permalink
Partially reverted changes in commit 2eca53a
Browse files Browse the repository at this point in the history
  • Loading branch information
dc42 committed Sep 18, 2024
1 parent d8daa10 commit bc56400
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 39 deletions.
109 changes: 71 additions & 38 deletions src/Networking/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,45 +224,12 @@ void Network::CreateAdditionalInterface() noexcept
#if HAS_NETWORKING

// Terminate all responders that handle a specified protocol (unless AnyProtocol is passed) on a specified interface
// CAUTION: this must only be called by the Network task (not the Main task) because in terminating the responders it releases output buffers that the Network task uses to send data.
void Network::TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol, bool client) noexcept
void Network::TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol) noexcept
{
# if HAS_RESPONDERS
if (!client)
{
for (NetworkResponder *r = responders; r != nullptr; r = r->GetNext())
{
r->Terminate(protocol, iface);
}
}

# if SUPPORT_HTTP
if (protocol == HttpProtocol || protocol == AnyProtocol)
{
HttpResponder::DisableInterface(iface); // remove HTTP sessions that use this interface
}
# endif
# if SUPPORT_FTP
if (protocol == FtpProtocol || protocol == AnyProtocol)
{
FtpResponder::Disable(); // TODO leave any FTP session using a different interface alone
}
# endif
# if SUPPORT_TELNET
if (protocol == TelnetProtocol || protocol == AnyProtocol)
{
TelnetResponder::Disable(); // TODO leave any Telnet session using a different interface alone
}
# endif
# if SUPPORT_MQTT
if (protocol == MqttProtocol || protocol == AnyProtocol)
for (NetworkResponder *r = responders; r != nullptr; r = r->GetNext())
{
MqttClient::Disable(); // TODO leave any Mqtt clients using a different interface alone
r->Terminate(protocol, iface);
}
# endif

// Nothing needed here for Multicast Discovery protocol
# endif
}

#endif
Expand Down Expand Up @@ -327,9 +294,54 @@ GCodeResult Network::DisableProtocol(unsigned int interface, NetworkProtocol pro

NetworkInterface * const iface = interfaces[interface];
const GCodeResult ret = iface->DisableProtocol(protocol, reply, !client);

if (ret == GCodeResult::ok)
{
TerminateResponders(iface, protocol, client);
#if HAS_RESPONDERS
if (!client)
{
TerminateResponders(iface, protocol);
}

switch (protocol)
{
#if SUPPORT_HTTP
case HttpProtocol:
HttpResponder::DisableInterface(iface); // free up output buffers etc.
break;
#endif

#if SUPPORT_FTP
case FtpProtocol:
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
FtpResponder::Disable();
break;
#endif

#if SUPPORT_TELNET
case TelnetProtocol:
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
TelnetResponder::Disable();
break;
#endif

#if SUPPORT_MULTICAST_DISCOVERY
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
case MulticastDiscoveryProtocol:
break;
#endif

#if SUPPORT_MQTT
case MqttProtocol:
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
MqttClient::Disable();
break;
#endif

default:
break;
}
#endif // HAS_RESPONDERS
}
return ret;
}
Expand Down Expand Up @@ -370,7 +382,22 @@ GCodeResult Network::EnableInterface(unsigned int interface, int mode, const Str
const GCodeResult ret = iface->EnableInterface(mode, ssid, reply);
if (ret == GCodeResult::ok && mode < 1) // if disabling the interface
{
TerminateResponders(iface, AnyProtocol, false);
#if HAS_RESPONDERS
TerminateResponders(iface, AnyProtocol);

# if SUPPORT_HTTP
HttpResponder::DisableInterface(iface); // remove sessions that use this interface
# endif
# if SUPPORT_FTP
FtpResponder::Disable(); // TODO leave any Telnet session using a different interface alone
# endif
# if SUPPORT_TELNET
TelnetResponder::Disable(); // TODO leave any Telnet session using a different interface alone
# endif
# if SUPPORT_MQTT
MqttClient::Disable();
# endif
#endif // HAS_RESPONDERS
}
return ret;
}
Expand Down Expand Up @@ -713,8 +740,11 @@ bool Network::UsingDhcp(unsigned int interface) const noexcept
return interface < GetNumNetworkInterfaces() && interfaces[interface]->UsingDhcp();
}

#endif

void Network::SetHostname(const char *name) noexcept
{
#if HAS_NETWORKING
size_t i = 0;
while (*name && i < ARRAY_UPB(hostname))
{
Expand Down Expand Up @@ -746,8 +776,11 @@ void Network::SetHostname(const char *name) noexcept
iface->UpdateHostname(hostname);
}
}
#endif
}

#if HAS_NETWORKING

// Net the MAC address. Pass -1 as the interface number to set the default MAC address for interfaces that don't have one.
GCodeResult Network::SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion src/Networking/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Network INHERIT_OBJECT_MODEL
const char *GetHostname() const noexcept { return hostname; }
void SetHostname(const char *name) noexcept;

void TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol, bool client) noexcept;
void TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol) noexcept;
#endif

#if SUPPORT_HTTP
Expand Down

0 comments on commit bc56400

Please sign in to comment.