-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#49 - Server can respond with data back to client.
- Loading branch information
1 parent
9cdf0b4
commit 352105b
Showing
21 changed files
with
299 additions
and
100 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
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
70 changes: 70 additions & 0 deletions
70
lib/smooth/application/network/http/websocket/responses/WSResponse.cpp
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,70 @@ | ||
#include <smooth/application/network/http/websocket/responses/WSResponse.h> | ||
#include <smooth/core/network/util.h> | ||
|
||
namespace smooth::application::network::http::websocket::responses | ||
{ | ||
ResponseStatus WSResponse::get_data(std::size_t max_amount, std::vector<uint8_t>& target) | ||
{ | ||
auto res{ResponseStatus::EndOfData}; | ||
|
||
auto remaining = std::distance(data.begin(), data.end()); | ||
if (remaining > 0) | ||
{ | ||
auto to_send = std::min(static_cast<std::size_t>(remaining), max_amount); | ||
auto begin = data.begin(); | ||
auto end = data.begin() + static_cast<long>(to_send); | ||
|
||
std::copy(std::make_move_iterator(begin), std::make_move_iterator(end), | ||
std::back_inserter(target)); | ||
data.erase(begin, end); | ||
|
||
// Anything still left? | ||
remaining = std::distance(data.begin(), data.end()); | ||
res = remaining > 0 ? ResponseStatus::HasMoreData : ResponseStatus::LastData; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
WSResponse::WSResponse(WebsocketProtocol::OpCode code) | ||
{ | ||
// Set fin-bit | ||
auto val = 0x80 | static_cast<uint8_t>(code); | ||
data.emplace_back(val); | ||
// No payload. | ||
data.emplace_back(0); | ||
} | ||
|
||
WSResponse::WSResponse(const std::string& text) | ||
{ | ||
auto val = 0x80 | static_cast<uint8_t>(WebsocketProtocol::OpCode::Text); | ||
data.emplace_back(val); | ||
if(text.size() <= 125) | ||
{ | ||
data.emplace_back(text.size()); | ||
} | ||
else if(text.size() > std::numeric_limits<uint16_t>::max()) | ||
{ | ||
std::array<uint8_t, 8> size{}; | ||
auto p = reinterpret_cast<uint64_t*>(&size[0]); | ||
*p = static_cast<uint64_t>(text.size()); | ||
*p = smooth::core::network::hton(*p); | ||
data.emplace_back(127); | ||
std::copy(size.begin(), size.end(), std::back_inserter(data)); | ||
data.emplace_back(text.size()); | ||
} | ||
else | ||
{ | ||
std::array<uint8_t, 2> size{}; | ||
auto p = reinterpret_cast<uint16_t*>(&size[0]); | ||
*p = static_cast<uint16_t>(text.size()); | ||
*p = smooth::core::network::hton(*p); | ||
data.emplace_back(126); | ||
std::copy(size.begin(), size.end(), std::back_inserter(data)); | ||
data.emplace_back(text.size()); | ||
} | ||
|
||
|
||
std::copy(text.begin(), text.end(), std::back_inserter(data)); | ||
} | ||
} |
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
Oops, something went wrong.