void u3HClient::DeserializeData(QByteArray & arrayRef) { char * dataPtr = arrayRef.data(); PACKET *packet = new PACKET(); memcpy(packet, dataPtr, PACKET_SIZE_WDATA); if(packet->dwDataSize > 0) { packet->data = static_cast<unsigned char *>(calloc(sizeof(unsigned char), packet->dwDataSize)); switch (packet->dataType) { case _string: { dataPtr[PACKET_SIZE_WDATA + packet->dwDataSize ] = '\0'; strcpy((char*)packet->data, dataPtr + PACKET_SIZE_WDATA); break; } case _bytes: { memcpy(packet->data, dataPtr + PACKET_SIZE_WDATA, packet->dwDataSize); break; } } } if (packet->_header.Command == _hello) { emit HelloClient(this, packet); } else { emit PacketReady(packet); } }
void u3HClient::on_clientSocket_DataReady() { if (!m_clientSocket->isReadable()) { return; } PACKET *packet = new PACKET(); QByteArray packetRaw = m_clientSocket->read(PACKET_SIZE_WDATA); if (packetRaw.size() < PACKET_SIZE_WDATA) { } memcpy(packet, packetRaw.data(), PACKET_SIZE_WDATA); QByteArray dataRaw; if (packet->dwDataSize > 0) { packet->data = static_cast<unsigned char *>(calloc(sizeof(unsigned char), packet->dwDataSize)); while (dataRaw.size() < packet->dwDataSize) { dataRaw.append(m_clientSocket->readAll()); if (dataRaw.size() < packet->dwDataSize) m_clientSocket->waitForReadyRead(); } memcpy(packet->data, dataRaw.data(), packet->dwDataSize); } emit updateRetrieveBytesCount(packetRaw.size() + dataRaw.size()); if (packet->_header.Command == _hello) { emit HelloClient(this, packet); } else { emit PacketReady(packet); } }
void ConnectionWidget::readPendingDatagrams() { QByteArray datagram; QHostAddress sender; quint16 senderPort; while(udpSocket->hasPendingDatagrams()) { //READ from Socket datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); } QString text = QString("Recieved Size: "); text.append(QString::number(datagram.size())); statusNetworkLabel->setText(text); emit PacketReady(&datagram); }