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

📄 nothread.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::PollNetFD( int iFD, int iFlags, int iTimeout )
{
	if ( iFD<0 ) 
		{ PIERROR( PIAPI_EINVAL ); return -1; };	
	timeval tv;
	tv.tv_sec = iTimeout;
	tv.tv_usec = 0;
	fd_set tRead, tWrite, tExcept;
	FD_ZERO( &tRead );
	FD_ZERO( &tWrite );
	FD_ZERO( &tExcept );
	if ( iFlags & Platform::POLL_READ )
		{ FD_SET( iFD, &tRead ); };	
	if ( iFlags & Platform::POLL_WRITE )
		{ FD_SET( iFD, &tWrite ); };	
	FD_SET( iFD, &tExcept );	
	int iRet = 0;
	do	{
		iRet = ::select( iFD+1, &tRead, &tWrite, &tExcept,
			iTimeout==-1 ? 0 : &tv );
		} while( iRet==-1 && errno==EINTR );
	switch( iRet )
		{
		case -1:	/* error */
			PIOSERR;
			return -1;

		case 0:		/* timeout */
			return 0;

		default:;	/* num descriptors (exact meaning varies by OS) */
		};
	int iRetFlags = 0;
	if ( FD_ISSET( iFD, &tExcept ) )	{ PIERROR( PIAPI_ERROR ); return -1; };
	if ( FD_ISSET( iFD, &tRead ) )
		{ iRetFlags = iRetFlags | Platform::POLL_READ; };
	if ( FD_ISSET( iFD, &tWrite ) )
		{ iRetFlags = iRetFlags | Platform::POLL_WRITE; };
	return iRetFlags;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::Sleep( int iMilliseconds )
{
#if defined(CONFIG_OS_POSIX)
	::sleep( (iMilliseconds/1000)+((iMilliseconds%1000)?1:0) );
#elif defined(CONFIG_OS_WIN32)
	::Sleep( iMilliseconds );
#elif defined(CONFIG_OS_WIN16)
	(void)iMilliseconds;
	/* LATER what to do here? */
#endif
	return 0;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::CatchExceptions( void (* fn)( void *), void *pArg )
{
#if defined(CONFIG_CPP_EXCEPTIONS)
	try	{
		(fn)( pArg );
		}
	catch( StubThread::StackUnwindException *pE )
		{
		PI_DELETE( pE );
		}
#else
	if ( setjmp( ___tJmpBuf )==0 )
		{ (fn)( pArg ); };
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::Read( int iFd, int iLength, void *pData )
{
	assert( iFd!=-1 && iLength && pData );
	if ( iFd==-1 || !iLength || !pData )
		{
		PIERROR( PIAPI_EINVAL );
		return -1;
		};
	int iToRead = iLength;
	int iRead;
	for(; iToRead ;)
		{
		iRead = -1;
#if defined(CONFIG_OS_POSIX)
		iRead = ::read( iFd, pData, iLength );
		if ( iRead==-1 && errno==EINTR )
			{ continue; };

#elif defined(CONFIG_OS_WIN32)
		/* --- 
		the cast from int * to DWORD * (DWORD is currrently unsigned long), is
		not	so bad because iLength is the maximum size that can be sent and 
		is also an int
		--- */
		if ( !::ReadFile( (HANDLE)iFd, pData, iLength, (DWORD *)&iRead,
			0 ) )
			{ iRead=-1; };

#endif
		if ( !iRead || iRead==-1 )
			{ break; };
		iToRead -= iRead;
		};

	if ( iRead==-1 )
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
	return PIAPI_COMPLETED;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::Write( int iFd, int iLength, const void *pData )
{
	assert( iFd!=-1 && iLength && pData );
	if ( iFd==-1 || !iLength || !pData )
		{
		PIERROR( PIAPI_EINVAL );
		return -1;
		};
	int iToWrite = iLength;
	int iWritten;
	for(; iToWrite ;)
		{
		iWritten = -1;
#if defined(CONFIG_OS_POSIX)
		iWritten = ::write( iFd, pData, iLength );
		if ( iWritten==-1 && errno==EINTR )
			{ continue; };

#elif defined(CONFIG_OS_WIN32)
		/* ---
		the cast from int * to DWORD * (DWORD is currrently unsigned long), is
		not	so bad because iLength is the maximum size that can be sent and 
		is also an int
		--- */
		DWORD dwWritten;
		if ( !::WriteFile( (HANDLE)iFd, pData, iLength, &dwWritten, 0 ) )
			{
			dwWritten = (DWORD)-1;
			}
		else
			{
			iWritten = (int)dwWritten;
			};

#endif
		if ( !iWritten || iWritten==-1 )
			{ break; };
		iToWrite -= iWritten;
		};

	if ( iWritten==-1 )
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
	return PIAPI_COMPLETED;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::WriteAtomic( int iFd, int iLength, const void *pData )
{
	assert( iFd!=-1 && iLength && pData );
	if ( iFd==-1 || !iLength || !pData )
		{
		PIERROR( PIAPI_EINVAL );
		return -1;
		};
	int iToWrite = iLength;
	int iWritten;
#if defined(CONFIG_OS_POSIX)
	/* ---
	Enter loop to obtain a lock on the file
	--- */
	for(;;)
		{
		/* --- blocking call to lock --- */
#if defined(CONFIG_LOCK_FLOCK)
		if ( ::flock( iFd, LOCK_EX )==0 )
#else
		if ( ::lockf( iFd, F_LOCK, 0 )==0 )
#endif
			{
			/* --- got the lock, continue with accept() --- */
			break;
			}
		else if ( errno==EINTR )
			{
			/* ---
			man on solaris did not list this as a possible scenario,
			i.e. operation interrupted because a signal was caught,
			but just in case...
			--- */
			continue;
			};

		/* --- an error occurred in lock --- */
		PIOSERR;
		return PIAPI_ERROR;
		};
#endif

	for(; iToWrite ;)
		{
		iWritten = -1;

#if defined(CONFIG_OS_POSIX)
		iWritten = ::write( iFd, pData, iLength );
		if ( iWritten==-1 && errno==EINTR )
			{ continue; };

#elif defined(CONFIG_OS_WIN32)
		/* --- 
		the cast from int * to DWORD * (DWORD is currrently unsigned long), is
		not	so bad because iLength is the maximum size that can be sent and 
		is also an int
		--- */
		if ( !::WriteFile( (HANDLE)iFd, pData, iLength, (DWORD *)&iWritten,
			0 ) )
			{ iWritten=-1; };

#endif
		if ( !iWritten || iWritten==-1 )
			{ break; };
		iToWrite -= iWritten;
		};

#if defined(CONFIG_OS_POSIX)
	/* ---
	Release the lock
	--- */
#if defined(CONFIG_LOCK_FLOCK)
	if ( ::flock( iFd, LOCK_UN )==-1 )
#else
	if ( ::lockf( iFd, F_ULOCK, 0 )== -1)
#endif
		{ iWritten = -1; };
	
#endif

	if ( iWritten==-1 )
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
	return PIAPI_COMPLETED;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::WaitForProcess( int tProcess, int iFlags, int iTimeout )
{
	(void)iFlags;	/* not used */
	(void)iTimeout; /* not used */
#if defined(CONFIG_OS_POSIX)
	int iRet = ::waitpid( tProcess, 0, 0 );
	if ( iRet==-1 && errno!=EINTR && errno!=ERESTART )
		{
		PIOSERR;
		return PIAPI_ERROR;
		};

	if ( iRet==tProcess )
		{ return PIAPI_COMPLETED; };

	/* --- how'd we get here? --- */
	assert( 0 );
	PIERROR( PIAPI_ERROR );
	return PIAPI_ERROR;

#elif defined(CONFIG_OS_WIN32)
	DWORD dwTimeout = ( iTimeout==-1 ) ? INFINITE : iTimeout*1000 ;
	DWORD dwWait=::WaitForSingleObject( (HANDLE)tProcess, dwTimeout );
	switch( dwWait )
		{
		case WAIT_OBJECT_0:
			return PIAPI_COMPLETED;

		case WAIT_FAILED:
			PIOSERR;
			return PIAPI_ERROR;

		case WAIT_TIMEOUT:
			PIERROR( PIAPI_TIMEOUT );
			return PIAPI_TIMEOUT;

		default:
			/* how did we get here */
			assert( 0 );
			PIERROR(PIAPI_ERROR);
			return PIAPI_ERROR;
		};
#endif
	(void)tProcess;
	return PIAPI_NOTSUPPORTED;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::LockFd( int iFd )
{
	assert( iFd!=-1 );
	if ( iFd==-1 )
		{
		PIERROR( PIAPI_EINVAL );
		return -1;
		};

#if defined(CONFIG_OS_POSIX)
	/* ---
	Enter loop to obtain a lock on the file descriptor
	--- */
	for(;;)
		{
		/* --- blocking call to lock --- */
#if defined(CONFIG_LOCK_FLOCK)
		if ( ::flock( iFd, LOCK_EX )==0 )
#else
		if ( ::lockf( iFd, F_LOCK, 0 )==0 )
#endif
			{
			/* --- got the lock, continue with accept() --- */
			break;
			}
		else if ( errno==EINTR )
			{
			/* ---
			man on solaris did not list this as a possible scenario,
			i.e. operation interrupted because a signal was caught,
			but just in case...
			--- */
			continue;
			};

		/* --- an error occurred in lock --- */
		PIOSERR;
		return PIAPI_ERROR;
		};
#endif

	return PIAPI_COMPLETED;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::UnlockFd( int iFd )
{
	assert( iFd!=-1 );
	if ( iFd==-1 )
		{
		PIERROR( PIAPI_EINVAL );
		return -1;
		};

	/* ---
	Release the lock
	--- */
#if defined(CONFIG_OS_POSIX)

#if defined(CONFIG_LOCK_FLOCK)
	if ( ::flock( iFd, LOCK_UN )==-1 )
#else
	if ( ::lockf( iFd, F_ULOCK, 0 )== -1)
#endif
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
#endif

	return PIAPI_COMPLETED;
}

#endif /* !defined(CONFIG_MT_USER) */

⌨️ 快捷键说明

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