Skip to content

Commit

Permalink
Fixed reading datachannel in streaming mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Apr 18, 2024
1 parent 6c00bc7 commit 638fd91
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/xmpp/xmpp-im/jingle-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <QDateTime>
#include <QObject>

#include <optional>

namespace XMPP::Jingle::FileTransfer {
struct Range {
std::uint64_t offset = 0; // 0 - default value from spec even when not set.
Expand Down
47 changes: 26 additions & 21 deletions src/xmpp/xmpp-im/jingle-webrtc-datachannel_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <QtEndian>

#include <cstring>

namespace XMPP { namespace Jingle { namespace SCTP {

WebRTCDataChannel::WebRTCDataChannel(AssociationPrivate *association, quint8 channelType, quint32 reliability,
Expand Down Expand Up @@ -127,34 +129,36 @@ namespace XMPP { namespace Jingle { namespace SCTP {
return true;
}

qint64 WebRTCDataChannel::bytesAvailable() const { return _bytesAvailable; }
qint64 WebRTCDataChannel::bytesAvailable() const
{
return tail.size() + _bytesAvailable + Connection::bytesAvailable();
}

qint64 WebRTCDataChannel::bytesToWrite() const { return 0; /*client->bytesToWrite();*/ }

qint64 WebRTCDataChannel::readData(char *buf, qint64 sz)
{
if (sz <= tail.size()) {
std::memcpy(buf, tail.data(), sz);
tail.remove(0, sz);
return sz;
}
sz -= tail.size();
qint64 actualRet = tail.size();
std::memcpy(buf, tail.data(), tail.size());
while (sz > 0 && !datagrams.isEmpty()) {
auto dg = datagrams.takeFirst();
auto data = dg.data();
auto dataSz = std::min(sz, data.size());
std::memcpy(buf + actualRet, data.data(), dataSz);
actualRet += dataSz;
data.remove(0, dataSz);
if (data.size()) {
tail = data;
return actualRet;
qint64 actualSz = 0;
do {
if (tail.isEmpty() && !datagrams.isEmpty()) {
tail = datagrams.takeFirst().data();
}
auto dataSz = std::min(sz, qint64(tail.size()));
std::memcpy(buf + actualSz, tail.data(), dataSz);
if (dataSz == qint64(tail.size())) {
tail.clear();
} else {
tail.remove(0, dataSz);
if (!tail.isEmpty()) {
break;
}
}
actualSz += dataSz;
sz -= dataSz;
}
return actualRet;
} while (sz > 0 && !datagrams.isEmpty());
_bytesAvailable -= actualSz;
// qDebug("read %lld bytes. more %lld is available", actualSz, _bytesAvailable);
return actualSz;
}

void WebRTCDataChannel::close() { XMPP::Jingle::Connection::close(); }
Expand Down Expand Up @@ -205,6 +209,7 @@ namespace XMPP { namespace Jingle { namespace SCTP {
// check other PPIDs.
datagrams.append(QNetworkDatagram { data });
_bytesAvailable += data.size();
// qDebug("datachannel readyread");
emit readyRead();
}

Expand Down

0 comments on commit 638fd91

Please sign in to comment.