-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_udp.h
68 lines (49 loc) · 1.73 KB
/
socket_udp.h
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
#ifndef SOCKET_UDP_H
#define SOCKET_UDP_H
#include <QObject>
#include <QQueue>
#include <QThread>
#include <QUdpSocket>
#include <QHostAddress>
#include <chrono>
#include "chat_packet_udp.h"
class SocketUDP : public QObject
{
Q_OBJECT
private:
QUdpSocket* uSocket;
// 预置信息
const QHostAddress serverAddr = QHostAddress("10.128.206.236");
const quint16 serverPort = 8002;
const quint16 clientPort = 8003;
// 最大单包内容大小
// UDP max payloadSize is 65507 Bytes (packetSize <= 65535)
// but payloadSize <= 548 (packetSize <= 576) will not cause fragmentation
// For macOS, 8192 bytes is at most.
const quint16 maxPayloadSize = 8192;
std::chrono::steady_clock::time_point timeLastSent = std::chrono::steady_clock::now();
double waitToSendUs = 500;
// 本机信息
QHostAddress thisAddr;
quint16 thisPort;
// 接收信息
QHostAddress receivedAddr;
quint16 receivedPort;
QByteArray receivedData;
signals:
void on_receivedData(); // 信号先由socket传递给本类,从缓冲区读取完成后再发信号通知mainWindow取走
private slots:
void on_receiveData();
public:
SocketUDP();
~SocketUDP();
QHostAddress GetReceivedAddr() { return this->receivedAddr; };
quint16 GetReceivedPort() { return this->receivedPort; };
QByteArray GetReceivedData() { return this->receivedData; };
quint16 MaxPacketSize() { return this->maxPayloadSize; }
QHostAddress ServerAddr() { return this->serverAddr; }
quint16 ServerPort() { return this->serverPort; }
bool SendPackedBytes(QByteArray& bytesPacked, QHostAddress targetAddr, quint16 targetPort);
bool SendPackedBytes(QByteArray& bytesPacked);
};
#endif // SOCKET_UDP_H