size hack for gui ipc (only temporary)

This commit is contained in:
Nick Bolton 2012-07-04 19:36:18 +00:00
parent e0098d5c8f
commit bab317931b
1 changed files with 30 additions and 14 deletions

View File

@ -18,6 +18,7 @@
#include "IpcClient.h" #include "IpcClient.h"
#include <QTcpSocket> #include <QTcpSocket>
#include <QHostAddress> #include <QHostAddress>
#include <iostream>
IpcClient::IpcClient() IpcClient::IpcClient()
{ {
@ -39,23 +40,38 @@ void IpcClient::read()
{ {
QDataStream stream(m_Socket); QDataStream stream(m_Socket);
char codeBuf[1]; while (m_Socket->bytesAvailable() != 0) {
stream.readRawData(codeBuf, 1);
switch (codeBuf[0]) { char codeBuf[1];
case kIpcLogLine: { stream.readRawData(codeBuf, 1);
// TODO: qt must have a built in way of converting bytes to int.
char lenBuf[2];
stream.readRawData(lenBuf, 2);
int len = (lenBuf[0] << 8) + lenBuf[1];
char* data = new char[len]; switch (codeBuf[0]) {
stream.readRawData(data, len); case kIpcLogLine: {
// TODO: qt must have a built in way of converting bytes to int.
QString s = QString::fromUtf8(data, len); char lenBuf[2];
readLogLine(s); stream.readRawData(lenBuf, 2);
int len = (lenBuf[0] << 8) + lenBuf[1];
// HACK: sometimes the size is wrong (probably a bug in the above code)
// but the following text always seems to be valid, so just read a large
// amount from the buffer and put a \0 at the end if the number looks
// valid (ugh, this sucks).
char* data = new char[1024];
stream.readRawData(data, 1024);
if (len > -1) {
data[len] = '\0';
}
QString s = QString::fromUtf8(data);
readLogLine(s);
}
break;
default: {
std::cerr << "invalid code: " << codeBuf[0] << std::endl;
}
break;
} }
break;
} }
} }