-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathudp.cpp
119 lines (108 loc) · 5.18 KB
/
udp.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "widget.h"
#include "message.h"
#include "utils.h"
void Widget::udpProcessPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams())
{
QHostAddress rAddr;
quint16 rPort;
QByteArray datagram;
qint64 dataRead = 0;
int datagramSize = udpSocket->pendingDatagramSize();
datagram.resize(datagramSize);
while(dataRead < datagram.size())
{
qint64 readNow = udpSocket->readDatagram(datagram.data()+dataRead, datagramSize, &rAddr, &rPort); // le +dataRead sur un tableau, ça décale le pointeur d'un offset de taille dataRead !
if(readNow != -1)
{
dataRead += readNow; // Compte le nombre d'octets lus au total, s'arreter quand dataRead atteint pendingDatagramSize
if (datagramSize > (datagram.size() - dataRead)) // Evite de lire après la fin du tableau en mode fragmenté, evite donc que dataSent dépasse datagramSize, sinon Overflow et envoi de données inutiles et aléatoires !!
datagramSize = (datagram.size() - dataRead);
//QMessageBox::information(NULL,"SenderInfo",QString("DatagramSize : %1 sentNow : %2 dataSent : %3 Sizeof(datagram->data()) : %4").arg(QString().setNum(datagramSize),QString().setNum(sentNow),QString().setNum(dataSent),QString().setNum(datagram->size())));
}
else
{
logStatusMessage(QString("Socket error : ").arg(udpSocket->errorString()));
return;
}
}
// Add player on connection
if ((unsigned char)datagram[0]==MsgConnect && (unsigned char)datagram[1]==0 && (unsigned char)datagram[2]==0 && datagram.size()>=22)
{
QString name = dataToString(datagram.right(datagram.size()-22));
QString sesskey = dataToString(datagram.right(datagram.size()-datagram.lastIndexOf(name)-name.size()));
//logMessage(QString("UDP: Connect detected with name : ")+name);
//logMessage(QString("UDP: Connect detected with sesskey : ")+sesskey);
if (QCryptographicHash::hash(QString(sesskey.right(40) + saltPassword).toLatin1(), QCryptographicHash::Md5).toHex() == sesskey.left(32))
{
//logMessage("Sesskey token accepted");
// Create new player if needed, else just update player
Player& newPlayer = Player::findPlayer(udpPlayers, rAddr.toString(),rPort);
if (newPlayer.IP != rAddr.toString()) // IP:Port not found in player list
{
newPlayer.reset();
newPlayer.connected = true;
newPlayer.name = name;
newPlayer.IP = rAddr.toString();
newPlayer.port = rPort;
// Check if we have too many players connected
int n=0;
for (int i=0;i<udpPlayers.size();i++)
if (udpPlayers[i].connected)
n++;
if (n>=maxConnected)
{
sendMessage(newPlayer, MsgDisconnect, "Error : Too much players connected. Try again later.");
}
// If not add the player
udpPlayers << newPlayer;
}
else // IP:Port found in player list
{
if (newPlayer.connected) // TODO: Error, player already connected
{
sendMessage(newPlayer, MsgDisconnect, "Error : Player already connected.");
return;
}
// Check if we have too many players connected
int n=0;
for (int i=0;i<udpPlayers.size();i++)
if (udpPlayers[i].connected)
n++;
if (n>=maxConnected)
{
sendMessage(newPlayer, MsgDisconnect, "Error : Too much players connected. Try again later.");
}
newPlayer.reset();
newPlayer.name = name;
newPlayer.IP = rAddr.toString();
newPlayer.port = rPort;
newPlayer.connected = true;
}
}
else
{
logMessage("UDP: Sesskey rejected");
Player newPlayer;
newPlayer.IP = rAddr.toString();
newPlayer.port = rPort;
sendMessage(newPlayer, MsgDisconnect, "Error : Wrong sesskey hash.");
return;
}
}
Player& player = Player::findPlayer(udpPlayers, rAddr.toString(), rPort);
if (player.IP == rAddr.toString() && player.port == rPort)
{
// Acquire datas
player.receivedDatas->append(datagram);
// Process data
receiveMessage(player);
}
else // You need to connect with TCP first
{
logMessage("UDP: Request from unknow peer rejected : "+player.IP+":"+QString().setNum(rPort));
sendMessage(player,MsgDisconnect);
}
}
}