void ReceiveThread::receiveData() { WDataInfo WTempInfo; //NetMsgID Messagetype; char temp[100]={0}; while (receiveSocket->bytesAvailable() < sizeof(WDataInfo)) { if (!(receiveSocket->waitForReadyRead(4000))) { //emit ReciverError(RecieverSocket->error(), RecieverSocket->errorString()); std::cout<<"in ReceiveThread::receiveData,waitForReadyRead"<<std::endl; return; } } //connect(&receiveSocket, SIGNAL(disconnected()),&receiveSocket, SLOT(deleteLater())); receiveSocket->read(temp, sizeof(WDataInfo));//读头 memcpy(&WTempInfo,temp,sizeof(WDataInfo)); std::cout<<"WTempInfo idtype is "<<WTempInfo.idtype<<"WDataInfo size---"<<WTempInfo.size<<std::endl; //Messagetype=WTempInfo.idtype; if(strcmp(TcpHeader,WTempInfo.header)!=0) { emit disconnectself(); return; } if(!(receivePackage(WTempInfo) )) { emit disconnectself(); return; } else { return ; } }
void listenConnection(Connection* connection) { Package* package = NULL; Request* request = NULL; int maxClientDataSize; char* url; Tipo tipo; request = malloc(sizeof (Request)); request->count_mutex = malloc(sizeof (pthread_mutex_t)); request->count_threshold_cv = malloc(sizeof (pthread_cond_t)); pthread_cond_init(request->count_threshold_cv, NULL); pthread_mutex_init(request->count_mutex, NULL); pthread_mutex_lock(request->count_mutex); package = receivePackage(connection); strcpy(url, package->dados); tipo = package->tipo; maxClientDataSize = askMaxClientDataSize(connection); //finalize request switch (tipo) { case LS: // request = createRequest(connection, LS); request->tipo = LS; setUrl(request, url); setMaxClientDataSize(request, maxClientDataSize); setStatus(request, READY); break; case WGET: //finalize request // request = createRequest(connection, WGET); request->tipo = WGET; setUrl(request, url); setMaxClientDataSize(request, maxClientDataSize); setStatus(request, READY); break; case CLOSECONNECTION: pthread_exit(NULL); return; break; default: // request = createRequest(connection, OTHER); request->tipo = OTHER; setStatus(request, READY); break; } addRequest(request); pthread_cond_wait(request->count_threshold_cv, request->count_mutex); pthread_mutex_unlock(request->count_mutex); pthread_cond_destroy(request->count_threshold_cv); pthread_mutex_destroy(request->count_mutex); }
int serialReceive(char * data) { char buffer[1028]; char crchigh; char crclow; int read = 0; do { while(!(read = receivePackage(buffer))) {} // NOTICE: ONLY FOR TESTING. REMOVE AFTERWARDS int high = (unsigned char)buffer[0]; int low = (unsigned char)buffer[1]; int type = (unsigned char)buffer[2]; int seq = (unsigned char)buffer[3]; printf("High: %i\nLow: %i\nType: %i\nSeq: %i\nData: %s\n", high, low, type, seq, buffer+4); crcCalc(buffer+4, read-4, crchigh, crclow); if(crchigh == buffer[0] && crclow == buffer[1]) // Pakke rigtig { char ack[] = {0,0,1,buffer[3]}; sendPackage(ack, 4); break; } else // Fejl i pakke { char wrong[] = {0,0,0,buffer[3]}; sendPackage(wrong , 4); } }while(1); for(int i = 4; i < read; i++) // Kopier til 'data' data[i-4] = buffer[i]; return(read); SLIPClose(); }
void serialSend(char * data , int size) // size = størrelse af hvad der skal sendes { // Size checking if(size > MAX_DATA_LENGTH) size = MAX_DATA_LENGTH; static char seq = 0; char crchigh; char crclow; char array[1028]; char buffer[1028]; crcCalc(data, size, crchigh, crclow); array[0] = crchigh; array[1] = crclow; array[2] = 0; array[3] = seq; for(int i = 4; i < size+4; i++) { array[i] = data[i-4]; } do { sendPackage(array, size+4); // NOTICE: ONLY FOR TESTING. REMOVE AFTERWARDS printf("Data: %u %u %u %u %s\n", (unsigned char)array[0], (unsigned char)array[1], (unsigned char)array[2], (unsigned char)array[3], array + 4); while(!receivePackage(buffer)) {} }while(!buffer[2] == 1 && buffer[3] == seq); seq = !seq; }
int askMaxClientDataSize(Connection* connection) { Request* request = NULL; Package* package = NULL; request = createRequest(connection, MAXDATASIZE); request->count_mutex = malloc(sizeof (pthread_mutex_t)); request->count_threshold_cv = malloc(sizeof (pthread_cond_t)); pthread_cond_init(request->count_threshold_cv, NULL); pthread_mutex_init(request->count_mutex, NULL); pthread_mutex_lock(request->count_mutex); addRequest(request); pthread_cond_wait(request->count_threshold_cv, request->count_mutex); package = receivePackage(connection); pthread_mutex_unlock(request->count_mutex); pthread_cond_destroy(request->count_threshold_cv); pthread_mutex_destroy(request->count_mutex); return atoi(package->dados); }