⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pi3app.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Win32App/Pi3App.cpp,v $
 * $Date: 2003/05/22 20:04:36 $
 *
 Description:
\*____________________________________________________________________________*/
//$SourceTop:$

#include <assert.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>

#include "Pi2API.h"
#include "PIStrStr.h"

#if defined(WINDOWING) && !defined(WIN32)
#	error Cannot have WINDOWING defined and not WIN32
#endif

#if defined(WINDOWING)
#include "StrToken.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#endif

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
int iRet;
int iTheArgc;
const char **ppTheArgv;
static int ___iDoRestart = 0;		/* this isn't really necessary */
PIObject *___pGlobalObject = 0;		/* this also */
PISync *___pSync = 0;				/* mutex to signal termination */
#define SIGNAL_MSGS	0				/* give stdout messages on signals ? */
#if defined(WINDOWING)
HINSTANCE __hInstance = 0;
#endif

/* ---
Output a diagnostic message
--- */
#if defined(WINDOWING)
#	define ERROR_TITLE "Pi3"
#	define MSG_OUT(msg) MessageBox( NULL, (msg), ERROR_TITLE, MB_OK ); 
#	define DIAG_OUT(msg,db,type)								\
		{														\
		PIOStrStream __os;										\
		if ( *(msg) ) { __os << (msg) << endl; }				\
		const char *__pTmpFile = tmpnam( NULL );				\
		PILog_writeDiagnostics( (db), (type), __pTmpFile );		\
		ifstream __ifs( __pTmpFile );							\
		enum { __BUF_SIZE=4095 };								\
		char __szBuf[__BUF_SIZE+1];								\
		while( __ifs.good() && !__ifs.eof() )					\
			{													\
			__ifs.getline( __szBuf, __BUF_SIZE );				\
			__os << __szBuf << endl;							\
			};													\
		__ifs.close();											\
		__os << ends;											\
		MSG_OUT( __os.str() );									\
		remove( __pTmpFile );									\
		};
#else
#	define MSG_OUT(msg) cerr << (msg) << endl; 
#	define DIAG_OUT(msg,db,type)								\
		{														\
		if ( *(msg) ) { cerr << (msg) << endl; }				\
		PILog_writeDiagnostics( (db), (type), 0 );				\
		};
#endif

/* #define D { cerr << PIPlatform_getProcessId() << ": " << __FILE__ << ", " \
**	<< __LINE__ << endl; };
*/
#define D

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void Exit( int iReturnCode )
{
	if ( ___pGlobalObject )
		{
		PIObject_delete( ___pGlobalObject, 0, 0 );
		___pGlobalObject = 0;
		};
	iRet = iReturnCode;
	PIThread_terminate( PIThread_getMain(), iReturnCode );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void Usage()
{
	const char *pProgram = ( ppTheArgv && ppTheArgv[0] ) ? ppTheArgv[0] : "";
	PIOStrStream os;
	os << "Usage: " << pProgram << " <configuration file>" << ends;
	MSG_OUT( os.str() );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void ErrorExit( const char *pFunc, const char *pError )
{
	PIOStrStream os;
	os << (pFunc?pFunc:"") << ": " << (pError?pError:"") << ends;
	MSG_OUT( os.str() );
	Exit( -1 );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PISignalHandler( int iSignal )
{
#if SIGNAL_MSGS
	cout << "Pi3App[" << PIPlatform_getProcessId() << "]: ";
#endif
	switch( iSignal )
		{
		case SIGINT:
#if SIGNAL_MSGS
			cout << "** Interupt **" << endl;
#endif
			break;

#if POSIX
		case SIGHUP:
#if SIGNAL_MSGS
			cout << "** Restart **" << endl;
#endif
			___iDoRestart = 1;
			break;
#endif

		case SIGTERM:
#if SIGNAL_MSGS
			cout << "** Shutdown **" << endl;
#endif
			break;
	
		default:;
#if SIGNAL_MSGS
			cout << "Unknown signal: " << iSignal << endl;
#endif
			Exit( 0 );
		};

	if ( ___pSync )
		{
		/* --- unlock the mutex --- */
		PISync_unlock( ___pSync );
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int SetupSignalHandlers()
{
#if POSIX
	if ( signal( SIGHUP, PISignalHandler )==SIG_ERR ) { return 0; };
#endif
	if ( signal( SIGINT, PISignalHandler )==SIG_ERR ) { return 0; };
	if ( signal( SIGTERM, PISignalHandler )==SIG_ERR ) { return 0; };
	return 1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void DoApp( PIDB *pDB )
{
	iRet = -1;

	if ( iTheArgc < 2 )
		{
		Usage();
		return;
		};

	/* --- set signals --- */
	if ( !SetupSignalHandlers() )
		{
		PIOStrStream os;
		os << "Unable to set signals" << ends;
		MSG_OUT( os.str() );
		return;
		};

	PIConfig *pConfigDB = 0;
	const char *pFile = ppTheArgv[1];
	if ( PIConfig_loadConfigurationFile( pDB, pFile, &pConfigDB ) )
		{
		PIOStrStream os;
		os << "Unable to load configuration file: '"
			<< ( pFile ? pFile : "" )
			<< "'" << ends;
		DIAG_OUT( os.str(), pDB, PILOG_ALL );
		return;
		};

	PIObject *pObject = PIObject_load( pDB, pConfigDB, "Main", 0, 0 );
	if ( !pObject )
		{
		DIAG_OUT( "", pDB, PILOG_ALL );
		return;
		};

	___pGlobalObject = pObject;

#if 0
	/* ---
 	Delete the configuration DB
	--- */
/*	PIDB_delete( (PIDB *)pConfigDB );
**	PIDBIterator *pIter = PIDB_getIterator( (PIDB *)pConfigDB, PIDBTYPE_TREE,
*/		PIDBKEY_CONFIGURATION, 0 );
	PIDBIterator *pIter = PIDB_getIterator( pDB, PIDBTYPE_TREE,
		0, 0 );
	if ( pIter )
		{
		for( ;	PIDBIterator_atValidElement( pIter ); 
				PIDBIterator_next( pIter ) )
			{
			PIDBIterator_removeCurrent( pIter );
			};
		PIDBIterator_delete( pIter );
		};
#endif

#if POSIX
	PILog_writeDiagnostics( pDB, PILOG_WARNING, 0 );
#else
	/* --- nothing else gets the warnings --- */
#endif

	/* --- send subsequent messages to error stream --- */
	PILog_newSink( PILOG_STDERR, 0 );
	if ( PILogic_execute( pObject, iTheArgc, ppTheArgv ) )
		{
		DIAG_OUT( "", pDB, PILOG_ALL );
		return;
		};

	iRet = 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Main function of execution thread. Releases __pSync lock when DoApp()
	returns;
\*____________________________________________________________________________*/
void App( PIDB *pDB )
{
	DoApp( pDB );
	if ( ___pSync )
		{ PISync_unlock( ___pSync ); };
}

#if defined(WINDOWING)

extern "C" int StartWindowingThread( HINSTANCE, void (*)(void *), void * );

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void fnOnWindowingTermination( void *pData )
{
	PISync *pSync = (PISync *)pData;
	if ( pSync )
		{
		/* --- unlock the mutex to allow termination --- */
		PISync_unlock( pSync );
		};
}

#endif

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void ThreadFn( PIDB *pDB )
{
	PIThread *pThread = PIThread_new( 0, 0 );
	if ( !pThread )
		{
		PIOStrStream os;
		os << "Internal error: could not allocate thread object" << ends;
		MSG_OUT( os.str() );
		return;
		};

	___pSync = PIPlatform_allocLocalSemaphore( 0, 1);	

	if ( !___pSync )
		{
		PIOStrStream os;
		os << "Internal error: could not allocate mutex" << ends;
		MSG_OUT( os.str() );
		return;
		};

	if ( PIThread_begin( pThread,
			(unsigned long (*)(unsigned long))App,
			(unsigned long)pDB,
			PITHREAD_PRIORITY_MED, 0 ) )
		{
		PIOStrStream os;
		os << "Internal error: could not begin thread" << ends;
		MSG_OUT( os.str() );
		return;
		};

#if defined(WINDOWING)
	/*
	** If This is Win32 then create the windowing thread
	*/
	if ( !StartWindowingThread( __hInstance, fnOnWindowingTermination,
		___pSync ) )
		{
		/* --- failed --- */
		MSG_OUT( "Internal error: failed to create windowing thread" );
		return;
		}
#endif

	/* --- block on the semaphore --- */
	if ( PISync_lock( ___pSync ) )
		{
		/* --- failed to lock --- */
		PIOStrStream os;
		os << "Internal error: failed to lock mutex" << ends;
		MSG_OUT( os.str() );
		return;
		};

	/* --- lock has been released --- */

	/* --- kill main thread --- */
	PIThread_delete( pThread );	

	PISync *pTmp = ___pSync;
	___pSync = 0;
	PISync_delete( pTmp );
	if ( ___pGlobalObject )
		{
		PIObject_delete( ___pGlobalObject, 0, 0 );
		___pGlobalObject = 0;
		};
}

/*
** Static linkage of functions and classes
*/
#if defined(CONFIG_STATICLINK)
extern void *__PiAPI_functions;
extern void *__Pi2API_functions;
extern void *__Pi3API_functions;
extern void *__Server_functions;
extern void *__IO_functions;
extern void *__HTTP_functions;
extern void *__Fcgi_functions;
extern void *__Plugins_functions;
void *__pPiInternalFunctions[]=
	{
	__PiAPI_functions,
	__Pi2API_functions,
	__Pi3API_functions,
	 __Server_functions,
	__IO_functions,
	__HTTP_functions,
	__Fcgi_functions,
	__Plugins_functions,
	0
	};
#endif

#if defined(WINDOWING)

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PASCAL WinMain(
		HINSTANCE hInstance,
		HINSTANCE /* hPreInstance */,
		LPSTR /* lpCmdLine */,
		int /* nCmdShow */ )
{

	__hInstance = hInstance;

	LPSTR lpCmdLine = GetCommandLine();
	if ( !lpCmdLine )
		{
		return -1;
		};
	StringTokenizer tTokens( lpCmdLine, " " );
	int iArgc = tTokens.NumTokens();
	const char **ppArgv = (const char **)PI_NEW( char *[iArgc] );
	if ( !ppArgv )
		{
		PIOStrStream os;
		os << "Internal error: Could not allocate memory" << ends;
		MessageBox( NULL, os.str(), ERROR_TITLE, MB_OK );
		return -1;
		};
	for( int i=0; i<iArgc; i++)
		{
		ppArgv[i] = tTokens.GetToken( i );
		};
	iTheArgc = iArgc;
	ppTheArgv = ppArgv;

	___iDoRestart = 1;
	while( ___iDoRestart )
		{
		___iDoRestart = 0;
		if ( PIProgram_enter( ppArgv[0], PIPROGRAM_VERSION_1_0, ThreadFn ) )
			{
			PIOStrStream os;
			os << "Program_enter() failed." << ends;
			MessageBox( NULL, os.str(), ERROR_TITLE, MB_OK );
			PI_DELETE( [] ppArgv );
			exit( 1 );
			};
		};
	PI_DELETE( [] ppArgv );

	return iRet;
}

#else

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int main( int iArgc, const char *ppArgv[] )
{
	iTheArgc = iArgc;
	ppTheArgv = ppArgv;

	___iDoRestart = 1;
	while( ___iDoRestart )
		{
		___iDoRestart = 0;
		if ( PIProgram_enter( ppArgv[0], PIPROGRAM_VERSION_1_0, ThreadFn ) )
			{
			cerr << "Program_enter() failed." << endl;
			exit( 1 );
			};
		};

	return iRet;
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -