[Winpcap-users] Efficient sending huge number of UDP packets at a fixed specified rate

Gianluca Varenni Gianluca.Varenni at riverbed.com
Mon Apr 11 17:21:15 PDT 2011


You cannot have a microseconds wait on windows (or linux). The only way to get sub-milliseconds delay is to use a busy wait loop and check the high performance counter with QueryPerformanceCounter/QueryPerformanceFrequency.

Have a nice day
GV

-----Original Message-----
From: winpcap-users-bounces at winpcap.org [mailto:winpcap-users-bounces at winpcap.org] On Behalf Of "Fish" (David B. Trout)
Sent: Saturday, April 09, 2011 3:34 PM
To: winpcap-users at winpcap.org
Subject: Re: [Winpcap-users] Efficient sending huge number of UDP packets at a fixed specified rate

Metcherin Sergey wrote:
> Fish wrote:
> > Metcherin Sergey wrote:
> > 
> > > Do you know efficient way of sending UDP packets at a fixed rate 
> > > efficiently?
> > > [...]
> > > Can you give any recommendations?
[...]
> > If you don't need to delay for less than a millisecond between 
> > packets (or groups of packets) you might consider using NPG (Network 
> > Packet Generator):
> 
> Thank you for the link, unfortunately I need high time resolution
> - several microseconds.

It's open source.

Just change the millisecond Sleep()'s to function calls that delay for a specified number of microseconds instead:


int usleep( useconds_t usecs )
{
  struct timespec rqtp;

  if (usecs < 0 || usecs >= 1000000)
  {
    errno = EINVAL;
    return -1;
  }

  rqtp.tv_sec  = 0;
  rqtp.tv_nsec = usecs * 1000;

  return nanosleep( &rqtp );
}

int nanosleep( const struct timespec* rqtp ) {
  static BOOL    bDidInit   = FALSE;
  static HANDLE  hTimer     = NULL;
  LARGE_INTEGER  liDueTime;

  // Create the waitable timer if needed...

  if (!bDidInit)
  {
    bDidInit = TRUE;
    VERIFY((hTimer = CreateWaitableTimer( NULL, TRUE, NULL )) != NULL);
  }

  // Check passed parameters...

  if (!rqtp || rqtp->tv_nsec < 0 ||
    rqtp->tv_nsec >= 1000000000)
  {
    errno = EINVAL;
    return -1;
  }

  // SetWaitableTimer argument is #of 100-nanosecond intervals.
  // Positive values indicate absolute UTC time. Negative values
  // indicate relative time. The actual timer accuracy depends
  // on the capability of your hardware.

  liDueTime.QuadPart =  -      // (negative means relative)
  (
      (((__int64)rqtp->tv_sec * 10000000))
      +
      (((__int64)rqtp->tv_nsec + 99) / 100)
  );

  // Set the waitable timer...

  VERIFY( SetWaitableTimer( hTimer, &liDueTime, 0, NULL, NULL, FALSE ));

  // Wait for the waitable timer to expire...

  VERIFY( WaitForSingleObject( hTimer, INFINITE ) == WAIT_OBJECT_0 ;
  return 0;
}


--
"Fish" (David B. Trout)
 fish at softdevlabs.com




_______________________________________________
Winpcap-users mailing list
Winpcap-users at winpcap.org
https://www.winpcap.org/mailman/listinfo/winpcap-users


More information about the Winpcap-users mailing list