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 / CTCPStream.h < prev    next >
Encoding:
Text File  |  1995-01-04  |  10.1 KB  |  270 lines  |  [TEXT/MMCC]

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