diff options
Diffstat (limited to 'pacman-c++/util.cpp')
| -rw-r--r-- | pacman-c++/util.cpp | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp index 6b9780b..ceba2b8 100644 --- a/pacman-c++/util.cpp +++ b/pacman-c++/util.cpp | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | #include "util.h" | 1 | #include "util.h" |
| 2 | 2 | ||
| 3 | #include <QtNetwork/QTcpSocket> | ||
| 4 | |||
| 3 | namespace Util { | 5 | namespace Util { |
| 4 | 6 | ||
| 5 | Transmission::map_t createUninitialisedMap() { | 7 | Transmission::map_t createUninitialisedMap() { |
| @@ -20,7 +22,7 @@ namespace Util { | |||
| 20 | cur = Transmission::none; | 22 | cur = Transmission::none; |
| 21 | } | 23 | } |
| 22 | } | 24 | } |
| 23 | return map; | 25 | return map; |
| 24 | } | 26 | } |
| 25 | // temporary | 27 | // temporary |
| 26 | Transmission::map_t createDummyMap() | 28 | Transmission::map_t createDummyMap() |
| @@ -138,4 +140,31 @@ namespace Util { | |||
| 138 | } | 140 | } |
| 139 | return Actor::None; // for pleasing the compiler | 141 | return Actor::None; // for pleasing the compiler |
| 140 | } | 142 | } |
| 143 | |||
| 144 | void QByteArrayToStdString(const QByteArray& arr, std::string& str) { | ||
| 145 | // TODO: normal conversion to std::string won't work, | ||
| 146 | // probably due to \0-bytes. | ||
| 147 | //std::string dataStr = std::string(data.constData()); | ||
| 148 | //std::string dataStr = QString(data).toStdString(); | ||
| 149 | for (int i=0; i<arr.size(); ++i) { | ||
| 150 | str += arr[i]; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | void sendPacket(const ::google::protobuf::Message& packet, QTcpSocket *socket) { | ||
| 155 | std::string dataStr = packet.SerializeAsString(); | ||
| 156 | const char *data = dataStr.c_str(); | ||
| 157 | sendPacket(data, dataStr.length(), socket); | ||
| 158 | } | ||
| 159 | |||
| 160 | void sendPacket(const char *data, unsigned int length, QTcpSocket *socket) { | ||
| 161 | unsigned int bytesWritten = socket->write(data, length); | ||
| 162 | if (bytesWritten != length) { | ||
| 163 | qDebug() << "written: " << bytesWritten; | ||
| 164 | qDebug() << "strl: " << length; | ||
| 165 | } | ||
| 166 | Q_ASSERT(bytesWritten == length); | ||
| 167 | socket->flush(); | ||
| 168 | } | ||
| 169 | |||
| 141 | } | 170 | } |
