summaryrefslogtreecommitdiffstats
path: root/pacman-c++/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pacman-c++/util.cpp')
-rw-r--r--pacman-c++/util.cpp122
1 files changed, 102 insertions, 20 deletions
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 @@
1#include "util.h" 1#include "util.h"
2
3#include <QtNetwork/QTcpSocket> 2#include <QtNetwork/QTcpSocket>
4 3
5namespace Util 4namespace Util
@@ -27,6 +26,13 @@ namespace Util
27 return map; 26 return map;
28 } 27 }
29 28
29 void deleteMap(Transmission::map_t map)
30 {
31 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
32 delete[] map[x];
33 delete[] map;
34 }
35
30 // temporary 36 // temporary
31 Transmission::map_t createDemoMap() 37 Transmission::map_t createDemoMap()
32 { 38 {
@@ -141,32 +147,108 @@ namespace Util
141 return def; 147 return def;
142 } 148 }
143 149
144 void QByteArrayToStdString(const QByteArray& arr, std::string& str) 150 QSharedPointer<QByteArray> createPacket(const ::google::protobuf::MessageLite& packet)
145 { 151 {
146 // TODO: normal conversion to std::string won't work, 152 qint64 packetlen = packet.ByteSize();
147 // probably due to \0-bytes. 153 /* datalen = packet with length prepended */
148 //std::string dataStr = std::string(data.constData()); 154 qint64 datalen = sizeof(qint64) + packetlen;
149 //std::string dataStr = QString(data).toStdString();
150 for (int i=0; i<arr.size(); ++i)
151 str += arr[i];
152 }
153 155
154 void sendPacket(const ::google::protobuf::Message& packet, QTcpSocket *socket) 156 QSharedPointer<QByteArray> data = QSharedPointer<QByteArray>(new QByteArray);
155 { 157 data->resize(datalen);
156 std::string dataStr = packet.SerializeAsString(); 158
157 const char *data = dataStr.c_str(); 159 /* use QDataStream for length to avoid endianess shit */
158 sendPacket(data, dataStr.length(), socket); 160 QDataStream out(data.data(), QIODevice::WriteOnly);
161 out << packetlen;
162
163 /* use protobuf.SerializeWithCachedSizesToArray() to avoid calling protobuf.ByteSize() again */
164 ::google::protobuf::uint8 *dataptr = reinterpret_cast<google::protobuf::uint8 *>(data->data());
165 packet.SerializeWithCachedSizesToArray(dataptr + sizeof(qint64));
166
167 return data;
159 } 168 }
160 169
161 void sendPacket(const char *data, int length, QTcpSocket *socket) 170 bool sendPacket(QByteArray *data, QTcpSocket *socket)
162 { 171 {
163 int bytesWritten = socket->write(data, length); 172 int bytesWritten = socket->write(*data);
164 if (bytesWritten != length) 173 if (bytesWritten != data->size())
165 { 174 {
166 qDebug() << "[sendPacket] Not all data has been sent." 175 qWarning() << "[sendPacket] Not all data has been sent:"
167 << "written=" << bytesWritten << ", length=" << length; 176 << "written=" << bytesWritten << ", length=" << data->size();
177 return false;
168 } 178 }
169 Q_ASSERT(bytesWritten == length);
170 socket->flush(); 179 socket->flush();
180 return true;
181 }
182
183 bool sendPacket(const ::google::protobuf::MessageLite& packet, QTcpSocket *socket)
184 {
185 return sendPacket(createPacket(packet).data(), socket);
186 }
187
188 QSharedPointer<QByteArray> receivePacket(QTcpSocket *socket)
189 {
190 QDataStream in(socket);
191 qint64 datalen;
192 in >> datalen;
193
194 QSharedPointer<QByteArray> data = QSharedPointer<QByteArray>(new QByteArray);
195 data->resize(datalen);
196 socket->read(data->data(), data->size());
197 return data;
198 }
199
200#if 0
201 void hexdump(void *pAddressIn, long lSize)
202 {
203 char szBuf[100];
204 long lIndent = 1;
205 long lOutLen, lIndex, lIndex2, lOutLen2;
206 long lRelPos;
207 struct { char *pData; unsigned long lSize; } buf;
208 unsigned char *pTmp,ucTmp;
209 unsigned char *pAddress = (unsigned char *)pAddressIn;
210
211 buf.pData = (char *)pAddress;
212 buf.lSize = lSize;
213
214 while (buf.lSize > 0)
215 {
216 pTmp = (unsigned char *)buf.pData;
217 lOutLen = (int)buf.lSize;
218 if (lOutLen > 16)
219 lOutLen = 16;
220
221 // create a 64-character formatted output line:
222 sprintf(szBuf, " > "
223 " "
224 " %08lX", pTmp-pAddress);
225 lOutLen2 = lOutLen;
226
227 for(lIndex = 1+lIndent, lIndex2 = 53-15+lIndent, lRelPos = 0;
228 lOutLen2;
229 lOutLen2--, lIndex += 2, lIndex2++
230 )
231 {
232 ucTmp = *pTmp++;
233
234 sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
235 if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
236 szBuf[lIndex2] = ucTmp;
237
238 if (!(++lRelPos & 3)) // extra blank after 4 bytes
239 { lIndex++; szBuf[lIndex+2] = ' '; }
240 }
241
242 if (!(lRelPos & 3)) lIndex--;
243
244 szBuf[lIndex ] = '<';
245 szBuf[lIndex+1] = ' ';
246
247 printf("%s\n", szBuf);
248
249 buf.pData += lOutLen;
250 buf.lSize -= lOutLen;
251 }
171 } 252 }
253#endif
172} 254}