📄 piiobuf.cpp
字号:
/*____________________________________________________________________________*\
*
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.cpp,v $
* $Date: 2003/05/13 18:42:10 $
*
Description:
TODO:
- really need some output buffering here
\*____________________________________________________________________________*/
//$SourceTop:$
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include "PICompat.h"
#include "PIIOBuf.h"
#include "HTTPDefs.h"
/*
#define D { cerr << __FILE__ << ": " << __LINE__ << endl; };
*/
#define D
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
inline int MyStrLen( const char *pS )
{ return (int)strlen( pS ); };
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Flush the output buffer. Returns >0 on success, 0 on failure.
\*____________________________________________________________________________*/
int PIIOBuffer::FlushOutput( int iCloseChunk )
{
int iWritten = 0;
int iIntWritten = 0;
if ( iOutBufferSize==0 )
{
if ( iCloseChunk && iChunked )
{
if ( PIIO_out( pIOObject, (void *)"0\r\n\r\n", 5, &iIntWritten, 0, 0 ))
{
iOutIOError = 1;
return 0;
}
else
{
iBytesSent += iIntWritten;
};
iChunked = 0;
}
return 1;
};
assert( pOutBuffer );
if ( iOutIOError )
{ return 0; };
if (iChunked)
{
char szBuf[16]; // depends on OUT_BUFFER_SIZE
sprintf(szBuf, "%x\r\n", iOutBufferSize );
if (PIIO_out( pIOObject, (void *)(szBuf), strlen(szBuf), &iIntWritten, 0, 0 ) )
{ iOutIOError = 1; return 0; };
}
else
{
iBytesSent += iIntWritten;
};
if ( PIIO_out( pIOObject, (void *)pOutBuffer, iOutBufferSize, &iWritten,
0, 0 ) )
{ iOutIOError = 1; return 0; };
assert( iWritten<=iOutBufferSize );
iBytesSent += iWritten;
if ( iWritten<iOutBufferSize )
{
memcpy( pOutBuffer, &( pOutBuffer[iWritten] ),
iOutBufferSize-iWritten );
iOutBufferSize -= iWritten;
int iRet = FlushOutput(0);
if ( !iRet )
{ return 0; };
iWritten += iRet;
};
assert( iWritten<=iOutBufferSize );
iOutBufferSize = 0;
if ( iChunked
&& PIIO_out( pIOObject, (void *)"\r\n", 2, &iIntWritten, 0, 0 ) )
{
iOutIOError = 1;
return 0;
}
else
{
iBytesSent += iIntWritten;
};
if ( iCloseChunk && iChunked )
{
if ( PIIO_out( pIOObject, (void *)"0\r\n\r\n", 5, &iIntWritten, 0, 0 ))
{
iOutIOError = 1;
return 0;
}
else
{
iBytesSent += iIntWritten;
};
iChunked = 0;
};
return iWritten;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Append output data to the internal buffer, or flush straight out depending
on flags. Returns bytes written on success, 0 on failure.
\*____________________________________________________________________________*/
int PIIOBuffer::Internal_Write( const char *pS, int iLen, int iFlags )
{
assert( pS );
assert( pOutBuffer );
int iWritten = 0;
/* --- buffering disabled and nothing currently in the buffer --- */
if ( ( iFlags & PIIOBUF_NOBUFFER ) && ( iOutBufferSize==0) )
{
if ( iOutIOError )
{ return 0; };
if ( PIIO_out( pIOObject, (void *)pS, iLen,
&iWritten, 0, 0 ) )
{ iOutIOError = 1; return 0; };
return iWritten;
};
/* --- data in the buffer --- */
if ( iOutBufferSize<OUT_BUFFER_SIZE )
{
int iToWrite = OUT_BUFFER_SIZE-iOutBufferSize;
if ( iToWrite>iLen )
{ iToWrite=iLen; };
memcpy( &( pOutBuffer[iOutBufferSize] ), pS, iToWrite );
iOutBufferSize+=iToWrite;
iLen -= iToWrite;
if ( iFlags & PIIOBUF_NOBUFFER )
{
if ( !FlushOutput(0) )
{
return 0;
};
};
if ( !iLen )
{
return iToWrite;
};
pS = &( pS[iToWrite] );
iWritten += iToWrite;
};
if ( !FlushOutput(0) )
{ return 0; };
int iTmp = Internal_Write( pS, iLen, iFlags );
if ( !iTmp )
{ return 0; };
return iWritten+iTmp;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: private
Description:
Grows the data in the internal buffer pBuffer by reading additional
data from the IO object. Returns length of data read or 0 if
no more data is available. Returns -1 if the buffer is full or an IO error
occurred.
\*____________________________________________________________________________*/
int PIIOBuffer::GrowData()
{
int iRead;
if ( iBufferLen==IN_BUF_SIZE )
{ return -1; };
if ( PIIO_in(
pIOObject,
&(szInBuf[iBufferLen]),
IN_BUF_SIZE-iBufferLen,
&iRead,
0, 0 ) )
{ return -1; };
iBufferLen += iRead;
return iRead;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Poll for incoming data. If there is data in the read buffer, then
return 1. If data was succesfully read from the input stream then
return 1. Otherwise 0 is returned on a timeout condition. On error
this function returns -1.
\*____________________________________________________________________________*/
int PIIOBuffer::PollBeforeRead()
{
if ( iBufferLen )
{ /* --- data in buffer --- */ return 1; };
int iRet = GrowData();
switch( iRet )
{
case 0:
case -1:
return iRet;
default:
return 1;
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int PIIOBuffer::Write( const char *pS, int iLen, int iFlags )
{
assert( pS );
iChunked |= iFlags & PIIOBUF_CHUNKED;
if ( iLen==-1 )
{
iLen = MyStrLen( pS );
};
int iWritten = 0;
if ( iLen && !( iWritten = Internal_Write( pS, iLen, iFlags ) ) )
{
return -1;
};
return iWritten;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int PIIOBuffer::WriteLn( const char *pS, int iLen, int iFlags )
{
assert( pS );
iChunked |= iFlags & PIIOBUF_CHUNKED;
if ( iLen==-1 )
{ iLen = MyStrLen( pS ); };
int iWritten = 0;
if ( iLen && !( iWritten = Internal_Write( pS, iLen, iFlags ) ) )
{ return -1; };
int iTotal = iWritten;
iWritten = Internal_Write( HTTP_CRLF, 2, iFlags );
if ( !iWritten )
{ return -1; };
iTotal += iWritten;
return iTotal;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Return the internal buffer, and put length into the integer at piRead.
Return 0 if no data if available or an error occurs.
\*____________________________________________________________________________*/
const char *PIIOBuffer::Read( int *piRead )
{
assert( piRead );
if ( !piRead )
{ return 0; };
/* ---
If there is data in the buffer, use it
--- */
if ( iBufferLen )
{
*piRead = iBufferLen;
iBufferLen = 0;
return szInBuf;
};
/* ---
Otherwise read in some more
--- */
switch( GrowData() )
{
case 0: /* --- no more data --- */
case -1: /* --- IO error --- */
return 0;
default:
/* got more data */
;
};
/* --- recurse to return the data block --- */
return Read( piRead );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Write up to iMaxLen characters into the buffer pszBuffer from
the input and return the number of characters written.
Returns 0 on EOF or -1 on error.
\*____________________________________________________________________________*/
int PIIOBuffer::Read( char *pszBuffer, int iMaxLen )
{
assert( pszBuffer && iMaxLen );
if ( !pszBuffer || !iMaxLen )
{ return -1; };
/* ---
If there is data in the buffer, use it
--- */
if ( iBufferLen )
{
if ( iBufferLen>iMaxLen )
{
/* ---
More characters are in the buffer than we want to read.
--- */
memcpy( pszBuffer, szInBuf, iMaxLen );
iBufferLen -= iMaxLen;
memmove( szInBuf, &(szInBuf[iMaxLen]), iBufferLen );
return iMaxLen;
}
else
{
memcpy( pszBuffer, szInBuf, iBufferLen );
int iTmp = iBufferLen;
iBufferLen = 0;
return iTmp;
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -