int& operator<<(int& sock, Ard& ard)
{
//Set socket fd to blocking mode (Sockets from Qt API are non-blocking by default)
int i = fcntl(sock, F_GETFL);
i = ( i & ~O_NONBLOCK );
fcntl ( sock, F_SETFL,i );
ard.updateSizes(); // corrects comment length and offset as well as file size variables
unsigned int writeTotal = 0;
char header[Ard::HEADER_SIZE];
int bytesWritten;
//serialise header
memcpy(header, ard.m_fileType, 4);
memcpy(header + 4, &ard.m_ardType, 1);
memcpy(header + 5, &ard.m_zTime, 8);
memcpy(header + 13, &ard.m_fc, 4);
memcpy(header + 17, &ard.m_fs, 4);
memcpy(header + 21, &ard.m_bw, 4);
memcpy(header + 25, &ard.m_minAmp, 4);
memcpy(header + 29, &ard.m_maxAmp, 4);
memcpy(header + 33, &ard.m_rangeRes, 4);
memcpy(header + 37, &ard.m_dopplerRes, 4);
memcpy(header + 41, &ard.m_xDim, 4);
memcpy(header + 45, &ard.m_yDim, 4);
memcpy(header + 49, &ard.m_txRxDist, 4);
memcpy(header + 53, &ard.m_cOffset, 8);
memcpy(header + 61, &ard.m_cLength, 4);
memcpy(header + 65, &ard.m_fileSize, 8);
char *ptr = header;
int bytesLeft = Ard::HEADER_SIZE;
while (bytesLeft)
{
bytesWritten = send(sock, ptr, bytesLeft, 0);
ptr += bytesWritten;
writeTotal += bytesWritten;
bytesLeft -= bytesWritten;
if(bytesWritten == -1)
cout << "Ard::operator<<(): Error: can't write to socket' " << endl;
}
cout << "Ard::operator<<(): After header writeTotal = " << writeTotal << endl;
for(unsigned int x = 0; x < ard.m_xDim; x++)
{
bytesLeft = ard.m_yDim * sizeof(float);
ptr = (char*)ard.m_data[x];
while(bytesLeft)
{
bytesWritten = send (sock, ptr, bytesLeft, 0);
bytesLeft -= bytesWritten;
writeTotal += bytesWritten;
ptr += bytesWritten;
if(bytesWritten == -1)
cout << "Ard::operator<<(): Error: can't write to socket' " << endl;
}
}
cout << "Ard::operator<<(): After data block writeTotal = " << writeTotal << endl;
bytesLeft = ard.m_cLength;
char comment[ard.m_cLength];
strncpy (comment, ard.m_comment.c_str(), ard.m_cLength);
ptr = comment;
while(bytesLeft)
{
bytesWritten = send (sock, ptr, bytesLeft, 0);
bytesLeft -= bytesWritten;
writeTotal += bytesWritten;
ptr += bytesWritten;
if(bytesWritten == -1)
cout << "Ard::operator<<(): Error: can't write to socket' " << endl;
}
cout << "Ard::operator<<(): At end of ARD writing writeTotal = " << writeTotal << endl;
return sock;
}
int& operator<<(int& sock, Ard& ard)
{
//Set socket fd to blocking mode (Sockets from Qt API are non-blocking by default)
int i = fcntl(sock, F_GETFL);
i = ( i & ~O_NONBLOCK );
fcntl ( sock, F_SETFL,i );
ard.updateSizes(); // corrects comment length and offset as well as file size variables
unsigned int writeTotal = 0;
char header[Ard::HEADER_SIZE];
int bytesWritten;
//serialise header
memcpy(header, ard.m_fileType, 4);
memcpy(header + 4, &ard.m_ardType, 1);
memcpy(header + 5, &ard.m_zTime, 8);
memcpy(header + 13, &ard.m_fc, 4);
memcpy(header + 17, &ard.m_fs, 4);
memcpy(header + 21, &ard.m_bw, 4);
memcpy(header + 25, &ard.m_minAmp, 4);
memcpy(header + 29, &ard.m_maxAmp, 4);
memcpy(header + 33, &ard.m_rangeRes, 4);
memcpy(header + 37, &ard.m_dopplerRes, 4);
memcpy(header + 41, &ard.m_xDim, 4);
memcpy(header + 45, &ard.m_yDim, 4);
memcpy(header + 49, &ard.m_txRxDist, 4);
memcpy(header + 53, &ard.m_cOffset, 8);
memcpy(header + 61, &ard.m_cLength, 4);
memcpy(header + 65, &ard.m_fileSize, 8);
char *ptr = header;
int bytesLeft = Ard::HEADER_SIZE;
while (bytesLeft)
{
bytesWritten = send(sock, ptr, bytesLeft, 0);
ptr += bytesWritten;
writeTotal += bytesWritten;
bytesLeft -= bytesWritten;
if(bytesWritten == -1)
cout << "Ard::operator<<(): Error: can't write to socket' " << endl;
}
cout << "Ard::operator<<(): After header writeTotal = " << writeTotal << endl;
for(unsigned int x = 0; x < ard.m_xDim; x++)
{
bytesLeft = ard.m_yDim * sizeof(float);
ptr = (char*)ard.m_data[x];
while(bytesLeft)
{
bytesWritten = send (sock, ptr, bytesLeft, 0);
bytesLeft -= bytesWritten;
writeTotal += bytesWritten;
ptr += bytesWritten;
if(bytesWritten == -1)
cout << "Ard::operator<<(): Error: can't write to socket' " << endl;
}
}
cout << "Ard::operator<<(): After data block writeTotal = " << writeTotal << endl;
bytesLeft = ard.m_cLength;
char comment[ard.m_cLength];
strncpy (comment, ard.m_comment.c_str(), ard.m_cLength);
ptr = comment;
while(bytesLeft)
{
bytesWritten = send (sock, ptr, bytesLeft, 0);
bytesLeft -= bytesWritten;
writeTotal += bytesWritten;
ptr += bytesWritten;
if(bytesWritten == -1)
cout << "Ard::operator<<(): Error: can't write to socket' " << endl;
}
cout << "Ard::operator<<(): At end of ARD writing writeTotal = " << writeTotal << endl;
return sock;
}
To copy to clipboard, switch view to plain text mode
void QArd::readData(QTcpSocket& sock)
{
int bytesRead;
int bytesLeft;
char* ptr;
if(!m_headerRead)
{
if(sock.bytesAvailable() < HEADER_SIZE) //wait for all header data to be available
{
qDebug() << "----socket receving function returning at header";
return;
}
bytesLeft = HEADER_SIZE;
char header[HEADER_SIZE];
ptr = header;
while (bytesLeft)
{
bytesRead = sock.read(ptr, bytesLeft);
ptr += bytesRead;
m_readTotal += bytesRead;
bytesLeft -= bytesRead;
}
//deserialise header
memcpy(m_fileType, header, 4);
memcpy(&m_ardType, header + 4, 1);
memcpy(&m_zTime, header + 5, 8);
memcpy(&m_fc, header + 13, 4);
memcpy(&m_fs, header + 17, 4);
memcpy(&m_bw, header + 21, 4);
memcpy(&m_minAmp, header + 25, 4);
memcpy(&m_maxAmp, header + 29, 4);
memcpy(&m_rangeRes, header + 33, 4);
memcpy(&m_dopplerRes, header + 37, 4);
memcpy(&m_xDim, header + 41, 4);
memcpy(&m_yDim, header + 45, 4);
memcpy(&m_txRxDist, header + 49, 4);
memcpy(&m_cOffset, header + 53, 8);
memcpy(&m_cLength, header + 61, 4);
memcpy(&m_fileSize, header + 65, 8);
qDebug() << getInfoString().c_str();
qDebug() << "QArd:: read After header readTotal = " << m_readTotal;
allocateMatrix();
m_headerRead = true;
}
while(m_rangeBin < m_xDim)
{
if((unsigned int)sock.bytesAvailable() < sizeof(float) * m_yDim)
{
qDebug() << "----socket receving function returning at data block";
return;
}
bytesLeft = m_yDim * sizeof(float);
ptr = (char*)m_data[m_rangeBin];
while(bytesLeft)
{
bytesRead = sock.read(ptr, bytesLeft);
m_readTotal += bytesRead;
ptr += bytesRead;
bytesLeft -= bytesRead;
}
m_rangeBin++;
}
qDebug() << "QArd:: readData() After data block readTotal = " << m_readTotal;
if(sock.bytesAvailable() < m_cLength)
{
qDebug() << "----socket receving function returning at comment";
return;
}
char comment[m_cLength];
ptr = comment;
bytesLeft = m_cLength;
while(bytesLeft)
{
bytesRead = sock.read(ptr, bytesLeft);
m_readTotal += bytesRead;
ptr += bytesRead;
bytesLeft -= bytesRead;
}
qDebug() << "COMMENT = " << comment;
m_comment = comment;
qDebug() << m_comment.c_str();
qDebug() << "QArd:: readData() After ARD read finished readTotal = " << m_readTotal;
//reset parameters to start reading new plot:
m_rangeBin = 0;
m_headerRead = false;
m_readTotal = 0;
//send signal to say reading is complete
readingComplete();
}
void QArd::readData(QTcpSocket& sock)
{
int bytesRead;
int bytesLeft;
char* ptr;
if(!m_headerRead)
{
if(sock.bytesAvailable() < HEADER_SIZE) //wait for all header data to be available
{
qDebug() << "----socket receving function returning at header";
return;
}
bytesLeft = HEADER_SIZE;
char header[HEADER_SIZE];
ptr = header;
while (bytesLeft)
{
bytesRead = sock.read(ptr, bytesLeft);
ptr += bytesRead;
m_readTotal += bytesRead;
bytesLeft -= bytesRead;
}
//deserialise header
memcpy(m_fileType, header, 4);
memcpy(&m_ardType, header + 4, 1);
memcpy(&m_zTime, header + 5, 8);
memcpy(&m_fc, header + 13, 4);
memcpy(&m_fs, header + 17, 4);
memcpy(&m_bw, header + 21, 4);
memcpy(&m_minAmp, header + 25, 4);
memcpy(&m_maxAmp, header + 29, 4);
memcpy(&m_rangeRes, header + 33, 4);
memcpy(&m_dopplerRes, header + 37, 4);
memcpy(&m_xDim, header + 41, 4);
memcpy(&m_yDim, header + 45, 4);
memcpy(&m_txRxDist, header + 49, 4);
memcpy(&m_cOffset, header + 53, 8);
memcpy(&m_cLength, header + 61, 4);
memcpy(&m_fileSize, header + 65, 8);
qDebug() << getInfoString().c_str();
qDebug() << "QArd:: read After header readTotal = " << m_readTotal;
allocateMatrix();
m_headerRead = true;
}
while(m_rangeBin < m_xDim)
{
if((unsigned int)sock.bytesAvailable() < sizeof(float) * m_yDim)
{
qDebug() << "----socket receving function returning at data block";
return;
}
bytesLeft = m_yDim * sizeof(float);
ptr = (char*)m_data[m_rangeBin];
while(bytesLeft)
{
bytesRead = sock.read(ptr, bytesLeft);
m_readTotal += bytesRead;
ptr += bytesRead;
bytesLeft -= bytesRead;
}
m_rangeBin++;
}
qDebug() << "QArd:: readData() After data block readTotal = " << m_readTotal;
if(sock.bytesAvailable() < m_cLength)
{
qDebug() << "----socket receving function returning at comment";
return;
}
char comment[m_cLength];
ptr = comment;
bytesLeft = m_cLength;
while(bytesLeft)
{
bytesRead = sock.read(ptr, bytesLeft);
m_readTotal += bytesRead;
ptr += bytesRead;
bytesLeft -= bytesRead;
}
qDebug() << "COMMENT = " << comment;
m_comment = comment;
qDebug() << m_comment.c_str();
qDebug() << "QArd:: readData() After ARD read finished readTotal = " << m_readTotal;
//reset parameters to start reading new plot:
m_rangeBin = 0;
m_headerRead = false;
m_readTotal = 0;
//send signal to say reading is complete
readingComplete();
}
To copy to clipboard, switch view to plain text mode
Bookmarks