Added sendBuffer() methods to DataChannel

This commit is contained in:
Paul-Louis Ageneau
2019-12-12 10:10:44 +01:00
parent 2c0955fe57
commit aa55aa76df
3 changed files with 55 additions and 19 deletions

View File

@ -28,6 +28,7 @@
#include <atomic>
#include <chrono>
#include <functional>
#include <type_traits>
#include <variant>
namespace rtc {
@ -48,6 +49,9 @@ public:
void send(const byte *data, size_t size);
std::optional<std::variant<binary, string>> receive();
template <typename Buffer> void sendBuffer(const Buffer &buf);
template <typename Iterator> void sendBuffer(Iterator first, Iterator last);
size_t available() const;
size_t availableSize() const;
@ -61,6 +65,7 @@ public:
private:
void open(std::shared_ptr<SctpTransport> sctpTransport);
void outgoing(mutable_message_ptr message);
void incoming(message_ptr message);
void processOpenMessage(message_ptr message);
@ -81,6 +86,34 @@ private:
friend class PeerConnection;
};
template <typename Buffer> std::pair<const byte *, size_t> to_bytes(const Buffer &buf) {
using T = typename std::remove_pointer<decltype(buf.data())>::type;
using E = typename std::conditional<std::is_void<T>::value, byte, T>::type;
return std::make_pair(static_cast<const byte *>(static_cast<const void *>(buf.data())),
buf.size() * sizeof(E));
}
template <typename Buffer> void DataChannel::sendBuffer(const Buffer &buf) {
auto [bytes, size] = to_bytes(buf);
auto message = std::make_shared<Message>(size);
std::copy(bytes, bytes + size, message->data());
outgoing(message);
}
template <typename Iterator> void DataChannel::sendBuffer(Iterator first, Iterator last) {
size_t size = 0;
for (Iterator it = first; it != last; ++it)
size += it->size();
auto message = std::make_shared<Message>(size);
auto pos = message->begin();
for (Iterator it = first; it != last; ++it) {
auto [bytes, size] = to_bytes(*it);
pos = std::copy(bytes, bytes + size, pos);
}
outgoing(message);
}
} // namespace rtc
#endif