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

Fish" (David B. Trout fish at infidels.org
Sat Apr 9 15:33:38 PDT 2011


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






More information about the Winpcap-users mailing list