summaryrefslogtreecommitdiff
path: root/sapsocket.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sapsocket.cc')
-rw-r--r--sapsocket.cc45
1 files changed, 37 insertions, 8 deletions
diff --git a/sapsocket.cc b/sapsocket.cc
index 472865c..b41e8f5 100644
--- a/sapsocket.cc
+++ b/sapsocket.cc
@@ -1,12 +1,15 @@
#include <QtCore/QDebug>
+#include <QtCore/QTimerEvent>
#include "sappeer.h"
#include "sapconnection.h"
#include "sapsocket.h"
+#define DELAYED_ACK_TIME 1000
+
SAPSocket::SAPSocket(SAPConnection *conn, int sessionId, const SAPChannelInfo &chanInfo) :
QObject(conn), _sessionId(sessionId), _info(chanInfo), _open(false),
- _seqNum(0), _expectedSeqNum(0)
+ _outLastSeqNum(0), _inLastSeqNum(0), _inLastAck(0)
{
}
@@ -48,16 +51,22 @@ bool SAPSocket::send(const QByteArray &data)
{
SAProtocol::DataFrame frame;
+ if (!isOpen()) {
+ qWarning() << "Socket is not yet open";
+ return false;
+ }
+
frame.withSeqNum = isWithSeqNum();
if (isReliable()) {
- frame.seqNum = _seqNum++;
+ frame.seqNum = ++_outLastSeqNum;
} else {
frame.seqNum = 0;
}
frame.unk_1 = 0;
frame.data = data;
- return peer()->writeToSession(_sessionId, SAProtocol::packDataFrame(frame));
+ peer()->writeDataToSession(_sessionId, SAProtocol::packDataFrame(frame));
+ return true;
}
void SAPSocket::setOpen(bool open)
@@ -71,14 +80,20 @@ void SAPSocket::acceptIncomingData(const QByteArray &data)
SAProtocol::DataFrame frame = SAProtocol::unpackDataFrame(data, isWithSeqNum());
if (isReliable()) {
- if (frame.seqNum != _expectedSeqNum) {
+ quint16 expectedSeqNum = _inLastSeqNum + 1;
+ if (frame.seqNum != expectedSeqNum) {
qWarning() << "Unexpected sequence number" << frame.seqNum
<< "on session" << _sessionId
- << "(expected " << _expectedSeqNum << ")";
- }
- _expectedSeqNum = frame.seqNum + 1;
+ << "(expected " << expectedSeqNum << ")";
+ } else {
+ _inLastSeqNum = frame.seqNum;
+
+ qDebug() << "Realiable received" << _inLastSeqNum;
- // TODO Do we actually need to ack this somehow?
+ if (!_timer.isActive()) {
+ _timer.start(DELAYED_ACK_TIME, Qt::CoarseTimer, this);
+ }
+ }
}
_in.enqueue(frame.data);
@@ -91,6 +106,20 @@ int SAPSocket::sessionId() const
return _sessionId;
}
+void SAPSocket::timerEvent(QTimerEvent *event)
+{
+ if (event->timerId() == _timer.timerId()) {
+ if (_inLastSeqNum != _inLastAck) {
+ qDebug() << "Acking" << _inLastAck << _inLastSeqNum;
+ peer()->writeAckToSession(_sessionId, _inLastSeqNum);
+ _inLastAck = _inLastSeqNum;
+ }
+ _timer.stop();
+ } else {
+ QObject::timerEvent(event);
+ }
+}
+
bool SAPSocket::isReliable() const
{
return _info.qosType() == SAPChannelInfo::QoSReliabilityEnable;