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

📄 piiobuf.h

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 H
📖 第 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/Pi3API/PIIOBuf.h,v $
 * $Date: 2003/06/01 09:39:21 $
 *
 Description:
\*____________________________________________________________________________*/
/* $SourceTop:$ */

#ifndef PIIOBUF_H_
#define PIIOBUF_H_

#include "Pi2API.h"

/*____________________________________________________________________________*\
 *
 Description:
	Flags which effect behaviour
\*____________________________________________________________________________*/
#define PIIOBUF_NONE		(0)
#define PIIOBUF_NOBUFFER	(1)
#define PIIOBUF_CHUNKED		(2)

	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
						C++ Interface

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
#if defined(__cplusplus) && defined(PI3_INTERNAL)

#include <assert.h>

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
class PIIOBuffer
{
private:
	PIObject *pIOObject;
	enum { IN_BUF_SIZE=2047, OUT_BUFFER_SIZE=8192 };
	char szInBuf[ IN_BUF_SIZE ];
	char *pOutBuffer;
	int iBufferLen;
	int iBytesSent;
	int iOutBufferSize;
	int iOutIOError;
	int iChunked;
    void *pUserData;

	/* --- internal methods --- */
	int GrowData();
	int FlushOutput( int iCloseChunk );
	int Internal_Write( const char *pData, int iLen, int iFlags );

public:
	PIIOBuffer( PIObject *pTheIOObject )
	:	pIOObject( pTheIOObject ),
		pOutBuffer( new char[(int)OUT_BUFFER_SIZE] ),
		iBufferLen( 0 ),
		iBytesSent( 0 ),
		iOutBufferSize( 0 ),
		iOutIOError( 0 ),
		iChunked( 0 ),
		pUserData( 0 )
		{
		assert( pOutBuffer );
		};

	~PIIOBuffer()
		{
		FlushOutput(1);
		delete [] pOutBuffer;
		};

	inline PIObject *GetIOObject()		{ return pIOObject; };
	int GetLine( char *pszBuf, int iBufLen );
	int PollBeforeRead();
	const char *Read( int *piRead );
	inline int Flush()					{ return FlushOutput(1); };
	int Read( char *pszBuffer, int iMaxLen );
	int Write( const char *pData, int iItsLength, int iFlags=PIIOBUF_NONE );
	int WriteLn( const char *pData, int iItsLength, int iFlags=PIIOBUF_NONE );
	inline int GetOutputBuffer( char **ppBuffer )
		{
		assert( pOutBuffer );
		assert( ppBuffer );
		*ppBuffer = &( pOutBuffer[iOutBufferSize] );
		return OUT_BUFFER_SIZE-iOutBufferSize;
		};
	inline int AdvanceBufferPointer( int iCount )
		{
		assert( iCount<=(OUT_BUFFER_SIZE-iOutBufferSize) );
		iOutBufferSize += iCount;
		if (iChunked) FlushOutput(0);
		return 1;
		};
	inline int getBufferLen()		{ return iBufferLen; };
	inline int GetBytesSent() const	{ return iBytesSent; };
	inline void ResetBytesSent()	{ iBytesSent = 0; iChunked = 0; };

    inline void *GetUserData()      { return pUserData; };
    inline void SetUserData( void *pTheUserData )
                                    { pUserData = pTheUserData; };
};

#else
	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
						External C Interface

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
/*____________________________________________________________________________*\
 *
 Description:
	Structure is opaque in C
\*____________________________________________________________________________*/
typedef int PIIOBuffer;


	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
						C Functions

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
#endif

/*____________________________________________________________________________*\
 *
 Name:
	PIIOBuffer_new

 Synopsis:
	PIIOBuffer *PIIOBuffer_new( PIObject *pIOObject )

 Description:
	Create a new IO buffer object which uses IO channel object pIOObject for
	input and output.

 Notes:
 Return Values:
	New created PIIOBuffer or NULL on error.

 Errors:

 See Also:
	PIIOBuffer_delete().
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIIOBuffer *PIIOBuffer_new( PIObject *pIOObject );

/*____________________________________________________________________________*\
 *
 Name:
	PIIOBuffer_delete

 Synopsis:
	int PIIOBuffer_delete( PIIOBuffer *pPIIOBuffer )

 Description:
	Deallocate memory associated with IO buffer object pIOBuffer after 
	flushing output.

 Notes:
	Destroying the buffer object does not destroy the IO channel it
	encapsulates.

 Return Values:
	PIAPI_COMPLETED if success or PIAPI_ERROR in case of an error.

 Errors:
	PIAPI_ERROR

 See Also:
	PIIOBuffer_new().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIIOBuffer_delete( PIIOBuffer *pPIIOBuffer );

/*____________________________________________________________________________*\
 *
 Name:
	PIIOBuffer_getLine

 Synopsis:
	int PIIOBuffer_getLine( PIIOBuffer *pIOBuf, char *pszBuf, int iBufLen )

 Description:
	Read a line from the IO buffer into buffer pszBuf. Up to iBufLen
	characters are written.

 Notes:
 Return Values:
	Returns number of characters read or PIAPI_ERROR (-1) on error.
	
 Errors:
	Returns PIAPI_ERROR (-1) on error.

 See Also:
	PIIOBuffer_read().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIIOBuffer_getLine( PIIOBuffer *pIOBuf, char *pszBuf,
	int iBufLen );

/*____________________________________________________________________________*\
 *
 Name:
	PIIOBuffer_pollBeforeRead

 Synopsis:
	int PIIOBuffer_pollBeforeRead( PIIOBuffer *pIOBuf )

 Description:
	Causes the current thread to sleep until data has been read from the
	IO channel into the internal buffer.

 Notes:
 Return Values:
	Returns PIAPI_ERROR (-1) on error, PIAPI_FALSE (0) for channel closed
	and PIAPI_TRUE (>0) for success.
	
 Errors:
	Returns PIAPI_ERROR (-1) on error, PIAPI_FALSE (0) on channel closed.

 See Also:
	PIIOBuffer_read().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIIOBuffer_pollBeforeRead( PIIOBuffer *pIOBuf );

/*____________________________________________________________________________*\
 *
 Name:
	PIIOBuffer_read

 Synopsis:
	const char *PIIOBuffer_read( PIIOBuffer *pIOBuf, int *piRead )

 Description:
	Reads data from the IO channel into the internal buffer and returns
	a pointer to the data read. The pointer piRead should point at 
	an integer which will be set to the number of bytes read on success.

 Notes:
 Return Values:
	Return a non-NULL pointer on success.
	
 Errors:
	Returns NULL on channel closed or error.

 See Also:
	PIIOBuffer_readToBuffer().
\*____________________________________________________________________________*/
PUBLIC_PIAPI const char *PIIOBuffer_read( PIIOBuffer *pIOBuf, int *piRead );

/*____________________________________________________________________________*\
 *
 Name:
	PIIOBuffer_readToBuffer

 Synopsis:
	int PIIOBuffer_readToBuffer( PIIOBuffer *pIOBuf, char *pszBuf,
		int iMaxLen )

⌨️ 快捷键说明

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