home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / turbo-tcp-20b4-cpp.hqx / TurboTCP / TurboTCP source / CTCPStream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-11  |  10.2 KB  |  275 lines

  1. /*
  2. ** CTCPStream.h
  3. **
  4. **    TurboTCP support library
  5. **    TCP stream class
  6. **
  7. **    Copyright © 1993-94, FrostByte Design / Eric Scouten
  8. **
  9. */
  10.  
  11.  
  12. #pragma once
  13.  
  14. #include "TCL.h"
  15. #include <MacTCPCommonTypes.h>
  16. #include <TCPPB.h>
  17.  
  18. #include "CTCPDriver.h"
  19. #include "CTCPEndpoint.h"
  20.  
  21. class CTCPAsyncCall;
  22.  
  23. #ifndef TCL_NO_TEMPLATES
  24.     class CTCPAsyncCallList;
  25.     class CTCPStreamList;
  26. #else
  27.     class CPtrArray_CTCPAsyncCall;
  28.     class CPtrArray_CTCPStream;
  29.     #define CTCPAsyncCallList CPtrArray_CTCPAsyncCall
  30.     #define CTCPStreamList CPtrArray_CTCPStream
  31. #endif
  32.  
  33.  
  34. // connection state codes for TCP Status call
  35.  
  36. enum {
  37.     strClosed = 0,
  38.     strListen = 2,
  39.     strSYNReceived = 4,
  40.     strSYNSent = 6,
  41.     strEstablished = 8,
  42.     strFINWait1 = 10,
  43.     strFINWait2 = 12,
  44.     strCloseWait = 14,
  45.     strClosing = 16,
  46.     strLastAck = 18,
  47.     strTimeWait = 20
  48. };
  49.  
  50.  
  51. // miscellaneous constants
  52.  
  53. #define autoReceiveMax        32                    // maximum # of entries in RDS
  54. #define IPOptionStringSize    40
  55. typedef char IPOptionString[IPOptionStringSize];
  56.  
  57.  
  58. /*______________________________________________________________________
  59. **
  60. ** CTCPStream
  61. **
  62. **    This class implements a stream for MacTCP. A TCP stream supports one connection at
  63. **    a time; but a connection may be closed and another connection opened without releasing
  64. **    the stream.
  65. **
  66. **    All MacTCP commands are originated through this class. (See the methods labelled
  67. **    “basic user TCP calls” below.) These methods originate asynchronous calls to the
  68. **    MacTCP driver by creating CTCPAsyncCall objects to correspond with each call. When
  69. **    the call is completed, notification is passed back to the caller through the Handle…
  70. **    methods in CTCPProtcolInterp.
  71. **
  72. **    This object also receives notification of asynchronous events related to the stream and
  73. **    passes these notifications to its dependents through the BroadcastChange mechanism.
  74. **    (See the description of the asynchronous notification routine [ASR] in the MacTCP
  75. **    manuals.)
  76. **
  77. **    Your application class need not be concerned with interrupt-level constraints on
  78. **    memory management. The CTCPStream object queues notifications received at interrupt
  79. **    level and the CTCPDriver schedules their processing at the next event loop.
  80. **
  81. **    TurboTCP 1.0 NOTE: This class is no longer a descendant of CCollaborator. It now
  82. **    communicates only with the CTCPEndpoint mix-in class.
  83. **
  84. */
  85.  
  86. class CTCPStream TCL_AUTO_DESTRUCT_OBJECT {
  87.  
  88.     friend class CTCPAsyncCall;
  89.     friend class CTCPDriver;
  90.     friend class CTCPEndpoint;
  91.     friend class CTCPStreamList;
  92.  
  93.     TCL_DECLARE_CLASS
  94.  
  95. public:
  96.     ip_addr            itsRemoteIP;                // remote host’s IP address
  97.     b_16                itsRemotePort;                // remote host’s TCP port
  98.     ip_addr            itsLocalIP;                // local host’s IP address
  99.     b_16                itsLocalPort;                // local host’s TCP port
  100.     Boolean            rcvUrgent;                // stream is receiving urgent data
  101.     Boolean            hasSessionOpen;            // currently open session
  102.  
  103. private:
  104.     CTCPEndpoint*        itsEndpoint;                // endpoint object for this stream
  105.     CTCPAsyncCallList* itsAsyncCalls;                // list of outstanding async calls
  106.     Ptr                macTCPStream;            // MacTCP’s reference code for this stream
  107.     Handle            itsBuffer;                    // receive buffer area
  108.     long                itsBufferSize;                // size of receive buffer
  109.  
  110.     b_16                sendNextUrgent;            // next send operation is “urgent” data
  111.     Boolean             sendNextPush;                // next send operation is “push” data
  112.  
  113.     Boolean            notifClosing;                // ASR got closing event
  114.     Boolean             remoteClose;                // ASR close due to remote host
  115.     Boolean            notifTimeout;                // ASR got ULP timeout event
  116.     Boolean             notifTerminate;                // ASR got terminate event
  117.     b_16                notifTermReason;            // ASR’s termination reason
  118.     Boolean            notifDataArrived;            // ASR got data w/ no receive
  119.     Boolean            notifUrgent;                // ASR got urgent data event
  120.     Boolean            notifICMP;                // ASR got ICMP report
  121.     ICMPReport        notifICMPreport;            // most recent ICMP report
  122.     
  123.     byte                itsULPtimeout;                // ULP timeout value in seconds
  124.     byte                itsULPaction;                // what to do on ULP timeout
  125.     byte                itsValidityFlag;                // validity bits for optional parms
  126.     byte                itsCommandTimeoutValue;        // command timeout value in seconds
  127.     byte                itsTosFlags;                // type of service flags
  128.     byte                itsPrecedence;                // precedence level
  129.     byte                itsDontFrag;                // don’t fragment flag
  130.     byte                itsTimeToLive;                // time to live
  131.     byte                itsSecurity;                // security flag
  132.     byte                itsOptionCnt;                // count of options bytes
  133.     byte                itsOptions[IPOptionStringSize];    // IP options
  134.     
  135.     Boolean             pendingOpen;                // session is being opened
  136.     Boolean             pendingClose;                // session is being closed
  137.     Boolean             pendingAbort;                // session is being aborted
  138.     Boolean            pendingNotify;                // stream is in ProcessNotify queue
  139.     Boolean             pendingDispose;                // stream is in ProcessDispose queue
  140.     Boolean             disposeOnTerminate;            // dispose stream when terminate notification comes
  141.     Boolean            receivedClose;                // received notification that session was closed or aborted
  142.     Boolean            receivedTerminate;            // terminate notification has been received
  143.     
  144.     short            itsAutoReceiveSize;            // auto-receive size (# of entries)
  145.     short            itsAutoReceiveNum;            // number of simultaneous receive calls to issue
  146.     TurboTCPQElem        qNotifyEntry;                // notification queue entry
  147.     TurboTCPQElem        qDisposeEntry;                // disposal queue entry
  148.  
  149. #if USESROUTINEDESCRIPTORS
  150.     static UniversalProcPtr notifyProcUPP;            // UPP for asynchronous notification proc
  151. #endif
  152.  
  153.  
  154.     // constructor/destructor
  155.  
  156. private:
  157.                     CTCPStream(CTCPEndpoint& theEndpoint,
  158.                         long recBufferSize = recReceiveSize,
  159.                         short autoReceiveSize = recAutoRecSize,
  160.                         short autoReceiveNum = recAutoRecNum);
  161.     virtual            ~CTCPStream()    ;                    // use Dispose() instead, DO NOT OVERRIDE
  162. public:                
  163.     void                Dispose();
  164.  
  165.     
  166.     // basic user TCP calls
  167.  
  168.     void                OpenConnection(Boolean passive, ip_addr theRemoteIP, b_16 theRemotePort,
  169.                         b_16 theLocalPort);
  170.     void                Close();
  171.     void                Abort();
  172.  
  173.     void                NoCopyRcv(rdsEntry* itsRDS, b_16 itsRDSSize, b_16 itsTimeOut);
  174.     void                BfrReturn(Ptr itsRDS);
  175.     void                Send(wdsEntry* itsWDS, b_16 itsTimeOut, Boolean disposeWDS,
  176.                         Boolean notifyWhenDone);
  177.     void                Receive(Ptr theData, b_16 itsDataSize, b_16 itsTimeOut);
  178.     void                Status(TCPStatusPB* theStatusBlock);
  179.     b_16                ConnectionState();
  180.     inline Boolean        RcvUrgentStatus()
  181.                         { return rcvUrgent; }
  182.  
  183.  
  184.     // specialized functions for sending data
  185.  
  186.     void                SendBfrCpy(const void* theData, unsigned short theDataSize);
  187.     void                SendBfrNoCpy(const void* theData, unsigned short theDataSize,
  188.                         Boolean disposeWhenDone, Boolean notifyWhenDone);
  189.     inline void            SetNextPush()
  190.                         { sendNextPush = TRUE; }
  191.     inline void            SetNextUrgent(Boolean useRFC793)
  192.                         { sendNextUrgent = (useRFC793 ? 2 : 1); }
  193.     
  194.  
  195.     // set configuration — use these before opening a connection
  196.     
  197.     inline void            SetULPTimeoutValue(b_16 ulpTimeoutValue)
  198.                         { itsULPtimeout = ulpTimeoutValue; itsValidityFlag |= timeoutValue; }
  199.     inline void            SetULPTimeoutAction(b_16 ulpTimeoutAction)
  200.                         { itsULPaction = ulpTimeoutAction; itsValidityFlag |= timeoutAction; }
  201.     inline void            SetCommandTimeout(b_16 cmdTimeoutValue)
  202.                         { itsCommandTimeoutValue = cmdTimeoutValue; }
  203.     inline void            SetTypeOfService(b_16 newTosFlag)
  204.                         { itsTosFlags = newTosFlag; itsValidityFlag |= typeOfService; }
  205.     inline void            SetPrecedence(b_16 newPrecedence)
  206.                         { itsPrecedence = newPrecedence; itsValidityFlag |= precedence; }
  207.     inline void            SetDontFrag(b_16 newDontFrag)
  208.                         { itsDontFrag = newDontFrag; }
  209.     inline void            SetTimeToLive(b_16 newTimeToLive)
  210.                         { itsTimeToLive = newTimeToLive; }
  211.     inline void            SetSecurity(b_16 newSecurity)
  212.                         { itsSecurity = newSecurity; }
  213.     inline void            SetIPOptions(b_16 newOptionsSize, IPOptionString* newOptions)
  214.                         { itsOptionCnt = newOptionsSize; BlockMove(newOptions, &itsOptions, IPOptionStringSize); }
  215.     
  216.  
  217.     // notification routines — used by CTCPAsyncCall and CTCPStream *only*
  218.  
  219. private:
  220.     inline void            HandleTCPError(OSErr theResultCode, short theCsCode)
  221.                         { if (itsEndpoint) itsEndpoint->HandleTCPError(theResultCode, theCsCode); }
  222.     inline void            HandleClosed()
  223.                         { if (itsEndpoint) itsEndpoint->HandleClosed(); }
  224.     inline void            HandleClosing(Boolean remoteClosing)
  225.                         { if (itsEndpoint) itsEndpoint->HandleClosing(remoteClosing); }
  226.     inline void            HandleDataArrived(Ptr theData, b_16 theDataSize, Boolean isUrgent)
  227.                         { if (itsEndpoint) itsEndpoint->HandleDataArrived(theData, theDataSize, isUrgent); }
  228.     void                HandleDataSent(wdsEntry* WDSPtr, Boolean disposeWhenDone, Boolean notifyWhenDone);
  229.     inline void            HandleICMP(struct ICMPReport* icmpMsg)
  230.                         { if (itsEndpoint) itsEndpoint->HandleICMP((ICMPType) icmpMsg->reportType,
  231.                             icmpMsg->optionalAddlInfo, (void*) (icmpMsg->optionalAddlInfoPtr)); }
  232.     inline void            HandleOpened()
  233.                         { hasSessionOpen = TRUE; pendingOpen = FALSE; if (itsEndpoint) itsEndpoint->HandleOpened(); }
  234.     inline void            HandleOpenFailed(OSErr theResultCode)
  235.                         { hasSessionOpen = FALSE; pendingOpen = FALSE;
  236.                         if (itsEndpoint) itsEndpoint->HandleOpenFailed(theResultCode); }
  237.     void                HandleSendFailed(wdsEntry* WDSPtr, Boolean disposeWhenDone,
  238.                         Boolean notifyWhenDone, OSErr theResultCode);
  239.     inline void            HandleTerminated(b_16 terminReason)
  240.                         { if (itsEndpoint) itsEndpoint->HandleTerminated((TCPTerminReason) terminReason,
  241.                             pendingDispose || disposeOnTerminate); }
  242.     inline void            HandleTimeout()
  243.                         { if (itsEndpoint) itsEndpoint->HandleTimeout(); }
  244.     inline void            HandleUnexpectedData()
  245.                         { if (itsEndpoint) itsEndpoint->HandleUnexpectedData(); }
  246.     inline void            HandleUrgentBegin()
  247.                         { if (itsEndpoint) itsEndpoint->HandleUrgentBegin(); }
  248.     
  249.     inline void            RcvUrgentBegin()
  250.                         { rcvUrgent = TRUE; }
  251.     inline void            RcvUrgentMark()
  252.                         { rcvUrgent = FALSE; }
  253.  
  254.  
  255.     // private methods for initiating TCP calls
  256.  
  257.     OSErr            DoAsyncCall(b_16 theCsCode, TCPiopb* theParamBlock);
  258.     OSErr            DoSyncCall(b_16 theCsCode, TCPiopb* theParamBlock);
  259.     void                StartAutoReceive()
  260.                         { for (short i=1; i<=itsAutoReceiveNum; i++) IssueAutoReceive(); }
  261.     void                IssueAutoReceive();
  262.     void                ProcessNotify();
  263.     void                ProcessAsyncCompletion(CTCPAsyncCall* theCall);
  264.  
  265.  
  266.     // interrupt-level methods: delay processing for non-interrupt status
  267.  
  268.     void                PostponeDispose();
  269.     void                PostponeNotify(b_16 eventCode, b_16 terminReason, struct ICMPReport* icmpMsg);
  270.     static pascal void    NotifyProc(StreamPtr tcpStream, unsigned short eventCode,
  271.                             Ptr userDataPtr, unsigned short terminReason,
  272.                             struct ICMPReport* icmpMsg);
  273.  
  274. };
  275.