home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-01-06 | 4.3 KB | 164 lines | [TEXT/MMCC] |
- //
- // UTurboTCP.cp
- //
- // TurboTCP library
- // Utility functions
- //
- // Copyright © 1993-95, FrostByte Design / Eric Scouten
- //
-
- #include "UTurboTCP.h"
-
- #include "CTCPDriver.h"
- #include "CTCPResolverCall.h"
-
- #if TurboTCP_TCL
- extern Boolean gInBackground; // TCL global: in background under MultiFinder
- #endif
-
-
- // -- opening/closing TCP services --
-
- //***********************************************************
-
- void UTurboTCP::InitTCP() // static method
-
- // Create a TCP driver object and initialize it. Should be called once at application startup.
- // In a future version of TurboTCP, this routine will check for MacTCP and/or Open Transport
- // and open the appropriate object.
-
- {
- if (!CTCPDriver::gTCPDriver)
- new CTCPDriver;
- }
-
-
- //***********************************************************
-
- void UTurboTCP::CloseTCP() // static method/
-
- // Close the TCP driver object and destroy it. Should be called once at application
- // shut-down time.
-
- {
- if (CTCPDriver::gTCPDriver)
- (CTCPDriver::gTCPDriver)->Dispose();
- }
-
-
- // -- processing network events --
-
- //***********************************************************
-
- void UTurboTCP::ProcessNetEvents // static method
- (short maxEventsToProcess, // maximum # of TCP events to process this time
- long maxTicks, // maximum ticks to spend in this loop
- // positive values: use value “as is”
- // zero: use intelligent default
- // negative value: use value with compensation
- // for background activities
- Boolean inBackground) // true if in background
-
- // Call once at every event-loop (or frequent non-interrupt-level) to handle processing
- // of TCP completions and notifications.
-
- {
- static long lastTickCount = 0; // tick count last time through routine
- long stopTickCount; // tick count to stop processing events
- long defaultNetTicks; // how long are we typically in net event loop
- long defaultNetWait; // how long to delay before each net event loop
-
-
- // if no TCP driver, quit now
-
- if (!CTCPDriver::gTCPDriver)
- return;
-
-
- // decide how long we can go through net event loop
-
- if (maxEventsToProcess < 1)
- maxEventsToProcess = 100;
- if (lastTickCount == 0)
- lastTickCount = LMGetTicks();
-
- if (!inBackground) {
- defaultNetTicks = 45; // spend up to 3/4 second on net events
- defaultNetWait = 0; // if in foreground
- }
- else {
- defaultNetTicks = 40; // consume less time in background
- defaultNetWait = 3; // and give other apps more chance to
- // do meaningful work
- }
-
-
- // adjust time in net event loop to other CPU activity
-
- if (maxTicks < 1) {
- long elapsedTicks = LMGetTicks() - lastTickCount; // long time, no see?
-
- if (elapsedTicks < defaultNetWait) // ensure that other apps can do meaningful
- return; // stuff before we hog the CPU again
-
- if (maxTicks < 0) // negative maxTicks values will override
- defaultNetTicks = -maxTicks; // earlier decision of how much time
-
- maxTicks = defaultNetTicks - elapsedTicks; // reduce time if CPU is otherwise busy
-
- if (maxTicks < 2) // make sure we get at least a little time
- maxTicks = 2;
- if (maxTicks > defaultNetTicks) // not sure how this would happen, but …
- maxTicks = defaultNetTicks;
- }
-
-
- // record the current time
-
- lastTickCount = LMGetTicks();
- stopTickCount = lastTickCount + maxTicks;
-
-
- // loop through async event queue (until queue is empty or time is up)
-
- while ((maxEventsToProcess-- > 0) && (LMGetTicks() < stopTickCount)) {
- if (!(CTCPDriver::gTCPDriver)->ProcessOneNetEvent())
- break; // no more events to process
- }
-
-
- // rerecord current time (in case net event processing was incredibly slow)
-
- lastTickCount = LMGetTicks();
-
- }
-
-
- // -- IP address utilities --
-
- //***********************************************************
-
- unsigned long UTurboTCP::GetLocalIPAddr() // static method
-
- // Return the local machine’s IP address.
-
- {
- if (CTCPDriver::gTCPDriver)
- return (CTCPDriver::gTCPDriver)->GetIPAddr();
- else
- return 0L;
- }
-
-
- //***********************************************************
-
- void UTurboTCP::DoAddrToStr // static method
- (unsigned long theIPaddr, // the IP address to convert
- char* theString) // pointer to a buffer for the string (16 chars)
-
- // Convert an IP address to dotted decimal format.
-
- {
- CTCPResolverCall::DoAddrToStr((ip_addr) theIPaddr, theString);
- }
-