home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Communications / Internet / TurboTCP 2.1 ƒ / TurboTCP core / UTurboTCP.cp < prev    next >
Encoding:
Text File  |  1995-01-06  |  4.3 KB  |  164 lines  |  [TEXT/MMCC]

  1. //
  2. // UTurboTCP.cp
  3. //
  4. //    TurboTCP library
  5. //    Utility functions
  6. //
  7. //    Copyright © 1993-95, FrostByte Design / Eric Scouten
  8. //
  9.  
  10. #include "UTurboTCP.h"
  11.  
  12. #include "CTCPDriver.h"
  13. #include "CTCPResolverCall.h"
  14.  
  15. #if TurboTCP_TCL
  16.     extern Boolean gInBackground;        // TCL global: in background under MultiFinder
  17. #endif
  18.  
  19.  
  20. // -- opening/closing TCP services --
  21.  
  22. //***********************************************************
  23.  
  24. void UTurboTCP::InitTCP()            // static method
  25.  
  26.     // Create a TCP driver object and initialize it. Should be called once at application startup.
  27.     // In a future version of TurboTCP, this routine will check for MacTCP and/or Open Transport
  28.     // and open the appropriate object.
  29.  
  30. {
  31.     if (!CTCPDriver::gTCPDriver)
  32.         new CTCPDriver;
  33. }
  34.  
  35.  
  36. //***********************************************************
  37.  
  38. void UTurboTCP::CloseTCP()                // static method/
  39.  
  40.     // Close the TCP driver object and destroy it. Should be called once at application
  41.     // shut-down time.
  42.  
  43. {
  44.     if (CTCPDriver::gTCPDriver)
  45.         (CTCPDriver::gTCPDriver)->Dispose();
  46. }
  47.  
  48.  
  49. // -- processing network events --
  50.  
  51. //***********************************************************
  52.  
  53. void UTurboTCP::ProcessNetEvents            // static method
  54.     (short    maxEventsToProcess,        // maximum # of TCP events to process this time
  55.     long        maxTicks,                    // maximum ticks to spend in this loop
  56.                                     //    positive values:    use value “as is”
  57.                                     //    zero:            use intelligent default
  58.                                     //    negative value:        use value with compensation
  59.                                     //                       for background activities
  60.     Boolean    inBackground)                // true if in background
  61.  
  62.     // Call once at every event-loop (or frequent non-interrupt-level) to handle processing
  63.     // of TCP completions and notifications.
  64.  
  65. {
  66.     static long    lastTickCount = 0;            // tick count last time through routine
  67.     long            stopTickCount;                // tick count to stop processing events
  68.     long            defaultNetTicks;            // how long are we typically in net event loop
  69.     long            defaultNetWait;                // how long to delay before each net event loop
  70.     
  71.  
  72.     // if no TCP driver, quit now
  73.  
  74.     if (!CTCPDriver::gTCPDriver)
  75.         return;
  76.  
  77.  
  78.     // decide how long we can go through net event loop
  79.  
  80.     if (maxEventsToProcess < 1)
  81.         maxEventsToProcess = 100;
  82.     if (lastTickCount == 0)
  83.         lastTickCount = LMGetTicks();
  84.  
  85.     if (!inBackground) {
  86.         defaultNetTicks = 45;                            // spend up to 3/4 second on net events
  87.         defaultNetWait = 0;                                //    if in foreground
  88.     }
  89.     else {
  90.         defaultNetTicks = 40;                            // consume less time in background
  91.         defaultNetWait = 3;                                //    and give other apps more chance to
  92.                                                     //    do meaningful work
  93.     }
  94.     
  95.     
  96.     // adjust time in net event loop to other CPU activity
  97.     
  98.     if (maxTicks < 1) {
  99.         long elapsedTicks = LMGetTicks() - lastTickCount;        // long time, no see?
  100.  
  101.         if (elapsedTicks < defaultNetWait)                    // ensure that other apps can do meaningful
  102.             return;                                    //    stuff before we hog the CPU again
  103.  
  104.         if (maxTicks < 0)                                // negative maxTicks values will override
  105.             defaultNetTicks = -maxTicks;                    //    earlier decision of how much time
  106.  
  107.         maxTicks = defaultNetTicks - elapsedTicks;            // reduce time if CPU is otherwise busy
  108.  
  109.         if (maxTicks < 2)                                // make sure we get at least a little time
  110.             maxTicks = 2;
  111.         if (maxTicks > defaultNetTicks)                        // not sure how this would happen, but …
  112.             maxTicks = defaultNetTicks;
  113.     }
  114.     
  115.     
  116.     // record the current time
  117.     
  118.     lastTickCount = LMGetTicks();
  119.     stopTickCount = lastTickCount + maxTicks;
  120.  
  121.  
  122.     // loop through async event queue (until queue is empty or time is up)
  123.     
  124.     while ((maxEventsToProcess-- > 0) && (LMGetTicks() < stopTickCount)) {
  125.         if (!(CTCPDriver::gTCPDriver)->ProcessOneNetEvent())
  126.             break;                        // no more events to process
  127.     }
  128.     
  129.     
  130.     // rerecord current time (in case net event processing was incredibly slow)
  131.  
  132.     lastTickCount = LMGetTicks();
  133.  
  134. }
  135.  
  136.  
  137. // -- IP address utilities --
  138.  
  139. //***********************************************************
  140.  
  141. unsigned long UTurboTCP::GetLocalIPAddr()            // static method
  142.  
  143.     // Return the local machine’s IP address.
  144.  
  145. {
  146.     if (CTCPDriver::gTCPDriver)
  147.         return (CTCPDriver::gTCPDriver)->GetIPAddr();
  148.     else
  149.         return 0L;
  150. }
  151.  
  152.  
  153. //***********************************************************
  154.  
  155. void UTurboTCP::DoAddrToStr            // static method
  156.     (unsigned long    theIPaddr,        // the IP address to convert
  157.     char*            theString)        // pointer to a buffer for the string (16 chars)
  158.  
  159.     // Convert an IP address to dotted decimal format.
  160.  
  161. {
  162.     CTCPResolverCall::DoAddrToStr((ip_addr) theIPaddr, theString);
  163. }
  164.