-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create proof-of-concept class for outgoing connections
- Loading branch information
Showing
6 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters