Fixed unordered flag and renamed reliability type for consistency

This commit is contained in:
Paul-Louis Ageneau
2020-08-10 10:05:30 +02:00
parent 980ee303c8
commit e2c42ff73b
4 changed files with 41 additions and 28 deletions

View File

@ -27,13 +27,9 @@
namespace rtc { namespace rtc {
struct Reliability { struct Reliability {
enum Type : uint8_t { enum class Type { Reliable = 0, Rexmit, Timed };
TYPE_RELIABLE = 0x00,
TYPE_PARTIAL_RELIABLE_REXMIT = 0x01,
TYPE_PARTIAL_RELIABLE_TIMED = 0x02,
};
Type type = TYPE_RELIABLE; Type type = Type::Reliable;
bool unordered = false; bool unordered = false;
std::variant<int, std::chrono::milliseconds> rexmit = 0; std::variant<int, std::chrono::milliseconds> rexmit = 0;
}; };

View File

@ -31,6 +31,7 @@ namespace rtc {
using std::shared_ptr; using std::shared_ptr;
using std::weak_ptr; using std::weak_ptr;
using std::chrono::milliseconds;
// Messages for the DataChannel establishment protocol // Messages for the DataChannel establishment protocol
// See https://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09 // See https://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09
@ -43,6 +44,12 @@ enum MessageType : uint8_t {
MESSAGE_CLOSE = 0x04 MESSAGE_CLOSE = 0x04
}; };
enum ChannelType : uint8_t {
CHANNEL_RELIABLE = 0x00,
CHANNEL_PARTIAL_RELIABLE_REXMIT = 0x01,
CHANNEL_PARTIAL_RELIABLE_TIMED = 0x02
};
#pragma pack(push, 1) #pragma pack(push, 1)
struct OpenMessage { struct OpenMessage {
uint8_t type = MESSAGE_OPEN; uint8_t type = MESSAGE_OPEN;
@ -168,22 +175,33 @@ size_t DataChannel::availableAmount() const { return mRecvQueue.amount(); }
void DataChannel::open(shared_ptr<SctpTransport> transport) { void DataChannel::open(shared_ptr<SctpTransport> transport) {
mSctpTransport = transport; mSctpTransport = transport;
uint8_t channelType = static_cast<uint8_t>(mReliability->type); uint8_t channelType;
if (mReliability->unordered) uint32_t reliabilityParameter;
channelType &= 0x80; switch (mReliability->type) {
case Reliability::Type::Rexmit:
using std::chrono::milliseconds; channelType = CHANNEL_PARTIAL_RELIABLE_REXMIT;
uint32_t reliabilityParameter = 0;
if (mReliability->type == Reliability::TYPE_PARTIAL_RELIABLE_REXMIT)
reliabilityParameter = uint32_t(std::get<int>(mReliability->rexmit)); reliabilityParameter = uint32_t(std::get<int>(mReliability->rexmit));
else if (mReliability->type == Reliability::TYPE_PARTIAL_RELIABLE_TIMED) break;
case Reliability::Type::Timed:
channelType = CHANNEL_PARTIAL_RELIABLE_TIMED;
reliabilityParameter = uint32_t(std::get<milliseconds>(mReliability->rexmit).count()); reliabilityParameter = uint32_t(std::get<milliseconds>(mReliability->rexmit).count());
break;
default:
channelType = CHANNEL_RELIABLE;
reliabilityParameter = 0;
break;
}
if (mReliability->unordered)
channelType |= 0x80;
const size_t len = sizeof(OpenMessage) + mLabel.size() + mProtocol.size(); const size_t len = sizeof(OpenMessage) + mLabel.size() + mProtocol.size();
binary buffer(len, byte(0)); binary buffer(len, byte(0));
auto &open = *reinterpret_cast<OpenMessage *>(buffer.data()); auto &open = *reinterpret_cast<OpenMessage *>(buffer.data());
open.type = MESSAGE_OPEN; open.type = MESSAGE_OPEN;
open.channelType = mReliability->type; open.channelType = channelType;
open.priority = htons(0); open.priority = htons(0);
open.reliabilityParameter = htonl(reliabilityParameter); open.reliabilityParameter = htonl(reliabilityParameter);
open.labelLength = htons(uint16_t(mLabel.size())); open.labelLength = htons(uint16_t(mLabel.size()));
@ -272,19 +290,18 @@ void DataChannel::processOpenMessage(message_ptr message) {
mLabel.assign(end, open.labelLength); mLabel.assign(end, open.labelLength);
mProtocol.assign(end + open.labelLength, open.protocolLength); mProtocol.assign(end + open.labelLength, open.protocolLength);
using std::chrono::milliseconds;
mReliability->unordered = (open.reliabilityParameter & 0x80) != 0; mReliability->unordered = (open.reliabilityParameter & 0x80) != 0;
switch (open.channelType & 0x7F) { switch (open.channelType & 0x7F) {
case Reliability::TYPE_PARTIAL_RELIABLE_REXMIT: case CHANNEL_PARTIAL_RELIABLE_REXMIT:
mReliability->type = Reliability::TYPE_PARTIAL_RELIABLE_REXMIT; mReliability->type = Reliability::Type::Rexmit;
mReliability->rexmit = int(open.reliabilityParameter); mReliability->rexmit = int(open.reliabilityParameter);
break; break;
case Reliability::TYPE_PARTIAL_RELIABLE_TIMED: case CHANNEL_PARTIAL_RELIABLE_TIMED:
mReliability->type = Reliability::TYPE_PARTIAL_RELIABLE_TIMED; mReliability->type = Reliability::Type::Timed;
mReliability->rexmit = milliseconds(open.reliabilityParameter); mReliability->rexmit = milliseconds(open.reliabilityParameter);
break; break;
default: default:
mReliability->type = Reliability::TYPE_RELIABLE; mReliability->type = Reliability::Type::Reliable;
mReliability->rexmit = int(0); mReliability->rexmit = int(0);
} }

View File

@ -254,14 +254,14 @@ int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
r.unordered = reliability->unordered; r.unordered = reliability->unordered;
if (reliability->unreliable) { if (reliability->unreliable) {
if (reliability->maxPacketLifeTime > 0) { if (reliability->maxPacketLifeTime > 0) {
r.type = Reliability::TYPE_PARTIAL_RELIABLE_TIMED; r.type = Reliability::Type::Timed;
r.rexmit = milliseconds(reliability->maxPacketLifeTime); r.rexmit = milliseconds(reliability->maxPacketLifeTime);
} else { } else {
r.type = Reliability::TYPE_PARTIAL_RELIABLE_REXMIT; r.type = Reliability::Type::Rexmit;
r.rexmit = int(reliability->maxRetransmits); r.rexmit = int(reliability->maxRetransmits);
} }
} else { } else {
r.type = Reliability::TYPE_RELIABLE; r.type = Reliability::Type::Reliable;
} }
} }
auto peerConnection = getPeerConnection(pc); auto peerConnection = getPeerConnection(pc);
@ -499,10 +499,10 @@ int rtcGetDataChannelReliability(int dc, rtcReliability *reliability) {
Reliability r = dataChannel->reliability(); Reliability r = dataChannel->reliability();
std::memset(reliability, sizeof(*reliability), 0); std::memset(reliability, sizeof(*reliability), 0);
reliability->unordered = r.unordered; reliability->unordered = r.unordered;
if (r.type == Reliability::TYPE_PARTIAL_RELIABLE_TIMED) { if (r.type == Reliability::Type::Timed) {
reliability->unreliable = true; reliability->unreliable = true;
reliability->maxPacketLifeTime = std::get<milliseconds>(r.rexmit).count(); reliability->maxPacketLifeTime = std::get<milliseconds>(r.rexmit).count();
} else if (r.type == Reliability::TYPE_PARTIAL_RELIABLE_REXMIT) { } else if (r.type == Reliability::Type::Rexmit) {
reliability->unreliable = true; reliability->unreliable = true;
reliability->maxRetransmits = unsigned(std::get<int>(r.rexmit)); reliability->maxRetransmits = unsigned(std::get<int>(r.rexmit));
} else { } else {

View File

@ -372,12 +372,12 @@ bool SctpTransport::trySendMessage(message_ptr message) {
spa.sendv_sndinfo.snd_flags |= SCTP_UNORDERED; spa.sendv_sndinfo.snd_flags |= SCTP_UNORDERED;
switch (reliability.type) { switch (reliability.type) {
case Reliability::TYPE_PARTIAL_RELIABLE_REXMIT: case Reliability::Type::Rexmit:
spa.sendv_flags |= SCTP_SEND_PRINFO_VALID; spa.sendv_flags |= SCTP_SEND_PRINFO_VALID;
spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_RTX; spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_RTX;
spa.sendv_prinfo.pr_value = uint32_t(std::get<int>(reliability.rexmit)); spa.sendv_prinfo.pr_value = uint32_t(std::get<int>(reliability.rexmit));
break; break;
case Reliability::TYPE_PARTIAL_RELIABLE_TIMED: case Reliability::Type::Timed:
spa.sendv_flags |= SCTP_SEND_PRINFO_VALID; spa.sendv_flags |= SCTP_SEND_PRINFO_VALID;
spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_TTL; spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_TTL;
spa.sendv_prinfo.pr_value = uint32_t(std::get<milliseconds>(reliability.rexmit).count()); spa.sendv_prinfo.pr_value = uint32_t(std::get<milliseconds>(reliability.rexmit).count());