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

📄 pipeio.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	inline int IsOK()				{ return iOK; };

	friend class PipeIOObject;
};

/*____________________________________________________________________________*\
** More Data structures
\*____________________________________________________________________________*/
class PipeIOObject : public PipeIOBase
{
private:
	PipeIOConfig &tConfig;
	PIPLATFORM_FD iRd;
	PIPLATFORM_FD iWrt;
	int iPipe;

public:
	PipeIOObject(
		PipeIOConfig &tTheConfig,
		PIPLATFORM_FD iTheRd,
		PIPLATFORM_FD iTheWrt,
		int iIsPipe
		)
	:	tConfig( tTheConfig ),
		iRd( iTheRd ),
		iWrt( iTheWrt ),
		iPipe( iIsPipe )
		{};
	inline PIPLATFORM_FD GetReadDescriptor() 	{ return iRd; };
	inline PIPLATFORM_FD GetWriteDescriptor() 	{ return iWrt; };
	inline PIPLATFORM_FD GetNoYieldFlags()
			{ return tConfig.iNoYieldFlags; };
	inline int GetReadTimeout()		{ return tConfig.GetReadTimeout(); };
	inline int GetWriteTimeout()	{ return tConfig.GetWriteTimeout(); };
	inline int IsPipe()				{ return iPipe; };
	inline int IsOK()	{ return 1; };
};

/*____________________________________________________________________________*\
 *
 Function:		 PipeIO_onClassLoad
 Synopsis:
 Description:
	This is called on program startup, restart or shutdown.
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PipeIO_onClassLoad( PIClass_LoadAction eAction,
	void * /* pV */ )
{
	switch( eAction )
		{
		case STARTUP:
		case SHUTDOWN:
		default:;
		};

	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PipeIO_PipeIO(
	PIObject *pThis,
	int iArgc,
	const char *ppArgv[] )
{
	PipeIOConfig *pConfig = new PipeIOConfig( pThis, iArgc, ppArgv );
	if ( !pConfig || !pConfig->IsOK() )
		{
		return PIAPI_ERROR;
		};
	PIObject_setUserData( pThis, pConfig );
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PipeIO_copyPipeIO( PIObject *pThis, int iArgc,
	const char *ppArgv[] )
{
	if ( iArgc<2 )
		{
		CONFIG_ERR(pThis, "Invalid arguments" );
		return PIAPI_ERROR;
		};

	/* ---
	Get the configuration prototype object
	--- */
	PipeIOConfig *pConfig = (PipeIOConfig *)PIObject_getUserData( pThis );
	assert( pConfig );
	if ( !pConfig )
		{ return PIAPI_ERROR; };

	int iIsPipe = pConfig->IsPipe();
	if ( iArgc>2 )
		{ iIsPipe = ppArgv[2] ? 1 : 0 ;  };

	PipeIOObject *pObject = new PipeIOObject(
		*pConfig,
		(PIPLATFORM_FD)ppArgv[0],			/* --- read --- */
		(PIPLATFORM_FD)ppArgv[1],			/* --- write --- */
		iIsPipe								/* --- pipe semantics */
		);

	if ( !( pObject && pObject->IsOK() ) )
		{
		delete pObject;

		/* --- 
		NOTE this assumes the PipeIOObject constructor generates
		an error message
		--- */
		return PIAPI_ERROR;
		};

	if ( PIObject_setUserData( pThis, pObject ) )
		{
		/* --- should never happen --- */
		return PIAPI_ABORT;
		};

	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:		PipeIO_xPipeIO
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PipeIO_xPipeIO( PIObject *pThis, int, const char *[] )
{
	PipeIOBase *pObject=(PipeIOBase *)PIObject_getUserData( pThis );
	delete pObject;
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PipeIO_send( PIObject *pThis, void *pData,
	int iLen, int *piSent, int, const char *[] )
{
	/* --- automatic variables --- */
	int iRet;
	int iToSend=iLen;
	int iSent=0;
	const char *pToSend=(const char *)pData;
	PipeIOObject *pObject=
        (PipeIOObject *)PIObject_getUserData( pThis );
	assert( pToSend && pObject && iToSend && piSent );
	if ( !( pToSend && pObject && iToSend && piSent ) )
		{ return PIAPI_ERROR; };

	PIPLATFORM_FD tWrt = pObject->GetWriteDescriptor();
	int iTimeout = pObject->GetWriteTimeout();

	do	{	
		iRet = -1;
		if ( pObject->GetNoYieldFlags() & PIPLATFORM_POLL_WRITE )
			{
			/* --- don't yield --- */
			iRet = PIPLATFORM_POLL_WRITE;
			}
		else
			{
			/* --- yield if write not ready --- */
			iRet = pObject->IsPipe() ?
				  PIPlatform_pollPipeFD( tWrt, PIPLATFORM_POLL_WRITE, iTimeout )
				: PIPlatform_pollFD( tWrt, PIPLATFORM_POLL_WRITE, iTimeout ) ;
			};

		if ( iRet <= 0 )
			{
			iRet = -1;
			break;
			};

#if WIN32
		if ( !::WriteFile( (HANDLE)tWrt, &(pToSend[iSent]), iLen,
			(unsigned long *)&iRet, NULL ) )
			{
			iRet=-1;
			break;
			};
#elif POSIX
		iRet=::write( tWrt, &(pToSend[iSent]), iToSend ); 
#endif
		if ( !iRet ) break;							/* connection closed */
		if ( iRet==-1 )
			{
#if POSIX
			if ( errno==EINTR )
				{ continue;	/* signal caught */ };
#endif
			break;
			};
		iSent += iRet;

		iToSend -= iRet;
		} 
	while( iToSend>0 ); 
	
	if ( iRet==-1 )
		{
		return PIAPI_ERROR;
		};
	*piSent=iSent;
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PipeIO_recv( PIObject *pThis, void *pData,
	int iLen, int *piReceived, int, const char *[] )
{
	/* --- automatic variables --- */
	int iRet;
	PipeIOObject *pObject = (PipeIOObject *)PIObject_getUserData( pThis );
	assert( pObject && piReceived );
	if ( !( pObject && piReceived ) )
		{ return PIAPI_ERROR; };

	PIPLATFORM_FD tRd = pObject->GetReadDescriptor();
	int iTimeout = pObject->GetReadTimeout();

	for(;;)
		{
		iRet = -1;

		if ( pObject->GetNoYieldFlags() & PIPLATFORM_POLL_READ )
			{
			/* --- don't yield --- */
			iRet = PIPLATFORM_POLL_READ;
			}
		else
			{
			/* --- yield if read not ready --- */
			iRet = pObject->IsPipe() ?
				  PIPlatform_pollPipeFD( tRd, PIPLATFORM_POLL_READ, iTimeout )
				: PIPlatform_pollFD( tRd, PIPLATFORM_POLL_READ, iTimeout ) ;
			};

		if ( iRet <= 0 )
			{
			iRet = -1;
			break;
			};

#if WIN32
		if ( !::ReadFile( (HANDLE)tRd, pData, iLen, (unsigned long *)&iRet,
			NULL ) )
			{
			iRet = -1;
			break;
			};
#elif POSIX
		iRet=::read( tRd, (char *)pData, iLen ); 
		if ( iRet==-1 && errno==EINTR ) continue;	/* signal caught */
#endif

		break;
		}; 
	
	if ( iRet==-1 )
		{
		return PIAPI_ERROR;
		};
	*piReceived=iRet;

	return PIAPI_COMPLETED;
}

/* ---
	Class configuration specification 
--- */
#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name PipeIOClass
		Type IO
		Library IO
		OnClassLoad PipeIO_onClassLoad
		Constructor PipeIO_PipeIO
		CopyConstructor PipeIO_copyPipeIO
		Destructor PipeIO_xPipeIO
		Out PipeIO_send
		In PipeIO_recv
	</Class>

	<Object>
		Name DefaultPipeIOObject
		Class PipeIOClass
		ReadTimeout 60
		WriteTimeout 60
	</Object>

/*___+++CNF_END+++___*/
#endif

⌨️ 快捷键说明

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