int PacketConvert::svc() { REPORT_THREAD_INFO(); Packet * packet = NULL; MSG_TYPE * protobuf_msg = NULL; PackInfo pack_info; PacketVec_t packet_vec; ACE_Time_Value sleep_time(0, 1 * 1000); while (!m_is_stop) { packet_vec.clear(); { ACE_GUARD_RETURN(ACE_Thread_Mutex, guard, m_packet_vec_mutex, -2); std::copy(m_packet_vec.begin(), m_packet_vec.end(), std::back_inserter(packet_vec)); m_packet_vec.clear(); } if (packet_vec.empty()) { ACE_OS::sleep(sleep_time); continue; } for (PacketVec_t::iterator it = packet_vec.begin(); it != packet_vec.end(); ++it) { packet = *it; if (packet->opcode() == SMSG_TIMER_OCCUR) { pack_info.guid = packet->guid(); pack_info.opcode = packet->opcode(); pack_info.msg = NULL; m_scene_imp->packInput(new PackInfo(pack_info.opcode, pack_info.guid, pack_info.msg)); } else { protobuf_msg = NULL; if (extractPacket(packet, protobuf_msg)) { pack_info.guid = packet->guid(); pack_info.opcode = packet->opcode(); pack_info.msg = protobuf_msg; PackInfo * pi = new PackInfo(pack_info.opcode, pack_info.guid, pack_info.msg); pi->owner = packet->getOwner(); m_scene_imp->packInput(pi); } else { DEF_LOG_ERROR("failed to extract message from packet, opcode is <%d>, guid is <%s>\n", packet->opcode(), boost::lexical_cast<string>(packet->guid()).c_str()); } } delete packet; } } return 0; }
BYTE* searchDataStream(BYTE *search_sync_ptr, BYTE *search_end_ptr) { BYTE sync_status; while (search_sync_ptr < search_end_ptr) { search_sync_ptr = searchSync(search_sync_ptr, search_end_ptr, &sync_status); if (sync_status == SYNC_FOUND) { if (search_end_ptr - search_sync_ptr > 2) { { //是否为完整的数据流 if ((search_end_ptr - search_sync_ptr) >= *(search_sync_ptr+2) + 3) { if (isCheckValueVaild(search_sync_ptr)) { //提取数据 //RETAILMSG(1,(TEXT("socket Read right!\r\n"))); search_sync_ptr = extractPacket(search_sync_ptr); } else { //RETAILMSG(1,(TEXT("socket Read err!\r\n"))); search_sync_ptr += 2; } } else { break; } } } else { break; } } else if (sync_status == SYNC_MISSING) { search_sync_ptr += 1; } else { break; } } return search_sync_ptr; }
/****************************************************************************************************** * * *Receive task execution * * * * ******************************************************************************************************/ void rx_task () { uint8_t rssi,len,*local_rx_buf,v; int receivedSeqNo=0,senderNode = 0; bmac_set_cca_thresh(DEFAULT_BMAC_CCA); bmac_rx_pkt_set_buffer (rx_buf,RF_MAX_PAYLOAD_SIZE); while(!bmac_started()); printf("Receiver node Bmac initialised\n"); while(1) { if( !bmac_rx_pkt_ready()) continue; //printf("received packet\n\r"); nrk_led_toggle(ORANGE_LED); local_rx_buf = (uint8_t *)bmac_rx_pkt_get (&len, &rssi); //If my own packet then dont receive if(local_rx_buf[SOURCE_ADDRESS_LOCATION]==MY_ID) { continue; } //Sample the data receivedSeqNo = local_rx_buf[SEQUENCE_NUM_LOCATION]; senderNode = local_rx_buf[SOURCE_ADDRESS_LOCATION]; //Check if the message is intended for me if(len!=0 && local_rx_buf[DESTINATION_ADDRESS_LOCATION]==MY_ID) { //Check the type of data switch(local_rx_buf[MESSAGE_TYPE_LOCATION]) { case DATA: if(receivedSeqNo>lastReceivedSequenceNo[senderNode]) { receiveComplete[senderNode] =0; printf("received new packet\n\r"); //Process and send the acknowledgement to the receiver lastReceivedSequenceNo[senderNode]=receivedSeqNo; extractPacket(local_rx_buf,&receiveData[DATA_LOCATION(senderNode)],len,senderNode); if(local_rx_buf[PACKET_NUM_LOCATION]==FIRST_PACKET) { receivedPacketSize[senderNode]=0; receivedPacketSize[senderNode] +=(len-HEADER_SIZE); } else { receivedPacketSize[senderNode] +=(len-HEADER_SIZE); } //Send the acknowledgement ack_buf[senderNode][DESTINATION_ADDRESS_LOCATION]=senderNode; ack_buf[senderNode][SOURCE_ADDRESS_LOCATION]= MY_ID; ack_buf[senderNode][SEQUENCE_NUM_LOCATION] = receivedSeqNo; ack_buf[senderNode][MESSAGE_TYPE_LOCATION] = DATA_PACKET_ACK; receiverNextPacket[senderNode]= SEND_ACK; if(local_rx_buf[PACKET_NUM_LOCATION]==LAST_PACKET) { receiveComplete[senderNode] =1; for(int i=0;i<receivedPacketSize[senderNode];i++) { printf("%d \t",receiveData[i]); } } sendAckFlag[senderNode]=1; } else { printf("received old packet\n\r"); ack_buf[senderNode][DESTINATION_ADDRESS_LOCATION]=senderNode; ack_buf[senderNode][SOURCE_ADDRESS_LOCATION]= MY_ID; ack_buf[senderNode][SEQUENCE_NUM_LOCATION] = lastReceivedSequenceNo[senderNode]; ack_buf[senderNode][MESSAGE_TYPE_LOCATION] = DATA_PACKET_ACK; //Send the acknowledgement to the sender receiverNextPacket[senderNode]= SEND_ACK; sendAckFlag[senderNode]=1; } //v=nrk_event_signal( signal_ack ); break; case DATA_PACKET_ACK: //If it is a new ACK packet then accept it. Else dont bother if(lastSentSequenceNo[senderNode]==receivedSeqNo) { printf("Received Ack\n"); switch(sendNextPacket[senderNode]) { case WAIT_ACK: sendNextPacket[senderNode]= IN_PROGRESS; printf("Sent IN_PROGRESS\n"); break; case LAST_PACKET_ACK: printf("Sent end_PROGRESS\n"); sendNextPacket[senderNode] = END; break; default: printf("Error shouldn't have entered here"); break; } } break; default: break; } } bmac_rx_pkt_release (); nrk_wait_until_next_period(); } // pointing the function pointer to the copied code in the flash }
void csp3(void *arg) { int errorCode, numBytesRead, i; ubyte bytes[B]; int numBytesPreviouslyRead = 0; struct timeval timeout; fd_set readfs; gettimeofday(&lastPktTime, NULL); FD_SET(fd, &readfs); /* Reading loop */ while (TRUE) { pthread_mutex_lock( &endFlagMutex); if (terminationFlag == TRUE) break; pthread_mutex_unlock( &endFlagMutex); timeout.tv_sec = 2; timeout.tv_usec = 0; errorCode = select(fd+1, &readfs, NULL, NULL, &timeout); if (errorCode==0) { fprintf(stderr,"Timed out at select()\n"); } else if (errorCode==-1) { fprintf(stderr, "Problem with select(): %s\n", strerror(errno)); //endProgram(); exit(EXIT_FAILURE); } numBytesRead = read(fd, &bytes, B-numBytesPreviouslyRead); if (numBytesRead==-1) { fprintf(stderr, "Problem with read(): %s\n", strerror(errno)); //endProgram(); //TODO See if it is possible to implement this instead of just exit() exit(EXIT_FAILURE); } else { for (i = 0; i < numBytesRead; i++) packet[numBytesPreviouslyRead+i] = bytes[i]; numBytesPreviouslyRead += numBytesRead; if (numBytesPreviouslyRead==B) { //packet complete! if (checkPacket()) { extractPacket(); reflexes(); ensureTransmitted(); pthread_mutex_lock( &pktNumMutex ); pktNum++; pthread_mutex_unlock( &pktNumMutex ); numBytesPreviouslyRead = 0; } else { printf("misaligned packet.\n"); //TODO Does this have a range error? for (i = 1; i<B; i++) packet[i-1] = packet[i]; numBytesPreviouslyRead--; } } } } }
Packet::Packet(void *packet) : _type(0), _origin(0), _target(0), _data(NULL), _packet(packet) { extractPacket(packet); }