Hello Chris and thanks again for the reply!
So let me see if I understood your now corrected explanation with a example (particularly the situation I want now to apply all this).
I have a socket connection from which data arrives into a char[]-buffer. I want to construct a temporary QByteArray that will never be modified (constant), only to manipulate these data as a QByteArray instead of a char[]. For this, I want to do a shallow copy, so I may use both options
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
const QByteArray bytesReceived
= QByteArray::fromRawData(bytesReceivedTemp,SOCKET_BUFFER_SIZE
));
//Option 1
const QByteArray bytesReceived
(bytesReceivedTemp,SOCKET_BUFFER_SIZE
);
//Option 2
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
const QByteArray bytesReceived = QByteArray::fromRawData(bytesReceivedTemp,SOCKET_BUFFER_SIZE)); //Option 1
const QByteArray bytesReceived(bytesReceivedTemp,SOCKET_BUFFER_SIZE); //Option 2
To copy to clipboard, switch view to plain text mode
, for both o them use QByteArray's implicit sharing of data. For this, I simply can't use append(), for in doing it I'll necessarily make a deep copy (what still seems to go against Qt Assistant's append() instructions, but that's what I understood of your explanations
(...)option 2 copies immediately(...)
(...)copies immediately the other does not(...) //which means, it copies later with append()
(...) and copies the specified bytes onto the end of the buffer. // For bytesReceived.append(bytesReceivedTemp,SOCKET_BUFF ER_SIZE)
). But what may happen is that the data package received in the socket system may come in fractions, so I'll need to store the already received data in a different QByteArray (now a deep copy) and wait for the new data pack from the socket. When it arrives, I should create a new QByteArray with a shallow copy to the first part of the pack plus a shallow copy of the new data recently received. The resulting template code:
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
memset(&bytesReceivedTemp,'\0',SOCKET_BUFFER_SIZE);
while(true)
{
socket_read(bytesReceivedTemp). //simplified
const QByteArray bytesReceived
(previousData
+ QByteArray::fromRawData(bytesReceivedTemp,SOCKET_BUFFER_SIZE
));
//bytesReceived with two shallow copies
switch(bytesReceived.at(0))
{
case PACK_HEADER_1: //identifies the pack
{
if (bytesReceived.size() < PACK_HEADER_1_SIZE) //verify if pack is fragmented
{
previousData = bytesReceived.mid(0,bytesReceived.size()); //Performs a deep copy of the current fraction of the pack
continue;
}
//Process...
}
//...
}
}
QByteArray previousData;
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
memset(&bytesReceivedTemp,'\0',SOCKET_BUFFER_SIZE);
while(true)
{
socket_read(bytesReceivedTemp). //simplified
const QByteArray bytesReceived(previousData + QByteArray::fromRawData(bytesReceivedTemp,SOCKET_BUFFER_SIZE)); //bytesReceived with two shallow copies
switch(bytesReceived.at(0))
{
case PACK_HEADER_1: //identifies the pack
{
if (bytesReceived.size() < PACK_HEADER_1_SIZE) //verify if pack is fragmented
{
previousData = bytesReceived.mid(0,bytesReceived.size()); //Performs a deep copy of the current fraction of the pack
continue;
}
//Process...
}
//...
}
}
To copy to clipboard, switch view to plain text mode
Note: obviously the algorithm is simplified.
Thanks,
Momergil
Bookmarks