void UDPConnection::Update() { if (!sharedSocket) { unsigned recv = 0; unsigned char buffer[UDPBufferSize]; sockaddr_in fromAddr; while ((recv = mySocket->RecvFrom(buffer, UDPBufferSize, &fromAddr)) >= hsize) { RawPacket* data = new RawPacket(buffer, recv); if (CheckAddress(fromAddr)) ProcessRawPacket(data); else ; // silently drop } } const unsigned curTime = SDL_GetTicks(); bool force = false; // should we force to send a packet? if((dataRecv == 0) && lastSendTime < curTime-1000 && !unackedPackets.empty()){ //server hasnt responded so try to send the connection attempt again SendRawPacket(unackedPackets[0].data,unackedPackets[0].length,0); lastSendTime = curTime; force = true; } if (lastSendTime<curTime-5000 && !(dataRecv == 0)) { //we havent sent anything for a while so send something to prevent timeout force = true; } else if(lastSendTime<curTime-200 && !waitingPackets.empty()){ //we have at least one missing incomming packet lying around so send a packet to ensure the other side get a nak force = true; } Flush(force); }
int RTPSources::ProcessRawPacket(RTPRawPacket *rawpack, RTPTransmitter *rtptrans, bool acceptownpackets) { RTPTransmitter *transmitters[1]; int num; transmitters[0] = rtptrans; if (rtptrans == 0) num = 0; else num = 1; return ProcessRawPacket(rawpack, transmitters, num, acceptownpackets); }
void CNet::Update(void) { Uint64 t; t = SDL_GetTicks(); curTime=float(t)/1000.f; if(playbackDemo) ReadDemoFile(); if(onlyLocal) { return; } sockaddr_in from; socklen_t fromsize; fromsize=sizeof(from); int r; unsigned char inbuf[16000]; if(connected) while(true) { if((r=recvfrom(mySocket,(char*)inbuf,16000,0,(sockaddr*)&from,&fromsize))==SOCKET_ERROR) { if (IsFakeError()) break; char test[500]; sprintf(test,"Error receiving data. %i %d",(int)imServer,WSAGetLastError()); handleerror(NULL,test,"SHUTDOWN ERROR",MBF_OK | MBF_INFO); exit(0); } int conn=ResolveConnection(&from); if(conn==-1) { if(waitOnCon && r>=12 && (*(int*)inbuf)==0 && (*(int*)&inbuf[4])==-1 && inbuf[8]==0 && inbuf[9]==NETMSG_ATTEMPTCONNECT && inbuf[11]==NETWORK_VERSION) { conn=InitNewConn(&from,false,inbuf[10]); } else { continue; } } inInitialConnect=false; int packetNum=(*(int*)inbuf); ProcessRawPacket(inbuf,r,conn); } for(int a=0; a<gs->activePlayers; ++a) { Connection* c=&connections[a]; if(c->localConnection || !c->active) continue; std::map<int,Packet*>::iterator wpi; while((wpi=c->waitingPackets.find(c->lastInOrder+1))!=c->waitingPackets.end()) { //process all in order packets that we have waiting if(c->readyLength+wpi->second->length>=NETWORK_BUFFER_SIZE) { logOutput.Print("Overflow in incoming network buffer"); break; } memcpy(&c->readyData[c->readyLength],wpi->second->data,wpi->second->length); c->readyLength+=wpi->second->length; delete wpi->second; c->waitingPackets.erase(wpi); c->lastInOrder++; } if(inInitialConnect && c->lastSendTime<curTime-1) { //server hasnt responded so try to send the connection attempt again SendRawPacket(a,c->unackedPackets[0]->data,c->unackedPackets[0]->length,0); c->lastSendTime=curTime; } if(c->lastSendTime<curTime-5 && !inInitialConnect) { //we havent sent anything for a while so send something to prevent timeout SendData(NETMSG_HELLO); } if(c->lastSendTime<curTime-0.2f && !c->waitingPackets.empty()) { //we have at least one missing incomming packet lying around so send a packet to ensure the other side get a nak SendData(NETMSG_HELLO); } if(c->lastReceiveTime < curTime-(inInitialConnect ? 40 : 30)) { //other side has timed out c->active=false; } if(c->outgoingLength>0 && (c->lastSendTime < (curTime-0.2f+c->outgoingLength*0.01f) || c->lastSendFrame < gs->frameNum-1)) { FlushConnection(a); } } }