Added available and availableSize getters on DataChannel

This commit is contained in:
Paul-Louis Ageneau
2019-12-04 12:00:40 +01:00
parent 5d57b4e214
commit a1df562785
2 changed files with 11 additions and 0 deletions

View File

@ -46,6 +46,9 @@ public:
void send(const byte *data, size_t size); void send(const byte *data, size_t size);
std::optional<std::variant<binary, string>> receive(); std::optional<std::variant<binary, string>> receive();
size_t available() const;
size_t availableSize() const;
unsigned int stream() const; unsigned int stream() const;
string label() const; string label() const;
string protocol() const; string protocol() const;
@ -69,6 +72,7 @@ private:
std::atomic<bool> mIsClosed = false; std::atomic<bool> mIsClosed = false;
Queue<message_ptr> mRecvQueue; Queue<message_ptr> mRecvQueue;
std::atomic<size_t> mRecvSize = 0;
friend class PeerConnection; friend class PeerConnection;
}; };

View File

@ -118,9 +118,11 @@ std::optional<std::variant<binary, string>> DataChannel::receive() {
break; break;
} }
case Message::String: case Message::String:
mRecvSize -= message->size();
return std::make_optional( return std::make_optional(
string(reinterpret_cast<const char *>(message->data()), message->size())); string(reinterpret_cast<const char *>(message->data()), message->size()));
case Message::Binary: case Message::Binary:
mRecvSize -= message->size();
return std::make_optional(std::move(*message)); return std::make_optional(std::move(*message));
} }
} }
@ -128,6 +130,10 @@ std::optional<std::variant<binary, string>> DataChannel::receive() {
return nullopt; return nullopt;
} }
size_t DataChannel::available() const { return mRecvQueue.size(); }
size_t DataChannel::availableSize() const { return mRecvSize; }
unsigned int DataChannel::stream() const { return mStream; } unsigned int DataChannel::stream() const { return mStream; }
string DataChannel::label() const { return mLabel; } string DataChannel::label() const { return mLabel; }
@ -197,6 +203,7 @@ void DataChannel::incoming(message_ptr message) {
} }
case Message::String: case Message::String:
case Message::Binary: case Message::Binary:
mRecvSize += message->size();
mRecvQueue.push(message); mRecvQueue.push(message);
triggerAvailable(mRecvQueue.size()); triggerAvailable(mRecvQueue.size());
break; break;