Skip to content

Commit

Permalink
Create proof-of-concept class for outgoing connections
Browse files Browse the repository at this point in the history
  • Loading branch information
rechrtb committed Oct 14, 2022
1 parent a3b38dd commit 9992576
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Networking/ESP8266WiFi/WiFiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept
for (size_t i = 0; i < NumProtocols; ++i)
{
portNumbers[i] = DefaultPortNumbers[i];
protocolEnabled[i] = (i == HttpProtocol);
protocolEnabled[i] = (i == HttpProtocol) || (i == HelloHiProtocol);
}

strcpy(actualSsid, "(unknown)");
Expand Down Expand Up @@ -431,6 +431,10 @@ void WiFiInterface::StartProtocol(NetworkProtocol protocol) noexcept
SendListenCommand(portNumbers[protocol], protocol, 1);
break;

case HelloHiProtocol:
SendConnectCommand(portNumbers[protocol], protocol, 4094339264);
break;

default:
break;
}
Expand Down
79 changes: 79 additions & 0 deletions src/Networking/HelloHiResponder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* HelloHiResponder.cpp
*/

#include "HelloHiResponder.h"
#include "Socket.h"

HelloHiResponder::HelloHiResponder(NetworkResponder *n) noexcept : NetworkResponder(n), lastSend(0), lenRead(0)
{

}

bool HelloHiResponder::Spin() noexcept
{
switch (responderState)
{
case ResponderState::sending:
if (skt->CanSend() && (millis() - lastSend) > 500)
{
const char *greeting = "Hello!";
skt->Send((const uint8_t*)greeting, strlen(greeting));
responderState = ResponderState::reading;
return true;
}
break;

case ResponderState::reading:
if (skt->CanRead())
{
const char reply[] = "Hi!";

const uint8_t *buf;
size_t len = 0;
skt->ReadBuffer(buf, len);
skt->Taken(len);

lenRead += len;

if (lenRead == strlen(reply))
{
responderState = ResponderState::sending;
lastSend = millis();
lenRead = 0;
}
return true;
}
break;

default:
break;
}
return false;
}

bool HelloHiResponder::Accept(Socket *s, NetworkProtocol protocol) noexcept
{
if (responderState == ResponderState::free && protocol == HelloHiProtocol)
{
skt = s;
responderState = ResponderState::sending;
lastSend = millis();
lenRead = 0;
return true;
}
return false;
}

void HelloHiResponder::Terminate(NetworkProtocol protocol, NetworkInterface *interface) noexcept
{
if (responderState != ResponderState::free && (protocol == HelloHiProtocol || protocol == AnyProtocol) && skt != nullptr && skt->GetInterface() == interface)
{
lastSend = 0;
ConnectionLost();
}
}

void HelloHiResponder::Diagnostics(MessageType mt) const noexcept
{
}
26 changes: 26 additions & 0 deletions src/Networking/HelloHiResponder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* HelloHiResponder.h
*
*/

#ifndef SRC_NETWORKING_HELLOHIRESPONDER_H_
#define SRC_NETWORKING_HELLOHIRESPONDER_H_

#include "NetworkResponder.h"

class HelloHiResponder : public NetworkResponder
{
public:
HelloHiResponder(NetworkResponder *n) noexcept;

bool Spin() noexcept override; // do some work, returning true if we did anything significant
bool Accept(Socket *s, NetworkProtocol protocol) noexcept override; // ask the responder to accept this connection, returns true if it did
void Terminate(NetworkProtocol protocol, NetworkInterface *interface) noexcept override; // terminate the responder if it is serving the specified protocol on the specified interface
void Diagnostics(MessageType mtype) const noexcept override;

private:
uint32_t lastSend;
uint8_t lenRead;
};

#endif /* SRC_NETWORKING_HELLOHIRESPONDER_H_ */
7 changes: 7 additions & 0 deletions src/Networking/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
# include "RTOSPlusTCPEthernet/RTOSPlusTCPEthernetInterface.h"
#endif

#include "HelloHiResponder.h"

#if SUPPORT_HTTP
# include "HttpResponder.h"
#endif
Expand Down Expand Up @@ -461,6 +463,11 @@ void Network::Activate() noexcept
MulticastResponder::Init();
#endif

for (size_t i = 0; i < NumHelloHiResponders; ++i)
{
responders = new HelloHiResponder(responders);
}

// Finally, create the network task
networkTask.Create(NetworkLoop, "NETWORK", nullptr, TaskPriority::SpinPriority);
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/Networking/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const size_t MaxNetworkInterfaces = 1;
# error Wrong Network.h file included
#endif

const size_t NumHelloHiResponders = 1;

#if defined(__LPC17xx__)
// Only 2 http responders as we are tight on memory.
const size_t NumHttpResponders = 2; // the number of concurrent HTTP requests we can process
Expand Down
11 changes: 7 additions & 4 deletions src/Networking/NetworkDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ constexpr IPAddress DefaultIpAddress; // will be initialised to 0 by construc
constexpr IPAddress DefaultNetMask(0x00FFFFFF); // equivalent to 255.255.255.0. Use constexpr constructor to avoid it being allocated in RAM.
constexpr IPAddress DefaultGateway; // will be initialised to 0 by constructor

constexpr size_t NumTcpProtocols = 3;
constexpr size_t NumTcpProtocols = 4;

#if SUPPORT_MULTICAST_DISCOVERY
constexpr size_t NumProtocols = NumTcpProtocols + 1; // number of network protocols we support, not counting FtpDataProtocol, MdnsProtocol or AnyProtocol
#else
constexpr size_t NumProtocols = NumTcpProtocols; // number of network protocols we support, not counting FtpDataProtocol, MdnsProtocol or AnyProtocol
#endif

constexpr NetworkProtocol HttpProtocol = 0, FtpProtocol = 1, TelnetProtocol = 2, MulticastDiscoveryProtocol = 3, FtpDataProtocol = 3, MdnsProtocol = 4, AnyProtocol = 255;
constexpr NetworkProtocol HttpProtocol = 0, FtpProtocol = 1, TelnetProtocol = 2, MulticastDiscoveryProtocol = 3, FtpDataProtocol = 3, HelloHiProtocol = 3, MdnsProtocol = 4, AnyProtocol = 255;

constexpr TcpPort DefaultHttpPort = 80;
constexpr TcpPort DefaultFtpPort = 21;
constexpr TcpPort DefaultTelnetPort = 23;
constexpr TcpPort DefaultHelloHiPort = 1883;
#if SUPPORT_MULTICAST_DISCOVERY
constexpr TcpPort DefaultMulticastDiscoveryPort = 10002; // this is actually a UDP port
#endif
Expand All @@ -57,15 +58,17 @@ constexpr TcpPort DefaultPortNumbers[NumProtocols] =
{
DefaultHttpPort, DefaultFtpPort, DefaultTelnetPort,
#if SUPPORT_MULTICAST_DISCOVERY
DefaultMulticastDiscoveryPort
DefaultMulticastDiscoveryPort,
#endif
DefaultHelloHiPort,
};
constexpr const char *_ecv_array ProtocolNames[NumProtocols] =
{
"HTTP", "FTP", "TELNET",
#if SUPPORT_MULTICAST_DISCOVERY
"Multicast Discovery"
"Multicast Discovery",
#endif
"HiHello"
};

constexpr uint8_t MdnsMacAddress[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFB };
Expand Down

0 comments on commit 9992576

Please sign in to comment.