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 / CTCPEndpoint.h < prev    next >
Encoding:
Text File  |  1995-01-18  |  8.3 KB  |  269 lines  |  [TEXT/MMCC]

  1. //
  2. // CTCPEndpoint.h
  3. //
  4. //    TurboTCP library
  5. //    TCP generic protocol interpreter
  6. //
  7. //    Copyright © 1993-95, FrostByte Design / Eric Scouten
  8. //
  9.  
  10. #pragma once
  11.  
  12. #include "TurboTCP.buildflags.h"
  13. #include "UTurboTCP.h"
  14.  
  15. #if TurboTCP_TCL
  16.     #include "TCL.h"
  17. #endif
  18.  
  19. class CTCPStream;
  20. class CTCPResolverCall;
  21.  
  22.  
  23. //***********************************************************
  24. //
  25. // TCP precedence values (used for SetPrecedence() method)
  26. //
  27.  
  28. enum TCPPrecedence {
  29.     precRoutine = 0,
  30.     precPriority = 1,
  31.     precImmediate = 2,
  32.     precFlash = 3,
  33.     precFlashOverride = 4,
  34.     precCriticECP = 5,
  35.     precInetControl = 6,
  36.     precNetControl = 7
  37. };
  38. typedef enum TCPPrecedence TCPPrecedence;
  39.  
  40.  
  41. //***********************************************************
  42. //
  43. // ICMP report message types
  44. //
  45.  
  46. enum ICMPType {
  47.     icmpNetUnreach = 0,
  48.     icmpHostUnreach = 1,
  49.     icmpProtocolUnreach = 2,
  50.     icmpPortUnreach = 3,
  51.     icmpFragReqd = 4,
  52.     icmpSourceRouteFailed = 5,
  53.     icmpTimeExceeded = 6,
  54.     icmpParmProblem = 7,
  55.     icmpMissingOption = 8
  56. };
  57. typedef enum ICMPType ICMPType;
  58.  
  59. //***********************************************************
  60. //
  61. // TCP termination reason codes
  62. //
  63.  
  64. enum TCPTerminReason {
  65.     tcpTermRemoteAbort = 2,
  66.     tcpTermNetFailure = 3,
  67.     tcpTermSecurityMismatch = 4,
  68.     tcpTermTimeout = 5,
  69.     tcpTermAbort = 6,
  70.     tcpTermClose = 7,
  71.     tcpTermServiceFailure = 8
  72. };
  73. typedef enum TCPTerminReason TCPTerminReason;
  74.  
  75.  
  76. //***********************************************************
  77.  
  78. class CTCPEndpoint {
  79.  
  80. // This mix-in class is provided to link your TCL classes with the TurboTCP CTCPStream
  81. // class. View this as a “connection session” helper. In a typical application, this class
  82. // (or a subclass of it) would be mixed with a CDocument or CDialogDirector.
  83.  
  84. // This class does not implement any specific TCP protocol. You will need to subclass it
  85. // to provide the behaviors specific to the protocol you are implementing. You may want
  86. // to examine and use the CTelnetInterpreter class to see how this may be done.
  87.  
  88. // This class provides most of the necessary behaviors for opening and closing a session.
  89. // It provides abstract methods for receiving data and handling various TCP notifications.
  90.  
  91. // TurboTCP 1.0 users NOTE: This class replaces the CCollaborator link between CTCPStream and
  92. // CTCPSessionDoc which was existed in version 1.0. It *must* be included in your project.
  93.  
  94.     friend class CTCPStream;
  95.     friend class CTCPResolverCall;
  96.  
  97.     #if TurboTCP_TCL
  98.         TCL_DECLARE_CLASS;
  99.     #endif
  100.  
  101. public:
  102.                     CTCPEndpoint(unsigned short theDefaultPort,
  103.                         unsigned long recBufferSize = recReceiveSize,
  104.                         unsigned short autoReceiveSize = recAutoRecSize,
  105.                         unsigned short autoReceiveNum = recAutoRecNum,
  106.                         Boolean doUseCName = true);         // TurboTCP 2.0: note new parameter sequence
  107.     virtual             ~CTCPEndpoint();
  108.  
  109.     // opening/closing session
  110.  
  111.     virtual void        OpenUserHost(char* theHostName, unsigned short theDefaultPort,
  112.                         Boolean allowPortChange);
  113.     virtual void        OpenHost(unsigned long remoteHostIP, unsigned short remoteHostPort);
  114.     virtual void        Listen(unsigned long remoteHostIP, unsigned short remoteHostPort);
  115.     virtual Boolean    LocalClose(Boolean quitting);
  116.     Boolean            SessionEstablished();
  117.  
  118.     // sending data
  119.  
  120.     void                SendBfrCpy(const void* theData, unsigned short theDataSize);
  121.     void                SendBfrNoCpy(const void* theData, unsigned short theDataSize, Boolean disposeWhenDone,
  122.                         Boolean notifyWhenDone);
  123.  
  124.     void                SendChar(const char theChar);
  125.     void                SendCString(const char* theString);
  126.     void                SendPString(const unsigned char* theString);
  127.  
  128.     void                SetNextPush();
  129.     void                SetNextUrgent();
  130.  
  131.     CTCPEndpoint& operator << (CTCPEndpoint& (*_F)(CTCPEndpoint&))
  132.         { return ((*_F)(*this)); }
  133.     friend inline CTCPEndpoint& operator << (CTCPEndpoint& s, const char* v)
  134.         { s.SendCString(v); return s; }
  135.     friend inline CTCPEndpoint& operator << (CTCPEndpoint& s, const unsigned char* v)
  136.         { s.SendPString(v); return s; }
  137.     friend inline CTCPEndpoint& operator << (CTCPEndpoint& s, char v)
  138.         { s.SendChar(v); return s; }
  139.     
  140.     // configuration methods
  141.  
  142.     void                SetDefaultPort(unsigned short newDefaultPort);
  143.     void                SetLocalHostPort(unsigned short newLocalPort);
  144.     void                SetOpenTimeout(unsigned short openMaxSeconds);
  145.     void                SetListenTimeout(unsigned short openMaxSeconds);
  146.     void                SetTypeOfService(Boolean lowDelay, Boolean highThroughput, Boolean highReliability);
  147.     void                SetPrecedence(TCPPrecedence newPrecedence);
  148.     void                SetDontFragment(Boolean newDontFragment);
  149.     void                SetTimeToLive(unsigned short newTimeToLive);
  150.     void                SetSecurity(unsigned short newSecurity);
  151.     
  152.     // selectors
  153.  
  154.     void                GetRemoteHostName(char* hostStringBfr);
  155.     unsigned long        GetRemoteHostIP();
  156.     unsigned short    GetRemoteHostPort();
  157.     unsigned long        GetLocalHostIP();
  158.     unsigned short    GetLocalHostPort();
  159.     unsigned short    GetDefaultPort();
  160.  
  161.  
  162. protected:
  163.  
  164.     // state change notifications
  165.  
  166.     virtual void        RemoteClose();
  167.     virtual void        StateChanged();
  168.  
  169.     // document/window naming
  170.  
  171.     virtual void        SetWindowTitle(Str255 newTitle) {}        // Override to set the window title to the given string.
  172.     virtual void        GetFileName(Str255 theName)            // Override to return the file name of the session.
  173.                         { theName[0] = '\0'; }
  174.  
  175.     // error handling
  176.  
  177.     virtual short        TCPErrorAlert(OSErr err, long message, short alertID, short parm3);
  178.  
  179.     // TCP/DNR notification routines — called by CTCPStream and CTCPResolverCall
  180.     //    Override some or all of these methods to respond to events as needed.
  181.  
  182.     virtual void        HandleDataArrived(void* theData, unsigned short theDataSize, Boolean isUrgent) = 0;
  183.                             // ** MUST OVERRIDE **
  184.  
  185.     virtual void        HandleClosed() {}
  186.     virtual void        HandleClosing(Boolean remoteClosing);
  187.     virtual void        HandleDataSent(void* theData, unsigned short theDataSize) {}
  188.     virtual void        HandleICMP(ICMPType reportType, unsigned short optionalAddlInfo,
  189.                         void* optionalAddlInfoPtr) {}
  190.     virtual void        HandleOpened();
  191.     virtual void        HandleOpenFailed(OSErr theResultCode);
  192.     virtual void        HandleSendFailed(void* theData, unsigned short theDataSize, OSErr theResultCode) {}
  193.     virtual void        HandleTCPError(OSErr theResultCode, short theCsCode);
  194.     virtual void        HandleTerminated(TCPTerminReason terminReason, Boolean aboutToDispose);
  195.     virtual void        HandleTimeout() {}
  196.     virtual void        HandleUnexpectedData() {}
  197.     virtual void        HandleUrgentBegin() {}
  198.  
  199.     virtual void        HandleStrToAddr(struct hostInfo* theHostInfo);
  200.     virtual void        HandleAddrToName(struct hostInfo* theHostInfo);
  201.     virtual void        HandleHInfo(struct returnRec* theHInfo) {}
  202.     virtual void        HandleMXInfo(struct returnRec* theMXInfo) {}
  203.  
  204.  
  205.     // configuration fields
  206.     //    Your subclass may change these variables to customize behaviors.
  207.     
  208.     Boolean            useCName;                // get host’s canonical name
  209.     Boolean            goAwayOnClose;            // close window when session closes
  210.     Boolean            showFileName;                // show filename in window titles
  211.     Boolean            showHostName;                // show host name in window titles
  212.     short            untitledNumber;            // serial number for untitled documents
  213.  
  214.  
  215. //----------------------
  216. //    protected interface (TEMPORARY)
  217. //        WARNING: these members will become private VERY SOON!
  218. //        Do your best to get rid of references to these members!
  219. //
  220.  
  221. protected:
  222.  
  223.     // informational fields
  224.     //    DO NOT change these values!
  225.  
  226.     CTCPStream*        itsStream;                // TCP stream for this document
  227.     CTCPResolverCall*    itsResolver;                // TCP resolver for this document
  228.     Boolean            pendingOpenByName;            // OpenUserHost has been issued
  229.     unsigned short    pendingPortNumber;            // port number for OpenUserHost
  230.     Boolean            closeAndQuit;                // closing windows to quit
  231.     
  232.     char            hostCName[256];            // canonical name of host
  233.                                             //    if cnames not requested, is user’s name
  234.     unsigned long        hostAddress;                // IP address of host
  235.     unsigned short    defaultPort;                // default port number
  236.     unsigned short    actualPort;                // current port number
  237.     unsigned short    localPort;                    // default local port number
  238.  
  239.     enum {
  240.         ALRT_TCPOpenFailed = 23000,
  241.         ALRT_TCPUnexpError = 23001,
  242.         ALRT_TCPSendFailed = 23002,
  243.         ALRT_TCPTerminated = 23003,
  244.         ESTR_TerminBase = 23010,
  245.     
  246.         STR__WindowStrings = 23000,
  247.         Wstr_NoSession = 1,
  248.         Wstr_Separator = 2,
  249.         Wstr_NotReadyPrefix = 3,
  250.         Wstr_NotReadySuffix = 4,
  251.         Wstr_Untitled = 5,
  252.         Wstr_UntitledNoNumber = 6,
  253.         Wstr_PortDelimiter = 7
  254.     };
  255.  
  256. };
  257.  
  258.  
  259. //***********************************************************
  260. //
  261. // Stream-like manipulators for CTCPEndpoint
  262. //
  263.  
  264. CTCPEndpoint& endl(CTCPEndpoint& ep);
  265. CTCPEndpoint& local_IP(CTCPEndpoint& ep);
  266. CTCPEndpoint& remote_IP(CTCPEndpoint& ep);
  267. CTCPEndpoint& push(CTCPEndpoint& ep);
  268. CTCPEndpoint& urgent(CTCPEndpoint& ep);
  269.