diff options
author | Simon Rettberg | 2018-07-24 13:08:25 +0200 |
---|---|---|
committer | Simon Rettberg | 2018-07-24 13:08:25 +0200 |
commit | 9ba63d1460db41c219b638212b42476164fcfdff (patch) | |
tree | 55e3249c18c50ec8e90278e639732988845c990c /src/shared | |
parent | [server] Fix logic error (diff) | |
download | pvs2-9ba63d1460db41c219b638212b42476164fcfdff.tar.gz pvs2-9ba63d1460db41c219b638212b42476164fcfdff.tar.xz pvs2-9ba63d1460db41c219b638212b42476164fcfdff.zip |
Update code style, fix compiler warnings
- Use nullptr instead of NULL for better warnings in case of mistakes
- Get rid of VLAs which are not in C++11 actually
- Fix implicit signed <-> unsigned mismatches by adding checks and casts
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/networkmessage.cpp | 38 | ||||
-rw-r--r-- | src/shared/util.h | 2 |
2 files changed, 20 insertions, 20 deletions
diff --git a/src/shared/networkmessage.cpp b/src/shared/networkmessage.cpp index 64e02e7..0bdf3c5 100644 --- a/src/shared/networkmessage.cpp +++ b/src/shared/networkmessage.cpp @@ -22,32 +22,32 @@ ((((x) & 0xFF00u) >> 8) | \ (((x) & 0x00FFu) << 8)) -static quint16 _htons(quint16 x) +static quint16 _htons(const quint16 x) { if (QSysInfo::ByteOrder == QSysInfo::BigEndian) return x; return (quint16)BYTE_SWAP2(x); } -static quint16 _ntohs(quint16 x) +static quint32 _htonl(const quint32 x) { if (QSysInfo::ByteOrder == QSysInfo::BigEndian) return x; - return (quint16)BYTE_SWAP2(x); + return (quint32)BYTE_SWAP4(x); } -static quint32 _htonl(quint32 x) +static quint16 _ntohs(const char *in) { - if (QSysInfo::ByteOrder == QSysInfo::BigEndian) - return x; - return (quint32)BYTE_SWAP4(x); + return quint16(quint8(in[0]) << 8 + | quint8(in[1])); } -static quint32 _ntohl(quint32 x) +static quint32 _ntohl(const char *in) { - if (QSysInfo::ByteOrder == QSysInfo::BigEndian) - return x; - return (quint32)BYTE_SWAP4(x); + return quint32(quint8(in[0])) << 24 + | quint32(quint8(in[1])) << 16 + | quint32(quint8(in[2])) << 8 + | quint32(quint8(in[3])); } /* @@ -56,7 +56,7 @@ static quint32 _ntohl(quint32 x) */ NetworkMessage::NetworkMessage() : - _buffer(NULL), _bufferSize(0), _bufferPos(0), _lastBufferSize(0), _mode(0) + _buffer(nullptr), _bufferSize(0), _bufferPos(0), _lastBufferSize(0), _mode(0) { // } @@ -69,7 +69,7 @@ NetworkMessage::~NetworkMessage() inline void NetworkMessage::allocBuffer() { - if (_lastBufferSize < _bufferSize || _buffer == NULL) { + if (_lastBufferSize < _bufferSize || _buffer == nullptr) { if (_buffer) delete[] _buffer; _lastBufferSize = _bufferSize; @@ -150,7 +150,7 @@ bool NetworkMessage::parseHeader(char *header) return false; } _bufferPos = 0; - _bufferSize = _ntohl(*(quint32*)(header + 4)); + _bufferSize = _ntohl(header + 4); if (_bufferSize > MAX_MSG_LEN) { qDebug("Disconnecting Client: MAX_MSG_LEN exceeded."); return false; @@ -166,9 +166,9 @@ bool NetworkMessage::parseMessage(char *buffer) { char *ptr = buffer; while (_bufferSize - (ptr - buffer) >= 4) { - const quint16 keyLen = _ntohs(*(quint16*)(ptr)); + const quint16 keyLen = _ntohs(ptr); ptr += 2; - const quint16 valLen = _ntohs(*(quint16*)(ptr)); + const quint16 valLen = _ntohs(ptr); ptr += 2; if (_bufferSize - (ptr - buffer) < keyLen + valLen) { qDebug() << "Warning: Error parsing message. key(" << keyLen << ")+value(" << valLen @@ -237,7 +237,7 @@ void NetworkMessage::serializeMessage() { QByteArray buf; //qDebug() << "Default size: " << buf.capacity(); - buf.reserve(_lastBufferSize > 0 ? _lastBufferSize : 200); + buf.reserve(_lastBufferSize > 0 ? int(_lastBufferSize) : 200); for (QHash<QByteArray, QByteArray>::const_iterator it = _fields.begin(); it != _fields.end(); ++it) { const QByteArray &ba = it.key(); const QByteArray &val = it.value(); @@ -249,9 +249,9 @@ void NetworkMessage::serializeMessage() buf.append(ba); buf.append(val); } - _bufferSize = buf.length() + HEADER_LEN; + _bufferSize = quint32(buf.length() + HEADER_LEN); allocBuffer(); - memcpy(_buffer + HEADER_LEN, buf.data(), buf.size()); + memcpy(_buffer + HEADER_LEN, buf.data(), size_t(buf.size())); _buffer[0] = 'P'; _buffer[1] = 'V'; _buffer[2] = 'S'; diff --git a/src/shared/util.h b/src/shared/util.h index c2e5145..e9530a8 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -10,6 +10,6 @@ #include <QByteArray> -QByteArray genSha1(const QByteArray* a, const QByteArray* b = NULL, const QByteArray* c = NULL, const QByteArray* d = NULL, const QByteArray* e = NULL); +QByteArray genSha1(const QByteArray* a, const QByteArray* b = nullptr, const QByteArray* c = nullptr, const QByteArray* d = nullptr, const QByteArray* e = nullptr); #endif /* UTIL_H_ */ |