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

📄 pipeio.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/IO/PipeIO.cpp,v $
 * $Date: 2003/05/13 18:42:04 $
 *
 Description:
	Pipe IO.
\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include <assert.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>

#if WIN32
#	include <windows.h>
#elif POSIX
#	include <unistd.h>
#	include <errno.h>
#else
#	error Unsupported Operating system
#endif

#define VERBOSE_DEBUG	0
#if VERBOSE_DEBUG
#	define D { cerr << __FILE__ << ", " << __LINE__ << endl; }
#else
#	define D
#endif

#include "StrToken.h"		/* --- from Base Library --- */
#include "Pi2API.h"

#define CONFIG_ERR(pThis, msg)	\
	{ PILog_addMessage( PIObject_getDB(pThis), \
	PIObject_getConfigurationDB(pThis), PILOG_ERROR, (msg) ); }

#define DEFAULT_READ_TIMEOUT	60
#define DEFAULT_WRITE_TIMEOUT	60
#define KEY_CONF_READTIMEOUT	"ReadTimeout"	
#define KEY_CONF_WRITETIMEOUT	"WriteTimeout"	
#define KEY_CONF_NOYIELD		"NoYield"	
#define KEY_CONF_FLAG			"Flag"	
#define KEY_VALUE_READ			"Read"	
#define KEY_VALUE_WRITE			"Write"	
#define KEY_VALUE_PIPE			"Pipe"	

/*____________________________________________________________________________*\
 Description:
	Documentation for this IO object.
\*____________________________________________________________________________*/

#if 0
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	PipeIO

Description:
	Implementation of IO using pipes.

Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>ReadTimeout
<TD>60
<TD>n seconds or -1 (no timeout)
<TD>Timeout on reading
<TD>ReadTimeout 45

<TR>
<TD>WriteTimout
<TD>60
<TD>n seconds or -1 (no timeout)
<TD>Timeout on writing
<TD>WriteTimeout 45

<TR>
<TD>NoYield
<TD>-
<TD>Read|Write
<TD>Do not yield to other threads
<TD>NoYield="Read"

<TR>
<TD>Flag
<TD>-
<TD>Pipe
<TD>Specify a flag
<TD>Flags="Pipe"

</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>

<H4>Description of Options</H4>
<H5>ReadTimeout</H5>
The timeout for reading data. A value of -1 specifies no timeout (infinite
wait).

<H5>WriteTimeout</H5>
Specifies the timeout for writing data. A value of -1 specifies no timeout
(infinite wait).

<H5>NoYield</H5>
Forces the program to block on reading or writing IO into the pipe. The
default behaviour is to allow yield processing to other threads if
the IO action would block. 
Some uses of this IO object require no latency on reading or sending data.
An example is reading the response from a CGI program.

Specifies the timeout for writing data. A value of -1 specifies no timeout
(infinite wait).

<H5>Flag</H5>
Specify a flag which effects the behaviour of this IO filter.
<UL>
<LI><B>Pipe</B> - Descriptors are pipes not file handles by default.
This primarily effects behaviour on non-POSIX systems. 

This directive can be repeated multiple times to specify multiple flags.

Returns:
	PIAPI_COMPLETED on success.
	PIAPI_ERROR and PIAPI_ABORT respectively for generic and severe
	error conditions.

Example:
	<PRE>
	&lt;Object&gt;
		Name PipeIO
		Class PipeIOClass
		ReadTimeout 45
		WriteTimeout -1
		Flag "Pipe"
	&lt;/Object&gt;
	</PRE>

/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
** Data structures
\*____________________________________________________________________________*/
class PipeIOBase
{
public:
	/* ---
	This is so that the Pi3 object destructor doesn't have to do
	wierd things to figure out the type of object to destruct
	--- */
	virtual ~PipeIOBase()	 {};
};

/*____________________________________________________________________________*\
** Data structures
\*____________________________________________________________________________*/
class PipeIOConfig : public PipeIOBase
{
private:
	PIObject *pObject;
	int iRdTimeout;
	int iWrtTimeout;
	int iNoYieldFlags;
	int iOK;
	int iPipe;

public:
	int Parameter( const char *pVar, const char *pVal, const char *pWhere )
		{
		assert( pVar && pVal );
		if ( !pVar || !pVal ) 
			{ return 0; };

		if ( !PIUtil_stricmp( pVar, KEY_CONF_READTIMEOUT ) )
			{
			iRdTimeout = atoi( pVal );
			}
		else if ( !PIUtil_stricmp( pVar, KEY_CONF_WRITETIMEOUT ) )
			{
			iWrtTimeout = atoi( pVal );
			}
		else if ( !PIUtil_stricmp( pVar, KEY_CONF_FLAG ) )
			{
			if ( !PIUtil_stricmp( pVal, KEY_VALUE_PIPE ) )
				{ iPipe = 1; }
			else
				{
				PILog_addMessage( PIObject_getDB(pObject),
					PIObject_getConfigurationDB(pObject),
					PILOG_ERROR, "%sPipeIO: Unknown value for 'Flag' '%s'.",
					( pWhere ? pWhere : "" ), pVal );
				return 0;
				};
			}
		else if ( !PIUtil_stricmp( pVar, KEY_CONF_NOYIELD ) )
			{
			StringTokenizer tTokens( pVal, "|" );
			for( int i=0; i<tTokens.NumTokens(); i++ )
				{
				/* ---
				Get token, strip leading and trailing spaces
				--- */
				const PIString &sToken = tTokens.GetToken( i );
				const char *pToken = sToken;
				int iLen = sToken.Len();
				int j=0;
				for( j=0; j<iLen && isspace( pToken[j] ); j++ );
				pToken = &( pToken[j] );
				iLen -= j;
				for( j=0; j<iLen && !isspace( pToken[i] ); j++ );
				PIString sTmp( pToken, j );
				
				/* ---
				Check string
				--- */
				if ( !PIUtil_stricmp( sTmp, KEY_VALUE_READ ) )
					{
					iNoYieldFlags |= PIPLATFORM_POLL_READ;
					}
				else if ( !PIUtil_stricmp( sTmp, KEY_VALUE_WRITE ) )
					{
					iNoYieldFlags |= PIPLATFORM_POLL_WRITE;
					}
				else
					{
					PILog_addMessage( PIObject_getDB(pObject),
						PIObject_getConfigurationDB(pObject),
						PILOG_ERROR, "%sPipeIO: Invalid 'NoYield' flags '%s', \
valid values are 'Read' and 'Write'.",
						( pWhere ? pWhere : "" ),
						pToken );
					return 0;
					};
				};
			}
		else
			{
			enum { BUF_SIZE=1023 };
			char szBuf[BUF_SIZE+1];
			sprintf( szBuf, "%sPipeIO: Unknown parameter '%s'",
				pWhere, pVar );
			CONFIG_ERR( pObject, szBuf );
			return 0;
			};
		return 1;
		};	
	static int ParameterFn( void *pData, const char *pVar, const char *pVal,
		const char *pWhere )
		{
		return ((PipeIOConfig *)pData)->Parameter( pVar, pVal, pWhere );
		};

public:
	PipeIOConfig( PIObject *pTheObject, int iArgc, const char *ppArgv[] )
	:
		pObject( pTheObject ),
		iRdTimeout( DEFAULT_READ_TIMEOUT ),
		iWrtTimeout( DEFAULT_WRITE_TIMEOUT ),
		iNoYieldFlags( 0 ),
		iOK( 0 ),
		iPipe( 0 )
		{
		if ( PIObject_readParameters( pObject, iArgc, ppArgv, ParameterFn,
			this ) )
			{
			iOK = 1;
			};
		};
	inline int GetReadTimeout()		{ return iRdTimeout; };
	inline int GetWriteTimeout()	{ return iWrtTimeout; };
	inline int IsPipe()				{ return iPipe; };

⌨️ 快捷键说明

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