Qdatastream overloaded function from a readready slot
-
I'm a little bit confused in how to use the Qdatastream operators or read and write data to a socket.
struct Science { int abby , int becky }; QDataStream &operator<<(QDataStream &stream, const Science &s) { stream << s.abby << s.becky; return stream; } QDataStream &operator<<(QDataStream &stream, Science &s) { stream >> s.abby >> s.becky; return stream; }how do i call these operators to fill in the data once i call my qdatastream from readready slot?
Anyone have a good full example of ready data from the socket then de-serialize it to a operator then serialize it back to the write socket.
-
void ServerThread::readyRead() { QDataStream in(socket); Science variable; in.startTransaction(); in >> variable; if(in.commitTransaction()) { // All data necessary was received } // proccess data here }What do i do about padding? how can i make it dynamic to de-serialize base on whatever structure was received.
-
@Sunfluxgames said in Qdatastream overloaded function from a readready slot:
What do i do about padding?
What do you mean with this? QDataStream is writing the data in it's own format so why do you want to pad something?
how can i make it dynamic to de-serialize base on whatever structure was received.
You need to distinguish between the different structures by e.g. a type field.
-
The receiving stream comes in a padding form. The client expects it to be padded.
Other than align the structure to be padded i guess?
-
The receiving stream comes in a padding form. The client expects it to be padded.
Other than align the structure to be padded i guess?
@Sunfluxgames
If @Christian-Ehrlicher is correct when he saysQDataStream is writing the data in it's own format
and you seem to want to read from/write to an external non-Qt source (right?) with its own "padding"/format, then I would not think
QDataStreamcan be used? It sounds more like you need to do your own char-by-char reading/writing of thisScienceclass? Similarly, how are yourints to be passed between client & server? -
Yes it is a non-QT source. My original source does a custom read/write without QDataStreams. I was just trying to see if I could use QDataStreams instead.
Only thing I can think of is once it enters the QDataStream is to strip the padding and use it like a normal QDataStream.
Build the reading Structure to fit the need of whatever type field I choose. Build the writing Structure to fit the needed size of whatever the non-QT source wants.
Something like this possible maybe?
typedef struct __atribute__((packed)) Science { quint32 abby; quint32 becky; } Science; -
As @JonB already told you you can't use QDataStream for your task and have to write the decoding by your own.
-
Yes it is a non-QT source. My original source does a custom read/write without QDataStreams. I was just trying to see if I could use QDataStreams instead.
Only thing I can think of is once it enters the QDataStream is to strip the padding and use it like a normal QDataStream.
Build the reading Structure to fit the need of whatever type field I choose. Build the writing Structure to fit the needed size of whatever the non-QT source wants.
Something like this possible maybe?
typedef struct __atribute__((packed)) Science { quint32 abby; quint32 becky; } Science;@Sunfluxgames said in Qdatastream overloaded function from a readready slot:
Something like this possible maybe?
typedef struct atribute((packed)) Science {
quint32 abby;
quint32 becky;
} Science;Just have a try !
As an old C programmer, i like to fiddle around with bytes :)
Let's go:struct __attribute__((packed)) Science { quint8 a; quint16 b; quint8 c; }; QByteArray arr=QByteArray::fromHex("01 01 04 0a"); QDataStream stream(arr); qint16 b; qint8 a,c; stream>>a>>b>>c;in the degugger i see:
arr "\001\001\004\n" QByteArray
a 1 qint8
b 260 qint16
c 10 qint8So far so good.
and now with the struct;
QDataStream &operator>>(QDataStream &stream, Science &s) { stream >> s.a >> s.b>>s.c; return stream; } QByteArray arr=QByteArray::fromHex("01 01 04 0a"); QDataStream stream(arr); Science sc; stream>>sc;arr "\001\001\004\n" QByteArray
sc @0x7fff5fbffb48 Science
a 1 quint8
b 260 quint16
c 10 quint8Well, everything is ok.
The main question is that 100% sure/reliable ?
That's your own choice ;)
(note: the values are decoded in big endian)