-
-
Notifications
You must be signed in to change notification settings - Fork 347
Added support for sending streams directly from TcpClient. #2341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -27,4 +27,5 @@ | |
| XX(Clocks) \ | ||
| XX(Timers) \ | ||
| XX(HttpRequest) \ | ||
| XX(TcpClient) \ | ||
| XX(Hosted) | ||
This file contains hidden or 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,104 @@ | ||
| #include <HostTests.h> | ||
|
|
||
| #include <Network/TcpClient.h> | ||
| #include <Network/TcpServer.h> | ||
| #include <Data/Stream/MemoryDataStream.h> | ||
| #include <Platform/Station.h> | ||
|
|
||
| class TcpClientTest : public TestGroup | ||
| { | ||
| public: | ||
| TcpClientTest() : TestGroup(_F("TcpClient")) | ||
| { | ||
| } | ||
|
|
||
| void execute() override | ||
| { | ||
| if(!WifiStation.isConnected()) { | ||
| Serial.println("No network, skipping tests"); | ||
| return; | ||
| } | ||
|
|
||
| constexpr int port = 9876; | ||
| String inputData = "This is very long and complex text that will be sent using multiple complicated streams."; | ||
|
|
||
| // Tcp Server | ||
| server = new TcpServer( | ||
| [this](TcpClient& client, char* data, int size) -> bool { | ||
| // on data | ||
| return receivedData.concat(data, size); | ||
| }, | ||
| [this, inputData](TcpClient& client, bool successful) { | ||
| // on client close | ||
| if(finished) { | ||
| return; | ||
| } | ||
| REQUIRE(successful == true); | ||
| REQUIRE(receivedData == inputData); | ||
| finished = true; | ||
| shutdown(); | ||
| }); | ||
| server->listen(port); | ||
| server->setTimeOut(USHRT_MAX); // disable connection timeout | ||
| server->setKeepAlive(USHRT_MAX); // disable connection timeout | ||
|
|
||
| // Tcp Client | ||
| bool connected = client.connect(WifiStation.getIP(), port); | ||
| debug_d("Connected: %d", connected); | ||
|
|
||
| TEST_CASE("TcpClient::send stream") | ||
| { | ||
| size_t offset = 0; | ||
|
|
||
| // Send text using bytes | ||
| client.send(inputData.c_str(), 5); | ||
| offset += 5; | ||
|
|
||
| // send data using more bytes | ||
| client.send(inputData.c_str() + offset, 7); | ||
| offset += 7; | ||
|
|
||
| // send data as stream | ||
| auto stream1 = new MemoryDataStream(); | ||
| stream1->write(inputData.c_str() + offset, 3); | ||
| client.send(stream1); | ||
| offset += 3; | ||
| client.commit(); | ||
|
|
||
| // more stream | ||
| auto stream2 = new LimitedMemoryStream(4); | ||
| stream2->write(reinterpret_cast<const uint8_t*>(inputData.c_str()) + offset, 4); | ||
| client.send(stream2); | ||
| offset += 4; | ||
|
|
||
| // and finally the rest of the bytes | ||
| String rest = inputData.substring(offset); | ||
| client.send(rest.c_str(), rest.length()); | ||
| client.setTimeOut(1); | ||
|
|
||
| pending(); | ||
| } | ||
| } | ||
|
|
||
| void shutdown() | ||
| { | ||
| server->shutdown(); | ||
| server = nullptr; | ||
| timer.initializeMs<1000>([this]() { complete(); }); | ||
| timer.startOnce(); | ||
| } | ||
|
|
||
| private: | ||
| String receivedData; | ||
| TcpServer* server{nullptr}; | ||
| TcpClient client{false}; | ||
| Timer timer; | ||
| volatile bool finished = false; | ||
| }; | ||
|
|
||
| void REGISTER_TEST(TcpClient) | ||
| { | ||
| #ifdef ARCH_HOST | ||
| registerGroup<TcpClientTest>(); | ||
| #endif | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dynamic_castwill require RTTI and, if enabled, would add overhead to every use of Stream sogetStreamType()is preferable here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @mikee47. I will do exactly as suggested. Hope to find some time today to submit the new version of the code.