📄 napi.c
字号:
MESG_SET); } if (error) {// Close the socket: INetEndTransaction(); inetInfoBlock.inetSockH = 0; } return (error);} // End INetGo(CharPtr,CharPtr,Int32)/*********************************************************************** * FUNCTION: INGetEvent * * DESCRIPTION: Get the next event in the Q. * * RETURNED: true if an event is in the Q ***********************************************************************/static inline void INGetEvent // (out) true if events in Q.( EventType *eventP, Int32 timeout){ INetLowInfoType inetInfoBlock; INetInfo(&inetInfoBlock, MESG_GET); INetLibGetEvent ( inetInfoBlock.inetRefNum, inetInfoBlock.inetHan, (INetEventType*)( eventP ), timeout );}/*********************************************************************** * FUNCTION: INetConnectionEventHandler * * DESCRIPTION: Get the next event in the Q. * * RETURNED: true if an event is in the Q ***********************************************************************/static Boolean INetConnectionEventHandler // (out) true if the connection canceled( INetEventType *eventP){ Boolean canceled = false;// If the antenna was put down, assume it's a cancellation if ( ( KeyCurrentState() & keyBitAntenna) == 0 ) canceled = true; // If it's related to our iNet socket, tell the task about it. if ( eventP->eType == inetSockReadyEvent ) { WWWSetSocketReady( true ); } if ( eventP->eType == inetSockStatusChangeEvent ) { if ( eventP->data.inetSockStatusChange.sockErr) { canceled = true; //ErrAlert(inetEventP->data.inetSockStatusChange.sockErr); } else { switch (eventP->data.inetSockStatusChange.status) { case inetStatusNew: break; case inetStatusResolvingName: case inetStatusNameResolved: case inetStatusConnecting: WWWSetSocketStatus( wwwStatusConnecting ); break; case inetStatusConnected: case inetStatusAcquiringNetwork: case inetStatusSendingRequest: case inetStatusWaitingForResponse: case inetStatusReceivingResponse: case inetStatusResponseReceived: WWWSetSocketStatus( wwwStatusDownloading ); break; case inetStatusClosingConnection: case inetStatusClosed: WWWSetSocketStatus( wwwStatusCompleted ); canceled = true; break; case inetStatusPrvInvalid: WWWSetSocketStatus( wwwStatusError ); break; default: break; } } } return canceled;}/*********************************************************************** * FUNCTION: INetSubTask * * DESCRIPTION: Does any and all network I/O during a fetch. * * RETURNED: state of connection * ***********************************************************************/static Err INetSubTask // (out) error if there's an error;( void){ UInt8 *dataP; static char buffP[ 64 ]; UInt32 reqBytes = 64, actBytes; Err anErr = noErr; INetLowInfoType inetInfoBlock; MemHandle h; INetInfo(&inetInfoBlock, MESG_GET);// Is there any data to read? if ( WWWIsSocketReady() ) { // Read the incoming bytes and add them to the document handle. anErr = INetLibSockRead ( inetInfoBlock.inetRefNum, inetInfoBlock.inetSockH, buffP, reqBytes, &actBytes, SysTicksPerSecond() );// What happened? if ( anErr == noErr ) { if ( actBytes > 0 ) {// Read incoming bytes h = WWWGetContentHandle( ); if ( h ) { dataP = MemHandleLock( h ); if ( dataP ) { MemMove ( dataP+WWWGetContentSize(), buffP, (long)actBytes ); WWWSetContentSize( WWWGetContentSize() + actBytes ); MemHandleUnlock( h ); } // copy data into handle else { anErr = memErrChunkNotLocked; } // We couldn't lock the handle to copy the data } // No handle. This is weird, but OK. } // Read incoming bytes else // no Error... and we're done! { WWWSetSocketStatus( wwwStatusCompleted ); } } // no error else if ( anErr == inetErrNeedTime ) {// We ignore timeout errors. anErr = noErr; } WWWSetSocketReady( false ); } // Is there any data to read? return anErr;}/*********************************************************************** * * FUNCTION: INetEndTransaction * * DESCRIPTION: Perform any necessary clean-up and close the socket. * (not much done here except calling INetLibSockClose and * updating the INet information store, but I thought * it might be nice to separate it further in case * something more is desired) * * PARAMETERS: nothing * * RETURNED: 0 for no error. Otherwise ... * ***********************************************************************/static Err INetEndTransaction(void){ Err error = 0; INetLowInfoType inetInfoBlock; // Initialize the inet information block: MemSet(&inetInfoBlock, sizeof(inetInfoBlock), 0); //------------------------------------------------------------------- // Get the library ref number, inet handle, socket handle, and // flags from storage: //------------------------------------------------------------------- error = INetInfo(&inetInfoBlock, MESG_GET);// Close the socket from any previous request: if (!error && inetInfoBlock.inetSockH) {// Non zero values can be returned to indicate errors, but these// are not documented: error = INetLibSockClose(inetInfoBlock.inetRefNum, inetInfoBlock.inetSockH);// Set the socket handle in storage to 0, as the socket was closed: inetInfoBlock.inetSockH = NULL; error = INetInfo(&inetInfoBlock, MESG_SET); } return error;} // INetEndTransaction(void)/*********************************************************************** * * FUNCTION: INetEnd * * DESCRIPTION: Perform any necessary clean-up and close the INetLib. * (not much done here except calling INetClose, but I * thought it might be nice to separate it further in * case something more related to INetLib is desired) * * PARAMETERS: nothing * * RETURNED: nothing * * REVISION HISTORY: * * ***********************************************************************/static Err INetEnd(void){ Err error = 0; INetLowInfoType inetInfoBlock;// Initialize the inet information block: MemSet(&inetInfoBlock, sizeof(inetInfoBlock), 0);// Retrieve the stored INet info: error = INetInfo(&inetInfoBlock, MESG_GET); if (!error && inetInfoBlock.inetRefNum && inetInfoBlock.inetHan) {// Close the INetLib (will actually remain open just long enough// for another app to use):// Note: INetLibClose() also closes any open sockets. if (inetInfoBlock.inetHan) INetLibClose(inetInfoBlock.inetRefNum, inetInfoBlock.inetHan); } return error; } // End INetEnd(void)/*********************************************************************** * FUNCTION: WWWUseWireline * * DESCRIPTION: Returns true of the app should use a wireline transport. * * RETURNED: true or false. ***********************************************************************/Boolean WWWUseWireline // (out) true if wireline should be used.( void){ return ( (Boolean)(wwwInstance.prefs.inetConfigIndex != 0) );}/*********************************************************************** * FUNCTION: WWWSetSocketStatus * * DESCRIPTION: Sets socket status for network layer. * SIDE EFFECT: Sets socketStatus field of wwwInstance. * * RETURNED: Nothing. ***********************************************************************/static void WWWSetSocketStatus( WWWStatusEnum status){ wwwInstance.socketStatus = status; WWWDisplayConnectionDialogMsg( wwwStatusMsg[ (int) status ]);}/*********************************************************************** * FUNCTION: WWWSetSocketReady * * DESCRIPTION: Sets socket ready to read flag for socket * SIDE EFFECT: Sets socketReadyToRead field of wwwInstance. * * RETURNED: Nothing. ***********************************************************************/static void WWWSetSocketReady( Boolean isReady // (in) is the socket ready?){ wwwInstance.socketReadyToRead = isReady;}/*********************************************************************** * FUNCTION: WWWIsSocketReady * * DESCRIPTION: Returns socket ready to read flag for socket * * RETURNED: true if socket has bytes to read. ***********************************************************************/static Boolean WWWIsSocketReady( void){ return wwwInstance.socketReadyToRead;}/*********************************************************************** * FUNCTION: WWWGetContentSize * * DESCRIPTION: Returns size of content * * RETURNED: Size of content ***********************************************************************/static UInt32 WWWGetContentSize( void){ return wwwInstance.contentSz;}/*********************************************************************** * FUNCTION: WWWSetContentSize * * DESCRIPTION: Sets size of content * * RETURNED: Nothing. ***********************************************************************/static void WWWSetContentSize( UInt32 newSz){ wwwInstance.contentSz = newSz;}/*********************************************************************** * FUNCTION: WWWGetContentHandle * * DESCRIPTION: Returns handle for content * * RETURNED: Nothing. ***********************************************************************/static MemHandle WWWGetContentHandle( void){ return wwwInstance.contentHandle;}/*********************************************************************** * FUNCTION: WWWSetPrefsDefault * * DESCRIPTION: Sets preferences to known defaults. * SIDE EFFECT: Sets prefs field of wwwInstance. * * RETURNED: Nothing. ***********************************************************************/static void WWWSetPrefsDefault( void){ wwwInstance.prefs.version = wwwPrefsVersion; wwwInstance.prefs.inetConfigIndex = 0; wwwInstance.prefs.inetCompression = ctpConvNone;}/*********************************************************************** * FUNCTION: WWWGetPrefs * * DESCRIPTION: Gets preferences from data store. * SIDE EFFECT: Sets prefs field of wwwInstance with fetched prefs. * * RETURNED: Nothing. ***********************************************************************/static void WWWGetPrefs( void){ UInt16 prefsSize; Int16 prefsVersion; // Read the saved preferences prefsSize = sizeof(WWWPrefsType); prefsVersion = PrefGetAppPreferences(wwwCreatorID, wwwPrefsID, &wwwInstance.prefs, &prefsSize, true); // Does the version look correct? if ( prefsVersion == wwwPrefsVersion && prefsSize == sizeof(WWWPrefsType) ) { // We read exactly what we expected } else { // We read an older or newer version of the prefs, or size is wrong, so return defaults. WWWSetPrefsDefault(); }}/*********************************************************************** * FUNCTION: WWWSetPrefs * * DESCRIPTION: Sets preferences from wwwInstance prefs. * * RETURNED: Nothing. ***********************************************************************/static void WWWSetPrefs( void){ PrefSetAppPreferences(wwwCreatorID, wwwPrefsID, wwwPrefsVersion, &wwwInstance.prefs, sizeof(WWWPrefsType), true);}/*********************************************************************** * FUNCTION: WWWGotoPanel * * DESCRIPTION: Launches the indicated Prefs panel. * SIDE EFFECT: Will cause the application to close. * * RETURNED: Nothing. ***********************************************************************/static void WWWGotoPanel( ULong creator // (in) what panel to launch){ LocalID theDBID; UInt theCardNo; DmSearchStateType theSearchState;// Grab the id of the panel we want to launch DmGetNextDatabaseByTypeCreator(true, &theSearchState, sysFileTPanel, creator, true, &theCardNo, &theDBID);// Launch the panel SysUIAppSwitch(theCardNo, theDBID, sysAppLaunchCmdPanelCalledFromApp, NULL );}/*********************************************************************** * FUNCTION: WWWPref * * DESCRIPTION: Launches the indicated Prefs panel. Will fail gracefully * if a connection is in progress. * SIDE EFFECT: Sets prefs field of wwwInstance with fetched prefs. * Closes and reopens the WWW functions to use new prefs. * * RETURNED: Nothing. ***********************************************************************/voidWWWPref( void){ FormPtr dialogFrmP, prevFrmP = FrmGetActiveForm(); ListType *uiListP; UInt16 btnObjectID; WinHandle winH; Err anErr; anErr = WWWOpen(); if ( anErr ) return; WWWShowIndicator( );// Load and init the Web preferences form winH = WinGetDrawWindow( ); WinSetDrawWindow( FrmGetWindowHandle ( prevFrmP ) ); WinEraseRectangle( &configRect, 0 ); WinSetDrawWindow( winH ); dialogFrmP = FrmInitForm ( _WWWConfigForm ); FrmDrawForm( dialogFrmP ); FrmSetActiveForm ( dialogFrmP ); // Set up the pick list of choices. uiListP = GetObjectPtr( _WWWConfigInetLibList ); LstSetSelection( uiListP, (short)wwwInstance.prefs.inetConfigIndex ); // Do the dialog btnObjectID = FrmDoDialog( dialogFrmP ); if ( btnObjectID == _WWWConfigOKButton ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -