home *** CD-ROM | disk | FTP | other *** search
/ More MacCube 1: Arcade Games / More MacCube Vol 1 Arcade Games.bin / Games⁄Arcade / Bolo / More information / Sample Code / Std Autopilot / BF_IAC.c < prev    next >
Encoding:
Text File  |  1995-04-28  |  5.0 KB  |  171 lines  |  [TEXT/KAHL]

  1. // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
  2. // All rights reserved. This code is owned by Stuart Cheshire and is donated
  3. // free of charge for non-commercial use. You may not use this code in any
  4. // product sold for commercial profit, except shareware priced at $25 or less.
  5.  
  6. #include <TextUtils.h>        // For EqualString
  7. #include <Errors.h>            // for userCanceledErr
  8.  
  9. #include "BrainFrame.h"
  10. #include "BF_Globals.h"
  11. #include "BF_Utils.h"
  12. #include "BF_IAC.h"
  13.  
  14. local PPCPortRefNum BoloPPCPort;
  15.  
  16. export OSErr CallBolo(ConnectionInfo *conn, BoloCommand command)
  17.     {
  18.     OSErr retcode;
  19.     BoloRequest req;
  20.     PPCParamBlockRec p;
  21.     
  22.     req.sig     = 'BOLO';
  23.     req.version = BoloMessageVersion;
  24.     req.command = command;
  25.     
  26.     p.readParam.ioCompletion = NULL;
  27.     p.readParam.sessRefNum   = conn->BoloSessRefNum;
  28.     p.readParam.bufferLength = sizeof(req);
  29.     p.readParam.bufferPtr    = (Ptr)&req;
  30.     p.readParam.more         = FALSE;
  31.     p.readParam.userData     = 0;
  32.     p.readParam.blockCreator = 'BOLO';
  33.     p.readParam.blockType    = 'BBRN';
  34.     retcode = PPCWrite(&p.writeParam, FALSE);
  35.     //if (retcode) fatal("PPCWrite failed %d", retcode);
  36.     if (retcode) return(retcode);
  37.     
  38.     if (command == BoloView)        // Only BoloView expects a reply
  39.         {
  40.         p.readParam.bufferLength = sizeof(conn->info);
  41.         p.readParam.bufferPtr    = (Ptr)&conn->info;
  42.         retcode = PPCRead(&p.readParam, FALSE);
  43.         //if (retcode) fatal("PPCRead failed %d", retcode);
  44.         }
  45.     
  46.     return(retcode);
  47.     }
  48.  
  49. local OSErr MyPPCOpen(PPCPortRefNum *thePortRefNum, Boolean *nbpRegisteredFlag)
  50.     {
  51.     static const u_char myname[] = "\pBrainFrame";
  52.     OSErr retcode;
  53.     PPCPortRec      thePPCPortRec = { smRoman, "\p", ppcByString, "\pBrainFrame 0" };
  54.     PPCOpenPBRec    thePPCOpenPBRec;
  55.  
  56.     BlockMove(myname, thePPCPortRec.name, 1+myname[0]);
  57.  
  58.     thePPCOpenPBRec.serviceType    = ppcServiceRealTime;
  59.     thePPCOpenPBRec.resFlag        = 0;
  60.     thePPCOpenPBRec.portName       = &thePPCPortRec;
  61.     thePPCOpenPBRec.locationName   = NULL;
  62.     thePPCOpenPBRec.networkVisible = TRUE;
  63.  
  64.     do    {
  65.         thePPCPortRec.u.portTypeStr[thePPCPortRec.u.portTypeStr[0]]++;
  66.         retcode = PPCOpen(&thePPCOpenPBRec, FALSE);
  67.         } while (retcode == portNameExistsErr);
  68.     
  69.     if (retcode == noErr)
  70.         {
  71.         *thePortRefNum = thePPCOpenPBRec.portRefNum;
  72.         *nbpRegisteredFlag = thePPCOpenPBRec.nbpRegistered;
  73.         }
  74.     return(retcode);
  75.     }
  76.  
  77. local OSErr MyStartSession(PPCPortRefNum port, PortInfoRec *portInfo, LocationNameRec *theLocation,
  78.     PPCSessRefNum *sessionRef, unsigned long *userRef)
  79.     {
  80.     OSErr retcode;
  81.     PPCStartPBRec thePPCStartPBRec;
  82.     thePPCStartPBRec.ioCompletion = NULL;
  83.     thePPCStartPBRec.portRefNum   = port;
  84.     thePPCStartPBRec.serviceType  = ppcServiceRealTime;
  85.     thePPCStartPBRec.resFlag      = 0;
  86.     thePPCStartPBRec.portName     = &portInfo->name;
  87.     thePPCStartPBRec.locationName = theLocation;
  88.     thePPCStartPBRec.userData     = 0;
  89.     thePPCStartPBRec.userRefNum   = 0;
  90.     if (!portInfo->authRequired) retcode = PPCStart(&thePPCStartPBRec, FALSE);
  91.     else
  92.         {
  93.         Boolean guestselected;
  94.         Str32 username = "\p";
  95.         retcode = StartSecureSession(&thePPCStartPBRec, username, TRUE, FALSE, &guestselected, NULL);
  96.         }
  97.     if (retcode == noErr)
  98.         {
  99.         *sessionRef = thePPCStartPBRec.sessRefNum;
  100.         *userRef    = thePPCStartPBRec.userRefNum;
  101.         }
  102.     return(retcode);
  103.     }
  104.  
  105. local OSErr MyEndSession(PPCSessRefNum session)
  106.     {
  107.     PPCEndPBRec theEndPBRec;
  108.     theEndPBRec.sessRefNum = session;
  109.     return(PPCEnd(&theEndPBRec, FALSE));
  110.     }
  111.  
  112. local OSErr MyDeleteNewUserRefNum(unsigned long userRef)
  113.     {
  114.     if (userRef != 0)
  115.         {
  116.         unsigned long defUserRef;
  117.         Str32 defUserName;
  118.         // If there is no default user, or there is a default user but we are not it,
  119.         // then we should delete our userRef
  120.         if (GetDefaultUser(&defUserRef, defUserName) || defUserRef != userRef)
  121.             return(DeleteUserIdentity(userRef));
  122.         }
  123.     return(noErr);
  124.     }
  125.  
  126. local pascal Boolean BrowseFilter(LocationNamePtr theLoc, PortInfoPtr thePortInfo)
  127.     {
  128.     theLoc;        // Unused
  129.     return(thePortInfo->name.portKindSelector == ppcByString &&
  130.             EqualString(thePortInfo->name.u.portTypeStr, "\pBoloTank", TRUE, TRUE));
  131.     }
  132.  
  133. export OSErr PPCToolBoxConnect(ConnectionInfo *conn)
  134.     {
  135.     LocationNameRec theLocation;
  136.     PortInfoRec portInfo;
  137.     OSErr retcode;
  138.     retcode = PPCBrowser("\pSelect a Bolo tank to view from", "\pTanks",
  139.                         FALSE, &theLocation, &portInfo, BrowseFilter, NULL);
  140.     if (retcode && retcode != userCanceledErr) fatal("PPCBrowser failed %d", retcode);
  141.     if (retcode) return(retcode);
  142.  
  143.     conn->portinfo = portInfo.name;
  144.  
  145.     retcode = MyStartSession(BoloPPCPort, &portInfo, &theLocation, &conn->BoloSessRefNum, &conn->BoloUserRefNum);
  146.     if (retcode) fatal("MyStartSession failed %d", retcode);
  147.  
  148.     return(retcode);
  149.     }
  150.  
  151. export void PPCToolBoxDisconnect(ConnectionInfo *conn)
  152.     {
  153.     CallBolo(conn, BoloSuspend);
  154.     if (conn->BoloSessRefNum) MyEndSession(conn->BoloSessRefNum);
  155.     if (conn->BoloUserRefNum) MyDeleteNewUserRefNum(conn->BoloUserRefNum);
  156.     }
  157.  
  158. export OSErr PPCToolBoxInit(void)
  159.     {
  160.     Boolean nbpRegisteredFlag;
  161.     OSErr retcode = MyPPCOpen(&BoloPPCPort, &nbpRegisteredFlag);
  162.     if (retcode) fatal("MyPPCOpen failed %d", retcode);
  163.     return(retcode);
  164.     }
  165.  
  166. export void PPCToolBoxQuit(void)
  167.     {
  168.     PPCClosePBRec p;
  169.     if (BoloPPCPort) { p.portRefNum = BoloPPCPort; PPCClose(&p, FALSE); }
  170.     }
  171.