summaryrefslogtreecommitdiffstats
path: root/src/shared/networkmessage.h
diff options
context:
space:
mode:
authorsr2013-02-04 19:50:31 +0100
committersr2013-02-04 19:50:31 +0100
commit1a5709501f94014d41987b956338bb6424b9f90c (patch)
treed3b93fe8dc406bca56aff147ef5cc4cbf9ed6be0 /src/shared/networkmessage.h
parentTest (diff)
downloadpvs2-1a5709501f94014d41987b956338bb6424b9f90c.tar.gz
pvs2-1a5709501f94014d41987b956338bb6424b9f90c.tar.xz
pvs2-1a5709501f94014d41987b956338bb6424b9f90c.zip
Initial commit
Diffstat (limited to 'src/shared/networkmessage.h')
-rw-r--r--src/shared/networkmessage.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/shared/networkmessage.h b/src/shared/networkmessage.h
new file mode 100644
index 0000000..5a8b2c2
--- /dev/null
+++ b/src/shared/networkmessage.h
@@ -0,0 +1,69 @@
+/*
+ * NetworkMessage.h
+ *
+ * Created on: 18.01.2013
+ * Author: sr
+ */
+
+#ifndef NETWORKMESSAGE_H_
+#define NETWORKMESSAGE_H_
+
+#include <QtCore>
+
+class QAbstractSocket;
+class QUdpSocket;
+
+// define qstrings for message ids. this prevents implicit instanciation of the same qstrings over and over again
+#define MSGTYPE(name) static const QString _ ## name ( #name )
+MSGTYPE(ID);
+MSGTYPE(IMG);
+MSGTYPE(LOGIN);
+MSGTYPE(THUMB);
+MSGTYPE(X);
+MSGTYPE(Y);
+MSGTYPE(VNCSERVER);
+MSGTYPE(VNCCLIENT);
+MSGTYPE(LOCK);
+MSGTYPE(HASH);
+MSGTYPE(SALT1);
+MSGTYPE(SALT2);
+MSGTYPE(IPLIST);
+MSGTYPE(PORT);
+MSGTYPE(CERT);
+MSGTYPE(CHALLENGE);
+MSGTYPE(ERROR);
+
+class NetworkMessage
+{
+private:
+ char *_buffer;
+ quint32 _bufferSize, _bufferPos, _lastBufferSize;
+ QHash<QString, QByteArray> _fields;
+ int _mode; // 0 = none, 1 = reading, 2 = writing, 3 = read complete, 4 = write complete
+
+ void allocBuffer();
+ bool parseHeader(char *header);
+ bool parseMessage(char *buffer);
+ void serializeMessage();
+
+public:
+ NetworkMessage();
+ virtual ~NetworkMessage();
+ bool readMessage(QAbstractSocket* socket);
+ bool readMessage(char* data, quint32 len);
+ bool writeMessage(QAbstractSocket* socket);
+ bool writeMessage(QUdpSocket* socket, const QHostAddress& address, quint16 port);
+ void reset() { _fields.clear(); _bufferSize = 0; _mode = 0; }
+ const bool readComplete() const { return _mode == 3; }
+ const bool writeComplete() const { return _mode == 4; }
+ const bool hasField(const QString& key) const { return _fields.contains(key); }
+ const QString getFieldString(const QString& key) const { return QString::fromUtf8(_fields.value(key)); }
+ const QByteArray getFieldBytes(const QString& key) const { return _fields.value(key); }
+ void setField(const QString& key, const QByteArray& value) { if (_mode == 1 || _mode == 2) qFatal("setField called in bad state."); _fields.insert(key, value); _mode = 0; }
+ void setField(const QString& key, const QString& value) { setField(key, value.toUtf8()); }
+ // Convenience
+ void buildErrorMessage(const QString& error);
+ void buildErrorMessage(const char* error);
+};
+
+#endif /* NETWORKMESSAGE_H_ */