Packet* PacketParser::ParsePacket(Buffer* raw) { if( raw->size() == 0 ) return 0L; uint8_t id = 0; // Packet ID LOG_NET("ParsePacket: %d bytes\n", raw->size()); //raw->dump(); *raw >> id; Packet* newPacket = CreatePacket(id); if(! newPacket ) { LOG_NET("Recieved invalid packet type %d\n", id); raw->clear(); return NULL; } try { LOG_NET("Parsing payload..\n"); (*newPacket) << *raw; } catch( ... ) { DeletePacket(newPacket); LOG_NET("Exception thrown!\n"); return NULL; } return newPacket; }
cUDPpacket* cPeer::GetReadyPacket() { if(ToKill) { if(ReceivedPackets == ToKill) ReceivedPackets = ReceivedPackets->Next; DeletePacket(ToKill); ToKill = 0; } if(ReceivedPackets) { //si on est en rpesence d'un packet unsafe, il est disponible c clair if(ReceivedPackets->ID == 0) { ToKill = ReceivedPackets; return ReceivedPackets; } //on va checker si c le packet qu'on attendait if(ReceivedPackets->ID == PendingID) { //on est rendu au bon packet PendingID++; ToKill = ReceivedPackets; /*fp = fopen("_peerDebug.txt","a"); fprintf(fp,"Current Pending ID : %i",(int)PendingID); fclose(fp);*/ return ReceivedPackets; } } return 0; }
void CRTPRecv::OnPollThreadStep() { BeginDataAccess(); // check incoming packets if (GotoFirstSourceWithData()) { //处理接收数据包. do { RTPPacket *pack = NULL; while ((pack = GetNextPacket()) != NULL) { m_RecvTimeStamp = GetTickCount(); if (!m_RecvFlag) { //有新数据进来 m_RecvFlag=TRUE; //获取对端地址 RTPAddress *pAddress = (RTPAddress*)(GetCurrentSourceInfo()->GetRTPDataAddress()); //添加远端到发送地址列表 RTPIPv4Address *pIPV4Address = (RTPIPv4Address*)pAddress; //AddDestination(*pIPV4Address); //获取远端IP in_addr addr; addr.S_un.S_addr=pIPV4Address->GetIP(); //通知出去 //m_pRTPRecvStatusCallBack(EN_RTP_RECV_STATUS::StartRecv,inet_ntoa(addr),pIPV4Address->GetPort(),m_pRTPRecvStatusUserData); } HandleRTPData(pack); DeletePacket(pack); } } while (GotoNextSourceWithData()); } EndDataAccess(); }
void GRIDataBlock::LoadBalancing() { QList<reader_t*>::iterator it; DeletePacket(); // not much imbalance in the system if ((write_counter_ - first_packet_) < MAX_THRESHOLD) { return; } // Either decrease priority of writer (if possible) or increase the priority // of the reader (if possible) if(rand() % 2 && (int)writer_->priority() > (int)QThread::IdlePriority) { this->writer_->ChangePriority(false); } else { for (it = readers_.begin(); it != readers_.end(); it++) { reader_t* reader = *it; if ((reader->read_counter - first_packet_) > LOAD_BALANCING_FACTOR * MAX_THRESHOLD && (int)reader->reader->priority() < (int)QThread::TimeCriticalPriority) { reader->reader->ChangePriority(true); } } } }
int cPeer::SendPackets(int fd,float elapsed,fd_set master, UINT4 & nbBytes) { //pour chaque packet on va calculer combien ca fait qu'on envoie et limiter a 400 bytes par packet //pour garder de lespace pour nos ack int sent = 0; char sendBuf[800]; //buffer dans lequel on va packer les packets cUDPpacket *toKill = 0; fd_set fdwrite = master; // copy it timeval Timeout; Timeout.tv_sec = 0; Timeout.tv_usec = 0; if (select(fd+1, NULL, &fdwrite, NULL, &Timeout) == -1) { //char LastError[100]; //sprintf(LastError,"Error select()ing while cPeer::SendPackets WSA : %i",WSAGetLastError()); return 1; } Timeout.tv_sec = 0; Timeout.tv_usec = 0; if(FD_ISSET(fd,&fdwrite)) { //on debug lenvoie //char ip[16]; //unsigned short port; //sprintf(ip,inet_ntoa(IpAdress.sin_addr)); //port = ntohs(IpAdress.sin_port); //fp = fopen("_netDebug.txt","a"); //fprintf(fp,"> Sending Packets to peer with IP : %s:%i\n",ip,port); //fclose(fp); for(cUDPpacket *P=PacketsToSend;P;P=P->Next) { nbBytes += P->Size; if(toKill) { if(PacketsToSend == toKill) PacketsToSend = toKill->Next; DeletePacket(toKill); toKill = 0; } //fp = fopen("_netDebug.txt","a"); //fprintf(fp," >>interfaceID:%i ID:%i typeID:%i size:%i sent:%i\n",P->InterfaceID,P->ID,P->TypeID,P->Size,(int)P->Sent); //fclose(fp); if(P->Sent) //si yavait deja ete envoyer { if(P->Ack) //est-ce qu'on a confirmation de reception de packet ? { //on lenleve du queue toKill = P; continue; } P->LastCheck += elapsed; if(P->LastCheck >= RESEND_DELAY) { P->LastCheck = 0; sent += PackToBuffer(sendBuf + sent,P); } } else //packet jamais envoyer { if(P->TypeID == 1) //un ping { //fp = fopen("_netDebug.txt","a"); //fprintf(fp," >> SENDING PING\n",ip,port); //fclose(fp); } sent += PackToBuffer(sendBuf + sent,P); P->Sent = true; if(!(P->Safe)) toKill = P; //si le packet n'est pas safe, on le delete } if(sent >= 400) break; //si on a atteint la limite de grosseur de packet on sors } if(toKill) { if(PacketsToSend == toKill) PacketsToSend = toKill->Next; DeletePacket(toKill); toKill = 0; } int Acks=0; //on attach ici les ack des packets recu if(nbAcks) { Acks = nbAcks; //DebugAcks(); //on va se fair eun ti packet a ajouter au buffer cUDPpacket ackPacket; ackPacket.ID = 0; ackPacket.InterfaceID = INTERFACE_SYSTEM; ackPacket.TypeID = TYPE_ACK; ackPacket.Size = (unsigned short)(nbAcks * sizeof(UINT4)); ackPacket.Data = new char[ackPacket.Size]; memcpy(ackPacket.Data,AcksToSend,ackPacket.Size); sent += PackToBuffer(sendBuf + sent,&ackPacket); nbBytes += (nbAcks * sizeof(UINT4)); nbAcks = 0; memset(AcksToSend,0,100*sizeof(UINT4)); } //on est pret a envoyer notre packet int len = sizeof(sockaddr); int sentto = 0; int Remaining = sent; while(Remaining) { sentto = sendto(fd,sendBuf + (sent - Remaining),Remaining,0,(sockaddr*)&IpAdress,len); if(sentto == -1) { //int i = WSAGetLastError(); return 1; //error } Remaining -= sentto; } //fp = fopen("_netDebug.txt","a"); //fprintf(fp," >> Sent : %i bytes and %i Acks\n",sent,Acks); //fclose(fp); } return 0; }