mirror of
https://github.com/mii443/libdatachannel.git
synced 2025-08-31 11:29:28 +00:00
Added sendBuffer() methods to DataChannel
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user