Changeset 468

Show
Ignore:
Timestamp:
09/02/05 15:11:12 (8 years ago)
Author:
ahu
Message:

move to new 'wait with select' system

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/dnsreplay-mindex.cc

    r464 r468  
    2121What to do with timeouts. We keep around at most 65536 outstanding answers.  
    2222*/ 
     23 
     24 
     25/*  
     26   mental_clock=0; 
     27   for(;;) { 
     28 
     29   do { 
     30      read a packet 
     31      send a packet 
     32    } while(time_of_last_packet_sent < mental_clock)  
     33    mental_clock=time_of_last_packet_sent; 
     34 
     35    wait for a response packet for 0.1 seconds 
     36    note how much time has passed 
     37    mental_clock+=time_passed; 
     38   } 
     39 
     40 */ 
    2341 
    2442#include <pcap.h> 
     
    4462bool s_quiet=true; 
    4563 
     64 
     65void normalizeTV(struct timeval& tv) 
     66{ 
     67  if(tv.tv_usec > 1000000) { 
     68    ++tv.tv_sec; 
     69    tv.tv_usec-=1000000; 
     70  } 
     71  else if(tv.tv_usec < 0) { 
     72    --tv.tv_sec; 
     73    tv.tv_usec+=1000000; 
     74  } 
     75} 
     76 
     77const struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs) 
     78{ 
     79  struct timeval ret; 
     80  ret.tv_sec=lhs.tv_sec + rhs.tv_sec; 
     81  ret.tv_usec=lhs.tv_usec + rhs.tv_usec; 
     82  normalizeTV(ret); 
     83  return ret; 
     84} 
     85 
     86const struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs) 
     87{ 
     88  struct timeval ret; 
     89  ret.tv_sec=lhs.tv_sec - rhs.tv_sec; 
     90  ret.tv_usec=lhs.tv_usec - rhs.tv_usec; 
     91  normalizeTV(ret); 
     92  return ret; 
     93} 
     94 
     95const struct timeval operator*(int fact, const struct timeval& rhs) 
     96{ 
     97  //  cout<<"In: "<<rhs.tv_sec<<" + "<<rhs.tv_usec<<"\n"; 
     98  struct timeval ret; 
     99  if( (2000000000 / fact) < rhs.tv_usec) { 
     100    double d=1.0 * rhs.tv_usec * fact; 
     101    ret.tv_sec=fact * rhs.tv_sec; 
     102    ret.tv_sec+=(int) (d/1000000); 
     103    d/=1000000; 
     104    d-=(int)d; 
     105 
     106    ret.tv_usec=1000000*d; 
     107    normalizeTV(ret); 
     108     
     109    cout<<"out complex: "<<ret.tv_sec<<" + "<<ret.tv_usec<<"\n"; 
     110     
     111    return ret; 
     112  } 
     113 
     114  ret.tv_sec=rhs.tv_sec * fact; 
     115  ret.tv_usec=rhs.tv_usec * fact; 
     116 
     117  normalizeTV(ret); 
     118  //  cout<<"out simple: "<<ret.tv_sec<<" + "<<ret.tv_usec<<"\n"; 
     119  return ret; 
     120} 
     121 
     122 
     123bool operator<(const struct timeval& lhs, const struct timeval& rhs)  
     124{ 
     125  return make_pair(lhs.tv_sec, lhs.tv_usec) < make_pair(rhs.tv_sec, rhs.tv_usec); 
     126} 
     127 
     128 
     129 
     130 
    46131class DNSIDManager : public boost::noncopyable 
    47132{ 
     
    63148    } 
    64149    else 
    65       throw runtime_error("out of ids!"); 
     150      throw runtime_error("out of ids!"); // XXX FIXME 
    66151  } 
    67152 
     
    249334  IPEndpoint remote; 
    250335 
     336  int res=waitForData(s_socket->getHandle(), 0, 25000); 
     337  if(res < 0 || res==0) 
     338    return; 
     339 
    251340  while(s_socket->recvFromAsync(packet, remote)) { 
    252341    try { 
     
    286375    } 
    287376  } 
     377 
    288378} 
    289379catch(exception& e) 
     
    413503      dh->id=htons(qd.d_assignedID); 
    414504 
     505#if 0 
    415506      if(lastsent.tv_sec && (!(s_questions%25))) { 
    416507        double seconds=pr.d_pheader.ts.tv_sec - lastsent.tv_sec; 
     
    440531       
    441532      //        cout<<"sending!"<<endl; 
     533#endif 
    442534      s_socket->sendTo(string(pr.d_payload, pr.d_payload + pr.d_len), remote); 
    443535    } 
     
    503595 
    504596  unsigned int once=0; 
     597  struct timeval mental_time; 
     598  mental_time.tv_sec=0; mental_time.tv_usec=0; 
     599 
     600  if(!pr.getUDPPacket()) 
     601    return 0; 
     602 
    505603  for(;;) { 
     604 
    506605    if(!((once++)%100))  
    507606      houseKeeping(); 
    508607 
    509     if(!g_throttled) { 
     608    int count=0; 
     609    while(pr.d_pheader.ts < mental_time) { 
    510610      if(!pr.getUDPPacket()) 
    511         break; 
     611        goto out; 
    512612       
    513613      sendPacketFromPR(pr, remote); 
    514     } 
    515     else  
    516       usleep(1000); // move to 'swift poll'  
     614      count++; 
     615    }  
     616 
     617    //    cout<<count<<"\n"; 
     618 
     619    mental_time=pr.d_pheader.ts; 
     620    struct timeval then, now; 
     621    gettimeofday(&then,0); 
    517622 
    518623    receiveFromReference(); 
    519   } 
     624 
     625    gettimeofday(&now, 0); 
     626 
     627    mental_time= mental_time + 2*(now-then); 
     628  } 
     629 out:; 
    520630} 
    521631catch(exception& e)