🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #150081

Time.

#150081
From: Jeff Relf
Date: Tue, 15 Jun 2004 01:13
182 lines
6641 bytes
Hi MLC,

Ah, a Dialog script allowed you to change your
  User-Agent  line.  That's a  Very  cool feature, I think.

  Can you make a Message-ID like mine ?
  <_Jeff_Relf_2004_Jun_14_4mVZ@NCPlus.NET>
                          ^^^^
The 4 characters above are the low bits of
  Win98's high precision tic counter  ( See below ),
  expressed in base 62, using the ten digits, 0-9,
  plus the alphabet, A-Z, a-z, 2 * 26.
  ( NCPlus.NET is my ISP, a cheap dial-up account )

P.S.
  I fully realize that the following will make your eyes
  glaze over, but maybe some future Googler might enjoy it.

  If so, he can drop me a line at
    http://www.NCPlus.NET/~jeff_relf/ .
    ( Provided that I'm still using the same ISP then )

typedef LARGE_INTEGER Quad ;
typedef unsigned long ulong ;  typedef char * Line ;

__int64 _TimeUTC, _Tics, Int_Div, Secnd ;
double Secnd_Dub, _Secs ;

QueryPerformanceFrequency( ( Quad * ) & Secnd );

Secnd_Dub = Secnd ;

Int_Div = ( __int64( 1 ) << 32 ) * 1000 / Secnd ;

// Below is for  WM_  events, C is wparam and C2 is lparam.
MSG Msg ;  uint M, M_Ras, C ; ulong C2 ;

// Because Windows packs int16's into int32's.
ushort * P_C = ( ushort * ) & C, 
  * P_C2 = ( ushort * ) & C2 ;

int rv ; // a holding place for very temporary int's.

#define Tics \
  ( QueryPerformanceCounter( ( Quad * ) & _Tics ), _Tics )

#define Secs ( _Secs = Tics / Secnd_Dub )

#define GetMilliseconds \
  ( ulong ( ( Tics * Int_Div ) >> 32 ) )

#define TimeUTC \
  ( ( GetSystemTimeAsFileTime ( \
  ( FILETIME * ) & _TimeUTC ), _TimeUTC ) \
  / 10000  - 9435484800000 )

// " Win "  is my window, HWND.
// " Connected "  is the handle to the socket, a ulong.
#define Close ( WSAAsyncSelect ( Connected, Win, 0, 0 ), \
  closesocket( Connected ),  Connected = -1 )

So a timeout loop looks like this:

 double Due = Secs + 5 ;
  while ( Secs < Due  &&  
    ! ( Respond ( WM_USER + 2 ) && * P_C2 == FD_READ ) ) ;

Where Respond () simply checks for events,
  or returns the requested event, without ever waiting.

int Respond ( uint _M ) {  M = C = C2 = 0;

  while( PeekMessage( & Msg, 0,0,0, PM_REMOVE ) ) {
    TranslateMessage( & Msg );  DispatchMessage ( & Msg );
    M = Msg.message ;  C = Msg.wParam ;  C2 = Msg.lParam ;
    if ( _M && _M == M ) return 1 ; }

  Paint_Req = 1 ;  Paint_Maybe ( ); return 0 ; }
  // Paint_Maybe() is a VRAM-to-VRAM-only large window blt,
  //   ( it takes into account Win98's cursor and taskbar ),
  //   and it aborts rather than queuing blt commands.

And with my cheap dial-up,
  I had to write my own dial-up manager,
  which pings Time.NIST.GOV, port 37.
  ( National Institute of Standards and Technology )

  The NIST.GOV ping not only keeps my clock accurate
  ( to within a few deci-seconds ),
  it also lets me know when my cheap ISP is failing.

To wit:

  #define LOOP while ( 1 )

  #define SyncDown SetPriorityClass( \
    GetCurrentProcess(), NORMAL_PRIORITY_CLASS ); \
    return  _Wire ; // _Wire is the phone number, ignore it

  int Ti = 0 ;  __int64  Off_By = 0;  
  int Secs_ = 2 ;  double  Due = Secs + Secs_ ;  
  int Compares = 0 ; uint Delay_Net ;
  LOOP { 

    while ( Secs < Due &&  Connected < 0 
      && ! Logged_In ( NTP ) ) ;
    // Logged_In ( NTP ) reconnects to the NTP server,
    //   it doesn't actually login here,
    //   because Time.NIST.GOV is open access.
    //   It's WSAAsyncGetHostByName() is asynchronous.

    if ( Connected >= 0 ) { // Socket is still open.

      if ( ! Ti ) { 
        Sh ( " Pinging  %s ...", SrvNam );  Ti = 1 ;
        SetPriorityClass( GetCurrentProcess(), 
        REALTIME_PRIORITY_CLASS ); }

      while ( Secs < Due &&  ! Respond ( WM_USER + 2 ) ) ; }

    if (  M != WM_USER + 2 ) {  
      Close ;
      if ( ! Compares ) {  
        Ln.E -- ;  Sh (  " Failed to Ping %s.", SrvNam );
        SyncDown }

      Off_By /= Compares;  _Secs = fabs ( Off_By / 1000. ); 

      // Buff is just a huge global buffer.
      Line P = Buff + 90 ;
      Str ( P, "%3.1f", _Secs ); 

      // e.g. Makes  0.3  simply  .3
      if ( * P == '0' ) P ++ ;  
      
      Ln.E -- ; // This backs up a line in my shell. 

      Sh (  " %d Compares in %d Secs,"
        "  %s Secs %s.", Compares, Secs_, P,
        Off_By < 0 ? " Slow" : " Fast" );

      if ( _Secs >  360 )  { 
        Ln.E -- ;
        Sh( " To different to sync." ); SyncDown }

      __int64 File_TimeUTC = 
        10000 * ( TimeUTC + Off_By + 9435484800000 ); 

      SYSTEMTIME SysTime ;
      FileTimeToSystemTime( 
        ( FILETIME * ) & File_TimeUTC , & SysTime );
 
      // This actually sets my clock.
      SetSystemTime( & SysTime );  SyncDown }

    // if an error from recv(), this is not really needed
    //   as recv() returns the number of bytes read.
    if ( P_C2 [ 1 ] ) { Close ; continue ; }

    // This gets the two-way delay from the net traffic.
    if ( * P_C2 == FD_CONNECT )  { 
      Delay_Net = GetMilliseconds ; continue ; }

    if ( * P_C2 != FD_READ ) continue ; 

    ulong TimeUTC_NTP ;

    // Here the NTP server is giving me 4 bytes
    //   which happens every time I connect to it.
    rv = recv ( Connected, ( Line ) & TimeUTC_NTP, 4, 0 );

    if ( rv != 4 ) { Close ;  continue ; }

    Delay_Net = GetMilliseconds -  Delay_Net ;

    Off_By += ( __int64 ) ntohl( TimeUTC_NTP ) * 1000 
      + ( Delay_Net >> 1 ) - TimeUTC ;

    Close ;  Compares ++ ; }

And on and on ...
Technical Details
Message-ID: <_Jeff_Relf_2004_Jun_14_32Id@NCPlus.NET>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.20140922.mbox.gz!number1.nntp.dca.giganews.com!internal1.nntp.sjc.giganews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjc.giganews.com!nntp.giganews.com!news.moat.net!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!uni-berlin.de!not-for-mail
References: <hz13ek41vcz7$.dlg@ID-187316.user.uni-berlin.de> <_Jeff_Relf_2004_Jun_14_3xcX@NCPlus.NET> <14ttzks0k5c5g.dlg@ID-187316.user.uni-berlin.de> <_Jeff_Relf_2004_Jun_14_4mVZ@NCPlus.NET> <1nk239mpgbusc.dlg@ID-187316.user.uni-berlin.de>

Show Full Headers
Complete Article Headers
Message-ID: <_Jeff_Relf_2004_Jun_14_32Id@NCPlus.NET>
Subject: Time.
Date: Tue, 15 Jun 2004 01:13:12 +0000
From: Jeff Relf <Me@Privacy.NET>
References: <hz13ek41vcz7$.dlg@ID-187316.user.uni-berlin.de> <_Jeff_Relf_2004_Jun_14_3xcX@NCPlus.NET> <14ttzks0k5c5g.dlg@ID-187316.user.uni-berlin.de> <_Jeff_Relf_2004_Jun_14_4mVZ@NCPlus.NET> <1nk239mpgbusc.dlg@ID-187316.user.uni-berlin.de>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.20140922.mbox.gz!number1.nntp.dca.giganews.com!internal1.nntp.sjc.giganews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjc.giganews.com!nntp.giganews.com!news.moat.net!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!uni-berlin.de!not-for-mail
Newsgroups: news.software.readers,comp.os.linux.advocacy
Lines: 182
X-Trace: news.uni-berlin.de oEcl3VxFkG8eXENsrh8GDAMbX4bi3ZX1PMHgnCrKmMzbY7FECE
User-Agent: RRR_Puppy