From 2c351a8bccdfe0fe9ad0ccb4dba5e15ef23b4c0c Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 13 Apr 2011 17:24:38 +0200 Subject: - rewrite network methods (ugly but good performance) - fix memleaks --- pacman-c++/util.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 102 insertions(+), 20 deletions(-) (limited to 'pacman-c++/util.cpp') diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp index c60f2df..2fb9f69 100644 --- a/pacman-c++/util.cpp +++ b/pacman-c++/util.cpp @@ -1,5 +1,4 @@ #include "util.h" - #include namespace Util @@ -27,6 +26,13 @@ namespace Util return map; } + void deleteMap(Transmission::map_t map) + { + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + delete[] map[x]; + delete[] map; + } + // temporary Transmission::map_t createDemoMap() { @@ -141,32 +147,108 @@ namespace Util return def; } - void QByteArrayToStdString(const QByteArray& arr, std::string& str) + QSharedPointer createPacket(const ::google::protobuf::MessageLite& packet) { - // TODO: normal conversion to std::string won't work, - // probably due to \0-bytes. - //std::string dataStr = std::string(data.constData()); - //std::string dataStr = QString(data).toStdString(); - for (int i=0; i data = QSharedPointer(new QByteArray); + data->resize(datalen); + + /* use QDataStream for length to avoid endianess shit */ + QDataStream out(data.data(), QIODevice::WriteOnly); + out << packetlen; + + /* use protobuf.SerializeWithCachedSizesToArray() to avoid calling protobuf.ByteSize() again */ + ::google::protobuf::uint8 *dataptr = reinterpret_cast(data->data()); + packet.SerializeWithCachedSizesToArray(dataptr + sizeof(qint64)); + + return data; } - void sendPacket(const char *data, int length, QTcpSocket *socket) + bool sendPacket(QByteArray *data, QTcpSocket *socket) { - int bytesWritten = socket->write(data, length); - if (bytesWritten != length) + int bytesWritten = socket->write(*data); + if (bytesWritten != data->size()) { - qDebug() << "[sendPacket] Not all data has been sent." - << "written=" << bytesWritten << ", length=" << length; + qWarning() << "[sendPacket] Not all data has been sent:" + << "written=" << bytesWritten << ", length=" << data->size(); + return false; } - Q_ASSERT(bytesWritten == length); socket->flush(); + return true; + } + + bool sendPacket(const ::google::protobuf::MessageLite& packet, QTcpSocket *socket) + { + return sendPacket(createPacket(packet).data(), socket); + } + + QSharedPointer receivePacket(QTcpSocket *socket) + { + QDataStream in(socket); + qint64 datalen; + in >> datalen; + + QSharedPointer data = QSharedPointer(new QByteArray); + data->resize(datalen); + socket->read(data->data(), data->size()); + return data; + } + +#if 0 + void hexdump(void *pAddressIn, long lSize) + { + char szBuf[100]; + long lIndent = 1; + long lOutLen, lIndex, lIndex2, lOutLen2; + long lRelPos; + struct { char *pData; unsigned long lSize; } buf; + unsigned char *pTmp,ucTmp; + unsigned char *pAddress = (unsigned char *)pAddressIn; + + buf.pData = (char *)pAddress; + buf.lSize = lSize; + + while (buf.lSize > 0) + { + pTmp = (unsigned char *)buf.pData; + lOutLen = (int)buf.lSize; + if (lOutLen > 16) + lOutLen = 16; + + // create a 64-character formatted output line: + sprintf(szBuf, " > " + " " + " %08lX", pTmp-pAddress); + lOutLen2 = lOutLen; + + for(lIndex = 1+lIndent, lIndex2 = 53-15+lIndent, lRelPos = 0; + lOutLen2; + lOutLen2--, lIndex += 2, lIndex2++ + ) + { + ucTmp = *pTmp++; + + sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp); + if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char + szBuf[lIndex2] = ucTmp; + + if (!(++lRelPos & 3)) // extra blank after 4 bytes + { lIndex++; szBuf[lIndex+2] = ' '; } + } + + if (!(lRelPos & 3)) lIndex--; + + szBuf[lIndex ] = '<'; + szBuf[lIndex+1] = ' '; + + printf("%s\n", szBuf); + + buf.pData += lOutLen; + buf.lSize -= lOutLen; + } } +#endif } -- cgit v1.2.3