bool CLuaBinary::setSint64(const char *pszVal) { int64_t iVal = Q_INIT_NUMBER; m_objStream.clear(); m_objStream.str(""); m_objStream << pszVal; m_objStream >> iVal; iVal = ntohl64(iVal); return setVal((const char *)&iVal, sizeof(iVal)); }
bool CLuaBinary::setUint64(const char *pszVal) { uint64_t uiVal = Q_INIT_NUMBER; m_objStream.clear(); m_objStream.str(""); m_objStream << pszVal; m_objStream >> uiVal; uiVal = ntohl64(uiVal); return setVal((const char *)&uiVal, sizeof(uiVal)); }
const char *CLuaBinary::getUint64(void) { uint64_t uiVal = ntohl64(readNumber<uint64_t>()); m_strVali64.clear(); m_objStream.clear(); m_objStream.str(""); m_objStream << uiVal; m_objStream >> m_strVali64; return m_strVali64.c_str(); }
void DeviceAgent::onDataComing() { START_RECV: if (m_dataPos == 0 && m_readState == READ_STATE_WAIT_PACKAGE_HEADER) { memset(&m_dataHdr, 0 ,sizeof(m_dataHdr)); m_datalen = sizeof(m_dataHdr); delete m_data; m_data = NULL; } if (m_readState == READ_STATE_WAIT_PACKAGE_HEADER) { char *hdrStart = (char*)&m_dataHdr + m_dataPos; int dataReaded = read(hdrStart,m_datalen - m_dataPos); if (dataReaded == 0) { //no data, wait for next read return; } m_dataPos += dataReaded; if (m_dataPos < sizeof(NetPackageHdr)) { return; } if (m_dataPos > sizeof(NetPackageHdr)) { DEBUG_INFO << "Read error!!!!!!!!!!"; onPackageDataError(); return; } m_dataHdr.TAG = ntohl(m_dataHdr.TAG); m_dataHdr.data_len = ntohl64(m_dataHdr.data_len); if (m_dataHdr.TAG == PL_REQ_DOWNLOAD_FILE) { m_readState = READ_STATE_WAIT_FILE_JSON_HDR_LEN; m_dataPos = 0; m_datalen = sizeof(qint64); } else { m_readState = READ_STATE_WAIT_PACKAGE_DATA; m_dataPos = 0; m_datalen = m_dataHdr.data_len; } if (m_datalen > 0) { m_data = new QByteArray(m_datalen,0); } } if (m_readState == READ_STATE_WAIT_PACKAGE_DATA) { if (m_datalen > 0) { int dataReaded = read(m_data->data() + m_dataPos,m_datalen - m_dataPos); if (dataReaded == 0) { return; } m_dataPos += dataReaded; } if (m_dataPos < m_datalen) { return; } if (m_dataPos > m_datalen) { DEBUG_INFO << "Read error!!!!!!!!!!"; DEBUG_INFO << "m_datalen:" <<m_datalen << " m_dataPos:" << m_dataPos; onPackageDataError(); return; } m_readState = READ_STATE_WAIT_PACKAGE_HEADER; m_dataPos = 0; /*Notify controller here,need to block until data is copied out*/ if (m_dataHdr.TAG == PL_REQ_REPORT_ID) { m_devID = m_data->data(); emit deviceConnected(this); } else { emit dataReceived(m_devID,m_dataHdr.TAG,m_data); } } if (m_readState == READ_STATE_WAIT_FILE_JSON_HDR_LEN) { int dataReaded = read(m_data->data() + m_dataPos,m_datalen - m_dataPos); if (dataReaded == 0) { return; } m_dataPos += dataReaded; if (m_dataPos < m_datalen) { return; } if (m_dataPos > m_datalen) { DEBUG_INFO << "Read error!!!!!!!!!!"; onPackageDataError(); return; } m_readState = READ_STATE_WAIT_FILE_JSON_HDR; m_dataPos = 0; memcpy((char*)&m_datalen,m_data->data(),sizeof(m_datalen)); m_datalen = ntohl64(m_datalen); delete m_data; m_data = NULL; if (m_datalen > 0) { m_data = new QByteArray(m_datalen,0); } else { onPackageDataError(); return; } } if (m_readState == READ_STATE_WAIT_FILE_JSON_HDR) { int dataReaded = read(m_data->data() + m_dataPos,m_datalen - m_dataPos); if (dataReaded == 0) { return; } m_dataPos += dataReaded; if (m_dataPos < m_datalen) { return; } if (m_dataPos > m_datalen) { DEBUG_INFO << "Read error!!!!!!!!!!"; onPackageDataError(); return; } JsonHelper jsonHelper; if (PL_RET_OK != jsonHelper.parseObject(m_data)) { onPackageDataError(); return; } QJsonObject jsonHdr = jsonHelper.m_jsonObj; m_srcFileName = jsonHdr.value("path").toString(); m_dstFileName = jsonHdr.value("dstPath").toString(); m_fileTransferRetCode = jsonHdr.value("retCode").toInt(); m_datalen = m_dataHdr.data_len - m_datalen; if (m_datalen == 0) { //need post error here DEBUG_INFO << "No file data"; emit dataReceived(m_devID,m_dataHdr.TAG,NULL); m_dataPos = 0; m_readState = READ_STATE_WAIT_PACKAGE_HEADER; } else { m_readState = READ_STATE_WAIT_FILE_DATA; m_dataPos = 0; } } if (m_readState == READ_STATE_WAIT_FILE_DATA) { int buflen = 20480; char dataBuf[20480]; if ((m_datalen - m_dataPos) < buflen) { //this is to make sure do not read extra data buflen = m_datalen - m_dataPos; } int dataReaded = read(dataBuf,buflen); if (dataReaded < 1) { return; } //save data to file m_downloadfile.setFileName(m_dstFileName); bool opened = false; if (m_dataPos == 0 ) { //DEBUG_INFO << "Empty existing file" << m_dstFileName; opened = m_downloadfile.open(QIODevice::WriteOnly | QIODevice::Truncate); } else { opened = m_downloadfile.open( QIODevice::Append); } if (!opened) { DEBUG_INFO << "Failed to open file:" + m_dstFileName << m_downloadfile.error(); m_downloadfile.remove(); onPackageDataError(); return; } if (m_downloadfile.write(dataBuf,dataReaded) != dataReaded) { DEBUG_INFO << "Failed to save file:" + m_downloadfile.error() << ":" <<m_downloadfile.errorString(); m_downloadfile.close(); m_downloadfile.remove(); onPackageDataError(); return; } m_downloadfile.close(); m_dataPos += dataReaded; if (++m_iLoopCounter % 100 == 0 && m_datalen) { static qint64 last_percent = 0; qint64 percent = (m_dataPos*100)/m_datalen; if (percent != last_percent) { last_percent = percent; QString info = QString("%1% data received").arg(percent); emit busyMessage(info); } } if (m_dataPos == m_datalen) { emit dataReceived(m_devID,m_dataHdr.TAG,m_data); emit busyMessage("Receive file data done"); m_dataPos = 0; m_readState = READ_STATE_WAIT_PACKAGE_HEADER; } } goto START_RECV; }