home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / WSKSOCKM.CPP < prev   
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.5 KB  |  116 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.11  $
  6. //
  7. // Winsock for OWL subsystem.
  8. // Based on work by Paul Pedriana, 70541.3223@compuserve.com
  9. //----------------------------------------------------------------------------
  10. #include <owl/pch.h>
  11. #if !defined(OWL_WINSOCK_H)
  12. # include <owl/winsock.h>
  13. #endif
  14.  
  15. OWL_DIAGINFO;
  16.  
  17. //
  18. // Default constructor. Initializes all data members to 0.
  19. //
  20. TSocketInfo::TSocketInfo()
  21. {
  22.   wVersion = 0;
  23.   wHighVersion = 0;
  24.   szDescription[0] = 0;
  25.   szSystemStatus[0] = 0;
  26.   iMaxSockets = 0;
  27.   iMaxUdpDg = 0;
  28.   lpVendorInfo = 0;
  29. }
  30.  
  31. //----------------------------------------------------------------------------
  32.  
  33. //
  34. // The SocketManager constructor takes a major and minor version as parameters.
  35. //  These version parameters are the Winsock versions that you request, as
  36. //  in WSAStartup() calls.
  37. //
  38. TSocketManager::TSocketManager(short versionMajor, short versionMinor, bool autoStartup)
  39. {
  40.   TWinSock::IsAvailable();  
  41.  
  42.   StartupCount = 0;
  43.   Init(versionMajor, versionMinor);
  44.   if (autoStartup)
  45.      Startup();
  46. }
  47.  
  48. //
  49. // The TSocketManager destructor cleans up after itself.  For every time you
  50. //  called Startup() (same as WSAStartup()), this destructor will call
  51. //  ShutDown (same as WSACleanup()).
  52. //
  53. TSocketManager::~TSocketManager()
  54. {
  55.   while (StartupCount)
  56.     ShutDown();  // Call ShutDown() enough times to equal times Startup() was called.
  57. }
  58.  
  59. //
  60. // The ITSocketManager function does some ititialization for the TSocketManager.
  61. //  This function is separate from the constructor so that you can call it at
  62. //  any time in order to re-specify the desired major and minor versions before
  63. //  a call to Startup().
  64. // nNewVersionMajor and nNewVersionMinor will be the values specified in the
  65. //  Winsock WSAStartup() call that TSocketManager::Startup() makes.
  66. // The return value is TRUE or FALSE.
  67. //
  68. void TSocketManager::Init(short versionMajor, short versionMinor)
  69. {
  70.   LastError    = 0;
  71.   VersionMajor = versionMajor;
  72.   VersionMinor = versionMinor;
  73. }
  74.  
  75. //
  76. // The Startup() function is equivalent to the Winsock WSAStartup() function.
  77. //
  78. // This function fills out the TSocketManager's SocketInfo structure (same as a
  79. //  WSAData structure) with the return information.
  80. //
  81. // You can then call the GetMaxSocketsAvailable(), GetVendorInfo(), Available(),
  82. //  GetMajorVersion(), GetMinorVersion(), or Information() functions to get the
  83. //  results of the call.
  84. //
  85. int TSocketManager::Startup()
  86. {
  87.    uint16 packedValues = (uint16)(((uint16)VersionMinor) << 8) |
  88.                          (uint16)VersionMajor;
  89.    LastError = TWinSockDll::WSAStartup(packedValues, &SocketInfo);
  90.    if (LastError)
  91.      return WINSOCK_ERROR;
  92.  
  93.    StartupCount++;
  94.    VersionMajor = HiUint8(SocketInfo.wVersion);
  95.    VersionMinor = LoUint8(SocketInfo.wVersion);
  96.  
  97.    return WINSOCK_NOERROR;
  98. }
  99.  
  100. //
  101. // The ShutDown() function is the equivalent to the Winsock WSACleanup() function.
  102. // You can call this function if you want, but in its destructor, the TSocketManager
  103. //  will automatically call it once for every time Startup() was called.  Of course,
  104. //  the TSocketManager cannot know about any independent WSAStartup() direct calls
  105. //  that you may make.
  106. //
  107. int TSocketManager::ShutDown()
  108. {
  109.   if (TWinSockDll::WSACleanup()) {
  110.     LastError = TWinSockDll::WSAGetLastError();
  111.     return WINSOCK_ERROR;
  112.   }
  113.   StartupCount--;
  114.   return WINSOCK_NOERROR;
  115. }
  116.