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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/*
# Copyright (c) 2010 - OpenSLX Project, Computer Center University of Freiburg
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# src/net/pvsMsg.cpp
# - ??? .
# -----------------------------------------------------------------------------
*/
#include "pvsMsg.h"
#include <cstring>
#include <QtNetwork/QUdpSocket>
#include <QtNetwork/QHostAddress>
#ifndef min
#define min(a,b) (((a)<(b))?(a):(b))
#endif
static unsigned char PVS_ID = 'P';
// Several constructors for the PVSMsg class...
PVSMsg::PVSMsg()
{
_msgType = PVSMESSAGE;
_buffer = NULL;
_buffLen = 0;
_buffFill = 0;
}
PVSMsg::PVSMsg(PVSMsgType type, QString ident, QString msg, int recID)
{
this->_recID = recID;
if (ident.size())
_msgIdent = ident;
else
_msgIdent = QString("invalid");
_msgType = type;
_msgString = msg;
_buffer = NULL;
_buffLen = 0;
_buffFill = 0;
}
PVSMsg::PVSMsg(PVSMsg const& copyMe)
{
_msgType = copyMe._msgType;
_msgString = copyMe._msgString;
_msgIdent = copyMe._msgIdent;
_remoteIp = copyMe._remoteIp;
_buffLen = copyMe._buffLen;
_buffFill = copyMe._buffFill;
if (_buffLen > 0)
{
_buffer = new char[_buffLen];
memcpy(_buffer, copyMe._buffer, _buffLen);
}
else
_buffer = NULL;
_sndID = copyMe._sndID;
_recID = copyMe._recID;
}
// Destructor...
PVSMsg::~PVSMsg()
{
if (_buffer)
delete[] _buffer;
}
int PVSMsg::readMessage(QAbstractSocket* sock, bool udp)
{
if (_buffFill > 0 && _buffLen <= _buffFill)
return 1;
qint64 ret = 0;
if (_buffFill < 4) // message header is not complete yet
{
if (_buffer == NULL)
{
_remoteIp = sock->peerAddress().toString();
if (udp)
_buffer = new char[1000];
else
_buffer = new char[4];
}
// Read as many bytes as needed to complete the 4 byte header
if (udp)
ret = ((QUdpSocket*)sock)->readDatagram(_buffer, 1000);
else
ret = sock->read(_buffer + _buffFill, 4 - _buffFill);
if (ret == -1) return -1;
_buffFill += (int)ret;
if (_buffFill == 0)
return 0;
if (_buffer[0] != PVS_ID)
{
// check if the id byte is correct
// otherwise either the message parsing is broken
// or someone is sending garbage
return -1;
}
if (_buffFill >= 4)
{
// header complete, allocate buffer
_buffLen = 4 + (unsigned char)_buffer[2] + (unsigned char)_buffer[3];
char *nb = new char[_buffLen + 1]; // +1 for \0
memcpy(nb, _buffer, _buffFill);
delete[] _buffer;
_buffer = nb;
}
}
if (_buffFill >= 4)
{
// got header, assemble rest of msg
if (!udp)
{
ret = sock->read(_buffer + _buffFill, _buffLen - _buffFill); // read remaining bytes
if (ret == -1) return -1;
_buffFill += (int)ret;
}
if (_buffFill >= _buffLen)
{
// message is complete
_buffer[_buffLen] = '\0'; // first, terminate with nullchar
_msgString = QString::fromUtf8(_buffer + 4 + (unsigned char)_buffer[2]); // get message
_buffer[4 + (unsigned char)_buffer[2]] = '\0';
_msgIdent = QString::fromUtf8(_buffer + 4); // get ident
_msgType = (PVSMsgType)_buffer[1]; // get msg type
return 1;
}
}
return 0;
}
void PVSMsg::setMessage(QString text)
{
_msgString = text;
}
void PVSMsg::setIdent(QString ident)
{
_msgIdent = ident;
}
QString PVSMsg::getMessage()
{
return _msgString;
}
QString PVSMsg::getIdent()
{
return _msgIdent;
}
bool PVSMsg::getBinaryData(char*& data, int& dataLen)
{
if (_msgIdent.size() == 0)
return false;
this->makeSndBuff();
data = (char*) _buffer;
dataLen = _buffLen;
return true;
}
void PVSMsg::setSndID(int id)
{
_sndID = id;
}
void PVSMsg::setRecID(int id)
{
_recID = id;
}
// Create a send buffer with the necessary size
bool PVSMsg::makeSndBuff()
{
QByteArray uIdent = _msgIdent.toUtf8();
QByteArray uString = _msgString.toUtf8();
uIdent.truncate(255);
uString.truncate(255);
_buffLen = 4 + uIdent.size() + uString.size();
if (_buffer != NULL)
delete[] _buffer;
unsigned char *tmpBuffer = new unsigned char[_buffLen + 1];
tmpBuffer[0] = PVS_ID;
tmpBuffer[1] = (unsigned char) _msgType;
tmpBuffer[2] = (unsigned char) uIdent.size();
tmpBuffer[3] = (unsigned char) uString.size();
memcpy(tmpBuffer + 4, uIdent.data(), uIdent.size());
memcpy(tmpBuffer + 4 + uIdent.size(), uString.data(), uString.size());
//printf("[%c] '%s' - '%s'\n", (char)_msgType, _msgIdent.toUtf8().data(), _msgString.toUtf8().data());
tmpBuffer[_buffLen] = '\0';
//printf("Binary: '%s'\n", _buffer);
_buffer = (char*)tmpBuffer;
return true;
}
|