summaryrefslogtreecommitdiff
path: root/sapsocket.h
blob: 59fcb3c2747e237c9948f80e335b5546c3dee89b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef SAPSOCKET_H
#define SAPSOCKET_H

#include <QtCore/QObject>
#include <QtCore/QQueue>
#include <QtCore/QBasicTimer>

#include "sapchannelinfo.h"

class SAPConnection;
class SAPPeer;

class SAPSocket : public QObject
{
	Q_OBJECT

	SAPSocket(SAPConnection *conn, int sessionId, const SAPChannelInfo &chanInfo);

public:
	SAPPeer *peer();
	SAPConnection *connection();

	SAPChannelInfo channelInfo() const;

	bool isOpen() const;

	bool messageAvailable() const;
	QByteArray receive();
	bool send(const QByteArray &data);

signals:
	void connected();
	void disconnected();
	void messageReceived();

protected:
	void setOpen(bool open);
	void acceptIncomingData(const QByteArray &data);
	void acceptIncomingControl(const QByteArray &data);

	int sessionId() const;

	virtual void timerEvent(QTimerEvent *event) override;

private:
	bool isReliable() const;
	bool supportsFragmentation() const;

	void sendBlockAck(int seqNum);
	void sendPacket(int seqNum, const QByteArray &data);

	void handleBlockAck(int seqNum);
	void sendPacketsFromQueue();

private:
	const int _sessionId;
	const SAPChannelInfo _info;
	bool _open;
	QQueue<QByteArray> _in;
	QQueue<QByteArray> _out;
	QBasicTimer _ackTimer;
	QBasicTimer _resendTimer;

	/** Last acknowledged sent message. */
	quint16 _outLastAck;
	quint16 _outFlyingPkts;

	/** Next expected incoming sequence number */
	quint16 _inLastSeqNum;
	/** Last acknowledged sequence number */
	quint16 _inLastAck;

	friend class SAPPeer;
};

#endif // SAPSOCKET_H