-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.cpp
100 lines (85 loc) · 2.24 KB
/
client.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "client.h"
#include "ui_client.h"
Client::Client(QWidget *parent) :
QWidget(parent),
m_ui(new Ui::Client)
{
m_ui->setupUi(this);
s = new QUdpSocket();
s->bind(QHostAddress("192.168.1.111"),4567);
connect(s, SIGNAL(readyRead()), this, SLOT(ready_read()));
current_id = 0;
data = new char[PACKAGE_SIZE + sizeof(DataPacketHeader)]; //space for the last header in spite of override
sign = new char[PACKET_NUM];
header = new DataPacketHeader();
img = new QImage((unsigned char *)data, 320, 240, QImage::Format_RGB888);
}
Client::~Client()
{
delete m_ui;
delete s;
}
void Client::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
void Client::ready_read()
{
if(s->hasPendingDatagrams())
{
quint64 len = s->pendingDatagramSize();
char *read_addr = data;
quint16 i;
for(i = 0; i < PACKET_NUM; ++i)
{
if(sign[i] == 1)
{
continue;
}
read_addr += i * PAYLOAD_SIZE;
break;
}
if(-1 == s->readDatagram(read_addr, len, &addr, &port)) //read packet header information
{
qDebug("receive data error");
return;
}
packet = (DataPacket *)read_addr;
process(packet);
if(i + 1 >= PACKET_NUM)
{
updatePicture();
memset(sign, 0, PACKET_NUM);
}
}
}
void Client::process(DataPacket *packet)
{
if(current_id < packet->header.id)
{
memset(sign, 0, PACKET_NUM);
current_id = packet->header.id;
}
memcpy(header, &packet->header, sizeof(DataPacketHeader));
if( (((char *)packet - data) / PAYLOAD_SIZE != (signed)packet->header.num) &&
(sign[packet->header.num] == 0))
{
char *dest = data + PAYLOAD_SIZE * packet->header.num;
memcpy(dest, packet, PAYLOAD_SIZE);
}
sign[header->num] = 1;
}
void Client::updatePicture()
{
if(img->isNull())
qDebug("img null");
img->loadFromData((uchar *)data, PACKAGE_SIZE);
m_ui->label->setPixmap(QPixmap::fromImage(*img));
}