Skip to content

Commit cd7b213

Browse files
committed
Fix data channel open
Previously, the data channel object was not being created for the peer that generates the offer/datachannel. Included fixes: * Send ACK after receiving DATA_CHANNEL_OPEN * Pass and return uint16_t for all SIDs instead of int * Calls new channel and on_open callbacks inside HandleAck * Some formatting fixes
1 parent 4cc4d74 commit cd7b213

4 files changed

Lines changed: 40 additions & 24 deletions

File tree

include/rtcdcpp/PeerConnection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class PeerConnection {
161161

162162
// DataChannel message parsing
163163
void HandleNewDataChannel(ChunkPtr chunk, uint16_t sid);
164-
void HandleDataChannelAck();
164+
void HandleDataChannelAck(uint16_t sid);
165165
void HandleStringMessage(ChunkPtr chunk, uint16_t sid);
166166
void HandleBinaryMessage(ChunkPtr chunk, uint16_t sid);
167167

include/rtcdcpp/SCTPWrapper.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,19 @@ class SCTPWrapper {
6060
// Handle a decrypted SCTP packet
6161
void DTLSForSCTP(ChunkPtr chunk);
6262

63+
void SendACK();
6364
void CreateDCForSCTP(std::string label, std::string protocol="");
6465

6566
dc_open_msg *data;
66-
int sid;
67+
uint16_t sid;
6768
std::string label;
6869
std::string protocol;
6970

7071
dc_open_msg* GetDataChannelData();
71-
int GetSid();
72+
uint16_t GetSid();
7273
std::string GetProtocol();
7374
std::string GetLabel();
74-
void SetDataChannelSID(int sid);
75+
void SetDataChannelSID(uint16_t sid);
7576

7677
// Send a message to the remote connection
7778
// Note, this will cause 1+ DTLSEncrypt callback calls

src/PeerConnection.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,12 @@ void PeerConnection::OnSCTPMsgReceived(ChunkPtr chunk, uint16_t sid, uint32_t pp
210210
if (ppid == PPID_CONTROL) {
211211
SPDLOG_TRACE(logger, "Control PPID");
212212
if (chunk->Data()[0] == DC_TYPE_OPEN) {
213+
logger->info("DC TYPE OPEN RECEIVED");
213214
SPDLOG_TRACE(logger, "New channel time!");
214215
HandleNewDataChannel(chunk, sid);
215216
} else if (chunk->Data()[0] == DC_TYPE_ACK) {
216217
SPDLOG_TRACE(logger, "DC ACK");
217-
HandleDataChannelAck();
218+
HandleDataChannelAck(sid);
218219
} else {
219220
SPDLOG_TRACE(logger, "Unknown msg_type for ppid control: {}", chunk->Data()[0]);
220221
}
@@ -257,29 +258,26 @@ void PeerConnection::HandleNewDataChannel(ChunkPtr chunk, uint16_t sid) {
257258
auto new_channel = std::make_shared<DataChannel>(this, sid, open_msg.chan_type, label, protocol);
258259

259260
data_channels[sid] = new_channel;
260-
261+
this->sctp->SendACK();
261262
if (this->new_channel_cb) {
262263
this->new_channel_cb(new_channel);
263264
} else {
264265
logger->warn("No new channel callback, ignoring new channel");
265266
}
266267
}
267268

268-
void PeerConnection::HandleDataChannelAck() {
269-
dc_open_msg* datachannel_data = this->sctp->GetDataChannelData();
270-
int sid = this->sctp->GetSid();
271-
std::string label = this->sctp->GetLabel();
272-
std::string protocol = this->sctp->GetProtocol();
273-
// TODO: Support overriding an existing channel
274-
auto new_channel = std::make_shared<DataChannel>(this, sid, datachannel_data->chan_type, label, protocol);
275-
276-
data_channels[sid] = new_channel;
277-
269+
void PeerConnection::HandleDataChannelAck(uint16_t sid) {
270+
auto new_channel = GetChannel(sid);
278271
if (this->new_channel_cb) {
279272
this->new_channel_cb(new_channel);
280273
} else {
281274
logger->warn("No new channel callback, ignoring new channel");
282275
}
276+
if (!new_channel) {
277+
logger->warn("Cannot find the datachannel for sid {}", sid);
278+
} else {
279+
new_channel->OnOpen();
280+
}
283281
}
284282

285283
void PeerConnection::HandleStringMessage(ChunkPtr chunk, uint16_t sid) {
@@ -314,20 +312,25 @@ void PeerConnection::SendBinaryMsg(const uint8_t *data, int len, uint16_t sid) {
314312
}
315313

316314
void PeerConnection::CreateDataChannel(std::string label, std::string protocol) {
317-
int sid;
318-
if(this->role == 0){
315+
uint16_t sid;
316+
if (this->role == 0) {
319317
sid = 0;
320318
} else {
321319
sid = 1;
322320
}
323-
for(int i = sid; i < data_channels.size(); i = i + 2){
321+
for (int i = sid; i < data_channels.size(); i = i + 2) {
324322
auto iter = data_channels.find(i);
325323
if (iter == data_channels.end()) {
326324
sid = i;
327325
break;
328326
}
329327
}
330-
this->sctp->SetDataChannelSID(sid);
328+
329+
this->sctp->SetDataChannelSID(sid);
330+
331+
auto new_channel = std::make_shared<DataChannel>(this, sid, DATA_CHANNEL_RELIABLE, label, protocol);
332+
data_channels[sid] = new_channel;
333+
331334
std::thread create_dc = std::thread(&SCTPWrapper::CreateDCForSCTP, sctp.get(), label, protocol);
332335
logger->info("Spawning create_dc thread");
333336
create_dc.detach();

src/SCTPWrapper.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ void SCTPWrapper::Stop() {
295295

296296
void SCTPWrapper::DTLSForSCTP(ChunkPtr chunk) { this->recv_queue.push(chunk); }
297297

298-
int SCTPWrapper::GetSid(){
298+
uint16_t SCTPWrapper::GetSid(){
299299
return this->sid;
300300
}
301301

@@ -309,11 +309,23 @@ std::string SCTPWrapper::GetLabel(){
309309
std::string SCTPWrapper::GetProtocol(){
310310
return this->label;
311311
}
312-
void SCTPWrapper::SetDataChannelSID(int sid)
312+
void SCTPWrapper::SetDataChannelSID(uint16_t sid)
313313
{
314314
this->sid = sid;
315315
}
316-
316+
void SCTPWrapper::SendACK() {
317+
struct sctp_sndinfo sinfo = {0}; //
318+
int sid;
319+
sid = this->sid;
320+
sinfo.snd_sid = sid;
321+
sinfo.snd_ppid = htonl(PPID_CONTROL);
322+
uint8_t payload = DC_TYPE_ACK;
323+
if (usrsctp_sendv(this->sock, &payload, sizeof(uint8_t), NULL, 0, &sinfo, sizeof(sinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
324+
logger->error("Sending ACK failed");
325+
} else {
326+
logger->info("Ack has gone through");
327+
}
328+
}
317329
void SCTPWrapper::CreateDCForSCTP(std::string label, std::string protocol) {
318330

319331
std::unique_lock<std::mutex> l2(createDCMtx);
@@ -322,7 +334,7 @@ void SCTPWrapper::CreateDCForSCTP(std::string label, std::string protocol) {
322334
}
323335
struct sctp_sndinfo sinfo = {0};
324336
int sid;
325-
sid = this->sid ;
337+
sid = this->sid;
326338
sinfo.snd_sid = sid;
327339
sinfo.snd_ppid = htonl(PPID_CONTROL);
328340

0 commit comments

Comments
 (0)