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

📄 pi3srv32.cpp

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

 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/Pi3Srv32.cpp,v $
 * $Date: 2004/05/29 16:46:53 $
 *
 Description:
	Win32 service functions.
\*____________________________________________________________________________*/
//$SourceTop:$

#include <assert.h>
#include <windows.h>
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>

#include "Pi2API.h"
#include "PIStrStr.h"
#include <windowsx.h>
#include "resource.h"

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
void ServiceMain( DWORD dwArgc, LPTSTR *lpszArgv );
void ServiceControlHandler( DWORD dwCtrlCode );
void StopService( LPTSTR lpszMsg );
HANDLE hTerminateEvent = 0;
SERVICE_STATUS_HANDLE sshStatusHandle;
SERVICE_STATUS ssStatus;
DWORD dwCheck;
int iRet = -1;
#define PI3_SERVICE_NAME			"Pi3Web"
#define PI3_SERVICE_ERROR_FILE		"Pi3Error.log"
#define PI3_SERVICE_WARNING_FILE	"Pi3Warn.log"
#define DEFAULT_WAIT_TIME				1000
#define DEFAULT_WAIT_TIME_INTERACTIVE	10000
#define PI3_LOCAL_PARAMFILE			"SOFTWARE\\Pi3\\Pi3Svc"
#define PI3_CONFIGPATH_KEY			"ConfigPath"
#define PI3_DEFAULT_CONFIGPATH		"..\\Conf\\Config.pi3"
// #define D(x) { MessageBox( NULL, (x), __FILE__, MB_OK ); };
#define D(x)

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void main()
{
	SERVICE_TABLE_ENTRY steDispatcherTable[] =
		{
		{ TEXT( PI3_SERVICE_NAME ), (LPSERVICE_MAIN_FUNCTION) ServiceMain },
		{ NULL, NULL }
		};

	if ( !StartServiceCtrlDispatcher( steDispatcherTable ) )
		{
		DWORD dwError = GetLastError();
		cerr << "Win32 error #" << dwError << endl;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void StopService( LPTSTR lpszMsg )
{
	LPTSTR lpStrings[2];
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];
	HANDLE hEventSource = RegisterEventSource( NULL, TEXT(PI3_SERVICE_NAME) );
	assert( hEventSource );
	sprintf( szBuf, "Pi3Service Error: %d", GetLastError() );
	lpStrings[0] = szBuf;
	lpStrings[1] = lpszMsg;
	if ( hEventSource )
		{
		if ( !ReportEvent(
			hEventSource,
			EVENTLOG_ERROR_TYPE,
			0,
			0,
			NULL,
			2,
			0,
			(const char **)lpStrings,
			NULL ) )
			{
			}
		else
			{
			};
		DeregisterEventSource( hEventSource );
		};	

	if ( hTerminateEvent )
		{ SetEvent( hTerminateEvent ); };
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
DWORD dwTheArgc;
LPTSTR *lpszTheArgv;
PIObject *___pObject;
PIDB *___pDB;
unsigned long Pi3Fn( unsigned long )
{
	if ( PILogic_execute( ___pObject, dwTheArgc, (const char **)lpszTheArgv ) )
		{
		remove( PI3_SERVICE_ERROR_FILE );
		PILog_writeDiagnostics( ___pDB, PILOG_ALL, PI3_SERVICE_ERROR_FILE );
		};
	
	return 0UL;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void App( PIDB *pDB )
{
	PIThread *pThread = 0;
	PIConfig *pConfigDB = 0;
	const char *pErr = 0;

	___pDB = pDB;

	sshStatusHandle = RegisterServiceCtrlHandler(
		TEXT( PI3_SERVICE_NAME ),
		(LPHANDLER_FUNCTION)ServiceControlHandler );

	if ( sshStatusHandle == (SERVICE_STATUS_HANDLE)NULL )
		{
		pErr = "RegisterServiceCtrlHandler() failed";
		goto DO_ERROR;
		};

	dwCheck = 1;
	ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	ssStatus.dwServiceSpecificExitCode = 0;

	/* --- notify service is pending, checkpoint 1 --- */
	ssStatus.dwControlsAccepted = 0;
	ssStatus.dwCurrentState = SERVICE_START_PENDING;
	ssStatus.dwWin32ExitCode = NO_ERROR;
	ssStatus.dwCheckPoint = dwCheck++;
	ssStatus.dwWaitHint = DEFAULT_WAIT_TIME;
	if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
		{ StopService( "SetServiceStatus" ); goto CLEANUP; };

	/* read in parameters from parameter file in current directory */
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];
	HKEY hKey;
	if ( RegOpenKeyEx(
		HKEY_LOCAL_MACHINE,		// handle of an open key 
		PI3_LOCAL_PARAMFILE,	// address of subkey name		
		0,						// reserved
		KEY_EXECUTE |
		KEY_QUERY_VALUE,		// desired security access 
		&hKey			 		// address of buffer for opened handle 
	) == ERROR_SUCCESS )
		{
		DWORD dwSize = BUF_SIZE+1;
		DWORD dwType = REG_SZ;
		if ( RegQueryValueEx(
			hKey,				// handle of key to query 
			PI3_CONFIGPATH_KEY,	// address of name of value to query 
			NULL,				// reserved 
			&dwType,			// address of buffer for value type 
			(LPBYTE)&szBuf[0],	// address of data buffer 
			&dwSize				// address of data buffer size 
		) != ERROR_SUCCESS )
			{
			strcpy( szBuf, PI3_DEFAULT_CONFIGPATH );
			}
		}
	else
		{
		strcpy( szBuf, PI3_DEFAULT_CONFIGPATH );
		}

	if ( !*szBuf )
		{
		pErr = "Could not read configuration filename";
		goto DO_ERROR;
		};

	hTerminateEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
	if ( !hTerminateEvent ) 
		{ goto CLEANUP; };

	/* ---
	notify service is pending, checkpoint 2

	Loading the configuration files and setting up the objects
	and libraries can take a while.

	--- */
	ssStatus.dwControlsAccepted = 0;
	ssStatus.dwCurrentState = SERVICE_START_PENDING;
	ssStatus.dwWin32ExitCode = NO_ERROR;
	ssStatus.dwCheckPoint = dwCheck++;
	ssStatus.dwWaitHint = DEFAULT_WAIT_TIME;
	if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
		{ StopService( "SetServiceStatus" ); goto CLEANUP; };

	/* --- load up the configuration file --- */
	if ( PIConfig_loadConfigurationFile( pDB, szBuf, &pConfigDB ) )
		{
		remove( PI3_SERVICE_ERROR_FILE );
		PILog_writeDiagnostics( pDB, PILOG_ALL, PI3_SERVICE_ERROR_FILE );
		StopService("Could not load configuration file");
		goto CLEANUP;
		};

	ssStatus.dwControlsAccepted = 0;
	ssStatus.dwCurrentState = SERVICE_START_PENDING;
	ssStatus.dwWin32ExitCode = NO_ERROR;
	ssStatus.dwCheckPoint = dwCheck++;
	ssStatus.dwWaitHint = DEFAULT_WAIT_TIME;
	if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
		{ StopService( "SetServiceStatus" ); goto CLEANUP; };

	/* --- load up the Pi3 main object --- */
	___pObject = PIObject_load( pDB, pConfigDB, "Main", 0, 0 );
	if ( !___pObject )
		{
		remove( PI3_SERVICE_ERROR_FILE );
		PILog_writeDiagnostics( pDB, PILOG_ALL, PI3_SERVICE_ERROR_FILE );
		StopService("Could not load main Pi3 object");
		goto CLEANUP;
		};

⌨️ 快捷键说明

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