home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / comm / news102.sit / NewsWatcher / source / tcphi.c < prev    next >
Text File  |  1991-04-03  |  8KB  |  316 lines

  1. /*----------------------------------------------------------
  2. #
  3. #    NewsWatcher    - Macintosh NNTP Client Application
  4. #
  5. #    Written by Steven Falkenburg
  6. #    ⌐1990 Apple Computer, Inc.
  7. #
  8. #-----------------------------------------------------------
  9. #
  10. #    TCPHi.c
  11. #
  12. #    This module contains high-level routines for setting
  13. #    up connections using MacTCP and sending/receiving data
  14. #    on these connections.
  15. #
  16. #-----------------------------------------------------------*/
  17.  
  18. #pragma segment lowlevel
  19.  
  20. #include "compat.h"
  21. #include <string.h>
  22.  
  23. #ifdef PROTOS
  24. #include <Types.h>
  25. #include <Memory.h>
  26. #include <Dialogs.h>
  27. #include <Resources.h>
  28. #include <CursorCtl.h>
  29. #include <Strings.h>
  30. #include <Lists.h>
  31. #endif
  32.  
  33. #include "MacTCPCommonTypes.h"
  34. #include "GetMyIPAddr.h"
  35. #include "TCPPB.h"
  36. #include "TCPRoutines.h"
  37. #include "TCPHi.h"
  38. #include "nntp.h"
  39. #include "miscstuff.h"
  40.  
  41. #ifndef THINK_C
  42. acurHandle gNetAcur;        /* network animated cursor handle */
  43. #else
  44. Handle gNetAcur;
  45. #endif
  46.  
  47.  
  48.  
  49. /*    InitNetCursor loads the network animated cursor, depicting network traffic
  50. */
  51.  
  52. void InitNetCursor(void)
  53. {
  54. #ifndef THINK_C
  55.     gNetAcur = (acurHandle) GetResource('acur',128);
  56.     InitCursorCtl(gNetAcur);
  57. #endif
  58. }
  59.  
  60.  
  61. /*    TrashNetCursor disposes the network cursor allowing the spinning beach
  62.     ball to be used.
  63. */
  64.  
  65. void TrashNetCursor(void)
  66. {
  67. #ifndef THINK_C
  68.     InitCursorCtl(nil);
  69.     ReleaseResource((Handle)gNetAcur);
  70. #endif
  71. }
  72.  
  73.  
  74. /* InitNetwork opens the network driver
  75. */
  76.  
  77. OSErr InitNetwork(void)
  78. {
  79.     return OpenTCPDriver();
  80. }
  81.  
  82.  
  83. /* CreateStream() creates an unconnected network stream to be
  84.    used later by OpenConnection.  The length of the receive
  85.    buffer must be specified in the call */
  86.    
  87. OSErr CreateStream(unsigned long *stream,unsigned long recvLen)
  88. {
  89.     Ptr recvPtr;
  90.     OSErr err;
  91.     
  92.     recvPtr = MyNewPtr(recvLen);
  93.     if ((err = MyMemErr())==noErr)
  94.         err = LowTCPCreateStream(stream,recvPtr,recvLen,(TCPNotifyProc)nil);
  95.     return err;
  96. }
  97.  
  98.  
  99. /* OpenConnection() initiates a connection to a remote machine,
  100.    given that machine's network number and connection port.  A timeout
  101.    value for the call must be given, and the stream identifier is returned. */
  102.  
  103. OSErr OpenConnection(unsigned long stream,long remoteHost,short remotePort,Byte timeout)
  104. {
  105.     ip_addr localHost;
  106.     tcp_port localPort = 0;
  107.     
  108.     return LowTCPOpenConnection(stream,timeout,remoteHost,remotePort,&localHost,
  109.                                 &localPort);
  110. }
  111.  
  112.  
  113. /* WaitForConnection() listens for a connection on a particular port from a
  114.     particular host.  It returns when a connection has been established */
  115.  
  116. OSErr WaitForConnection(unsigned long stream,Byte timeout,short localPort,
  117.                         long *remoteHost,short *remotePort)
  118. {
  119.     ip_addr localHost;
  120.     
  121.     return LowTCPWaitForConnection(stream,timeout,(ip_addr *)remoteHost,
  122.                 (tcp_port *)remotePort,&localHost,(tcp_port *)&localPort,false,nil);
  123. }
  124.  
  125.  
  126. /* AsyncWaitForConnection() listens for a connection on a particular port from a
  127.     particular host.  It is executed asynchronously and returns immediately */
  128.  
  129. void AsyncWaitForConnection(unsigned long stream,Byte timeout,short localPort,
  130.                 long remoteHost,short remotePort,TCPiopb **returnBlock)
  131. {
  132.     ip_addr localHost;
  133.     
  134.     LowTCPWaitForConnection(stream,timeout,(ip_addr *)&remoteHost,
  135.                 (tcp_port *)&remotePort,&localHost,(tcp_port *)&localPort,true,returnBlock);
  136. }
  137.  
  138.  
  139. /* AsyncGetConnectionData() should be called when a call to AsyncWaitForConnection
  140.     completes (when returnBlock->ioResult <= 0).  This call retrieves the information
  141.     about this new connection and disposes the parameter block. */
  142.     
  143. OSErr AsyncGetConnectionData(TCPiopb *returnBlock,long *remoteHost,short *remotePort)
  144. {
  145.     ip_addr localHost;
  146.     tcp_port localPort;
  147.     
  148.     return LowFinishTCPWaitForConn(returnBlock,(ip_addr *)remoteHost,
  149.                         (tcp_port *)remotePort,&localHost,&localPort);
  150. }
  151.  
  152.  
  153. /* CloseConnection() terminates a connection to a remote host, given the
  154.    stream identifier of the connection */
  155.    
  156. OSErr CloseConnection(unsigned long stream)
  157. {
  158.     return LowTCPClose(stream,10);
  159. }
  160.  
  161.  
  162. /* AbortConnection() aborts a connection to a remote host, given the
  163.    stream identifier of the connection */
  164.    
  165. OSErr AbortConnection(unsigned long stream)
  166. {
  167.     return LowTCPAbort(stream);
  168. }
  169.     
  170.  
  171. /* ReleaseStream() frees the allocated buffer space for a given connection
  172.    stream.  This call should be made after CloseConnection. */
  173.    
  174. OSErr ReleaseStream(unsigned long stream)
  175. {
  176.     OSErr err;
  177.     Ptr recvPtr;
  178.     unsigned long recvLen;
  179.     
  180.     if ((err = LowTCPRelease(stream,&recvPtr,&recvLen)) == noErr)
  181.             MyDisposPtr(recvPtr);
  182.     
  183.     return err;
  184. }
  185.  
  186.  
  187. /* SendData() sends data along a connection stream to a remote host. */
  188.  
  189. OSErr SendData(unsigned long stream,Ptr data,unsigned short length,Boolean retry)
  190. {    
  191.     OSErr err;
  192.     struct wdsEntry myWDS[2];    /* global write data structure */
  193.  
  194.  
  195.     InitNetCursor();
  196.     myWDS[0].length = length;
  197.     myWDS[0].ptr = data;
  198.     myWDS[1].length = 0;
  199.     myWDS[1].ptr = nil;
  200.     do
  201.         err = LowTCPSendData(stream,20,false,false,(Ptr) myWDS,false,nil);
  202.     while (retry && err==commandTimeout);
  203.     TrashNetCursor();
  204.     return err;
  205. }
  206.  
  207. /* SendMultiData() is similar to SendData, but takes an array of strings to send
  208.    to the remote host. */
  209.  
  210. OSErr SendMultiData(unsigned long stream,Str255 data[],short numData,Boolean retry)
  211. {
  212.     struct wdsEntry *theWDS;
  213.     short i;
  214.     OSErr err;
  215.     
  216.     InitNetCursor();
  217.     theWDS = (wdsEntry *)NewPtr((numData+1) * sizeof(wdsEntry));
  218.     if (MemError())
  219.         return MemError();    
  220.     theWDS[numData].length = 0;
  221.     theWDS[numData].ptr = nil;
  222.     for (i=0; i<numData; i++) {
  223.         theWDS[i].ptr = data[i];
  224.         theWDS[i].length = strlen((char *)data[i]);
  225.     }
  226.     do
  227.         err = LowTCPSendData(stream,20,false,false,(Ptr) theWDS,false,nil);
  228.     while (retry && err==commandTimeout);
  229.     MyDisposPtr((Ptr)theWDS);
  230.     TrashNetCursor();
  231.     return err;
  232. }
  233.  
  234.  
  235. /* SendDataAsync() sends data to a remote host asynchronously.  The ioResult
  236.    parameter block variable should be checked, and SendDataDone() called when
  237.    this flag is zero or negative */
  238.  
  239. void SendDataAsync(unsigned long stream, Ptr data,unsigned short length,TCPiopb **returnBlock)
  240. {    
  241.     struct wdsEntry *theWDS;
  242.  
  243.     InitNetCursor();
  244.     theWDS = (wdsEntry *)NewPtr( (2*sizeof(wdsEntry)) );
  245.     theWDS[0].length = length;
  246.     theWDS[0].ptr = data;
  247.     theWDS[1].length = 0;
  248.     theWDS[1].ptr = 0;
  249.     LowTCPSendData(stream,20,false,false,(Ptr) theWDS,true,returnBlock);
  250.     TrashNetCursor();
  251. }
  252.  
  253.  
  254. /* SendDataDone() should be called in response to the completion of a SendDataAsync
  255.    call.  It returns any error which occurred in the send. */
  256.  
  257. OSErr SendAsyncDone(TCPiopb *returnBlock)
  258. {
  259.     MyDisposPtr((Ptr)returnBlock->csParam.send.wdsPtr);
  260.     return LowFinishTCPSend(returnBlock);
  261. }
  262.  
  263.  
  264. /* RecvData() waits for data to be received on a connection stream.  When data
  265.    arrives, it is copied into the data buffer and the call terminates. */
  266.  
  267. OSErr RecvData(unsigned long stream,Ptr data,unsigned short *length,Boolean retry)
  268. {
  269.     Boolean    urgent,mark;
  270.     OSErr    err;
  271.     unsigned short recvLength;
  272.  
  273.     InitNetCursor();
  274.     do {
  275.         recvLength = *length;
  276.         err = LowTCPRecvData(stream,20,&urgent,&mark,data,&recvLength,false,nil);
  277.     }
  278.     while (retry && err==commandTimeout);
  279.     *length = recvLength;
  280.     if (err == noErr) {
  281.         *(data+*length) = '\0';
  282.     }
  283.     TrashNetCursor();
  284.     return err;
  285. }
  286.  
  287.  
  288. /* RecvDataAsync() is identical to RecvData above, but in this case, the call is
  289.    made asynchronously. */
  290.  
  291. void RecvDataAsync(unsigned long stream,Ptr data,unsigned short length,TCPiopb **returnBlock)
  292. {
  293.     Boolean urgent,mark;
  294.     
  295.     LowTCPRecvData(stream,20,&urgent,&mark,data,&length,true,returnBlock);
  296. }
  297.  
  298.  
  299. /* GetDataLength() should be called in response to the completion of the
  300.    RecvDataAsync call. */
  301.  
  302. OSErr GetDataLength(TCPiopb *returnBlock,unsigned short *length)
  303. {
  304.     Boolean urgent,mark;
  305.     
  306.     return LowFinishTCPRecv(returnBlock,&urgent,&mark,length);
  307. }
  308.  
  309.  
  310. void StatusProc(char *status)
  311. {
  312.     Str255 Pstatus;
  313.     strcpy(Pstatus,status);
  314.     c2pstr(Pstatus);
  315. /*    DebugStr(Pstatus); */
  316. }