Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cNetStream Class Reference

#include <cNetStream.h>

Inheritance diagram for GrayLib::cNetStream:
Gray::cStream Gray::cStreamStats Gray::cStreamInput Gray::cStreamOutput Gray::cStreamBase Gray::cStreamBase GrayLib::cNetServerConnection

Public Member Functions

 cNetStream (SOCKET hSocket=INVALID_SOCKET, bool bAsync=false, size_t nRxBufferLimit=(cStream::k_FILE_BLOCK_SIZE/2))
 
virtual ~cNetStream (void)
 
bool isConnected () const
 
bool isConnecting () const noexcept
 
bool isValidSocket () const noexcept
 
HASHCODE_t get_HashCode () const noexcept
 
bool isInputDesired () const
 
bool isInputEmpty () const
 
bool isOutputEmpty () const
 
ITERATE_t get_InputQty () const
 
ITERATE_t get_OutputQty () const
 
virtual HRESULT onEventWrite ()
 override these if we want to know if data is ready to TX or RX More...
 
virtual HRESULT onEventRead ()
 FD_WRITE similar to MFC OnReceive. More...
 
HRESULT UpdateQWrite ()
 
HRESULT UpdateQRead ()
 
cStreamOutputget_StreamOut ()
 
cStreamInputget_StreamInp ()
 
virtual HRESULT WriteX (const void *pData, size_t nDataSize) override
 
virtual HRESULT ReadX (void *pData, size_t nDataSize) override
 
virtual size_t SetSeekSizeMin (size_t nSizeMin=k_FILE_BLOCK_SIZE) override
 
virtual STREAM_SEEKRET_t Seek (STREAM_OFFSET_t iOffset, SEEK_ORIGIN_TYPE eSeekOrigin) override
 Disambiguate Seek for cStreamBase to cStreamInput for stupid compiler. More...
 
virtual void Close ()
 
void SetStateEstablished ()
 
 UNITTEST_FRIEND (cNetStream)
 
- Public Member Functions inherited from Gray::cStream
virtual STREAM_POS_t GetPosition () const override
 
virtual STREAM_POS_t GetLength () const override
 
void SeekToBegin ()
 
STREAM_POS_t SeekToEnd ()
 
- Public Member Functions inherited from Gray::cStreamInput
 cStreamInput () noexcept
 
virtual ~cStreamInput ()
 
virtual HRESULT ReadX (OUT void *pData, size_t nDataSize)
 
HRESULT ReadAll (OUT cHeapBlock &block, size_t nSizeExtra=0)
 
virtual HRESULT ReadStringLine (OUT char *pszBuffer, StrLen_t iSizeMax)
 
virtual HRESULT ReadStringLine (OUT wchar_t *pszBuffer, StrLen_t iSizeMax)
 
HRESULT ReadT (OUT void *pVal, size_t nSize)
 
template<typename TYPE >
HRESULT ReadT (OUT TYPE &val)
 
template<typename TYPE >
HRESULT ReadTN (OUT TYPE &val)
 
HRESULT ReadSize (OUT size_t &nSize)
 
template<typename TYPE >
HRESULT ReadSizeT (OUT TYPE &n)
 
HRESULT ReadHashCode (OUT UINT32 &nHashCode)
 
HRESULT ReadHashCode (OUT UINT64 &nHashCode)
 
HRESULT ReadN (OUT BYTE *pBuffer, size_t nSizeMax)
 
template<typename _CH >
HRESULT ReadStringN (OUT _CH *pszStr, StrLen_t iSizeMax)
 
virtual HRESULT ReadPeek (void *pData, size_t nDataSize)
 
- Public Member Functions inherited from Gray::cStreamBase
virtual ~cStreamBase ()
 
void SeekToBegin ()
 
STREAM_POS_t SeekToEnd ()
 
- Public Member Functions inherited from Gray::cStreamOutput
 cStreamOutput () noexcept
 
virtual ~cStreamOutput ()
 
HRESULT WriteT (const void *pVal, size_t nDataSize)
 
template<typename TYPE >
HRESULT WriteT (TYPE val)
 
HRESULT WriteSize (size_t nSize)
 
HRESULT WriteHashCode (HASHCODE_t nHashCode)
 
HRESULT WriteN (const void *pBuffer, size_t nSize)
 
template<typename _CH >
HRESULT WriteStringN (const _CH *pszStr)
 
template<typename _CH >
HRESULT WriteCharRepeat (_CH nChar, int nCount=1)
 
virtual HRESULT WriteString (const char *pszStr)
 
virtual HRESULT WriteString (const wchar_t *pszStr)
 
StrLen_t VPrintf (const char *pszFormat, va_list args)
 
StrLen_t VPrintf (const wchar_t *pszFormat, va_list args)
 
StrLen_t _cdecl Printf (const char *pszFormat,...)
 
StrLen_t _cdecl Printf (const wchar_t *pszFormat,...)
 
HRESULT WriteStream (cStreamInput &sInp, STREAM_POS_t nSizeMax=k_FILE_BLOCK_SIZE, IStreamProgressCallback *pProgress=nullptr, TIMESYSD_t nTimeout=0)
 Copy cStreamInput to this stream. More...
 
virtual HRESULT FlushX ()
 
- Public Member Functions inherited from Gray::cStreamStats
void Add (const cStreamStats &n)
 

Public Attributes

cNetSocket m_Socket
 the raw TCP SOCKET we are buffering to/from. More...
 
bool m_bAsync
 Will this socket be using OnEvent FD_READ FD_WRITE events ? More...
 
bool m_bInpThrottled
 The m_bAsync input was throttled. we cant rely on m_bAsync OnEvent to notify us again. we must poll. More...
 
cStreamQueue m_bout
 raw output buffer to m_Socket. can write to this externally if m_bAsync. More...
 
cStreamQueue m_binp
 raw input buffer from m_Socket. can read from this externally if m_bAsync. More...
 
HRESULT m_hResultRx
 The status of the connect or disconnect. S_OK = connected. else how to it close ? don't return this until we read all the m_binp data. More...
 
- Public Attributes inherited from Gray::cStreamStats
cStreamStat m_StatOut
 
cStreamStat m_StatInp
 

Additional Inherited Members

- Static Public Attributes inherited from Gray::cStreamBase
static const BYTE k_SIZE_MASK = 0x80
 Used for WriteSize() More...
 
static const size_t k_FILE_BLOCK_SIZE = (32 * 1024)
 default arbitrary transfer block size. more than this is NOT more efficient. More...
 

Detailed Description

This is wrapper for cNetSocket with extra level of buffering (usually for TCP). both input and output buffered streams. (arbitrary buffer size) We can use Seek() to back up the read queue. (if we didn't get(RX) enough data to fill an atomic transaction)

Note
The normal default size SOCKET buffer isn't big enough by default (without put_BufferSizeRx) and doesn't allow Seek().

Constructor & Destructor Documentation

◆ cNetStream()

GrayLib::cNetStream::cNetStream ( SOCKET  hSocket = INVALID_SOCKET,
bool  bAsync = false,
size_t  nRxBufferLimit = (cStream::k_FILE_BLOCK_SIZE / 2) 
)
  • nRxBufferLimit = Max size of the buffer. Must at least hold a max sized message. nRxBufferLimit = 0 = write only stream.

◆ ~cNetStream()

GrayLib::cNetStream::~cNetStream ( void  )
virtual

Member Function Documentation

◆ Close()

void GrayLib::cNetStream::Close ( void  )
virtual

cStream. Force a close.

Note
Don't clear m_binp. i can still read what i got last even though the socket is closed.

Reimplemented in GrayLib::cNetServerConnection.

◆ get_HashCode()

HASHCODE_t GrayLib::cNetStream::get_HashCode ( ) const
inlinenoexcept

Use the socket id as a unique id for this stream.

◆ get_InputQty()

ITERATE_t GrayLib::cNetStream::get_InputQty ( ) const
inline

◆ get_OutputQty()

ITERATE_t GrayLib::cNetStream::get_OutputQty ( ) const
inline

◆ get_StreamInp()

cStreamInput* GrayLib::cNetStream::get_StreamInp ( )
inline

◆ get_StreamOut()

cStreamOutput* GrayLib::cNetStream::get_StreamOut ( )
inline

◆ isConnected()

bool GrayLib::cNetStream::isConnected ( ) const
inline

Actually connected (or connecting) to a peer.

◆ isConnecting()

bool GrayLib::cNetStream::isConnecting ( ) const
inlinenoexcept

connecting to a peer.

◆ isInputDesired()

bool GrayLib::cNetStream::isInputDesired ( ) const
inline

Is this a write only socket? No input buffer size = write only.

◆ isInputEmpty()

bool GrayLib::cNetStream::isInputEmpty ( ) const
inline

no data to read?

◆ isOutputEmpty()

bool GrayLib::cNetStream::isOutputEmpty ( ) const
inline

no data to write?

◆ isValidSocket()

bool GrayLib::cNetStream::isValidSocket ( ) const
inlinenoexcept

socket handle is valid.

◆ onEventRead()

HRESULT GrayLib::cNetStream::onEventRead ( )
virtual

FD_WRITE similar to MFC OnReceive.

Event FD_READ or polled frequently. Read and queue up any data that is available.

Note
default read high water limit = cStream::k_FILE_BLOCK_SIZE/2
Returns
0 = nothing here. <0 = error. HRESULT_WIN32_C(WSAECONNABORTED), HRESULT_WIN32_C(ERROR_DEVICE_NOT_CONNECTED) Number of bytes size new data;

Reimplemented in GrayLib::cNetServerConnection.

◆ onEventWrite()

HRESULT GrayLib::cNetStream::onEventWrite ( )
virtual

override these if we want to know if data is ready to TX or RX

FD_READ similar to MFC OnSend

use event FD_WRITE if possible. else someone needs to poll this Check if TX is ready/needed?

Returns
<0 = error 0 = schedule this again, nothing written?

◆ ReadX()

HRESULT GrayLib::cNetStream::ReadX ( void *  pData,
size_t  nDataSize 
)
overridevirtual

Read from cStream Read incoming data from the stream from socket. fill stream buffer from socket if needed.

◆ Seek()

STREAM_SEEKRET_t GrayLib::cNetStream::Seek ( STREAM_OFFSET_t  iOffset,
SEEK_ORIGIN_TYPE  eSeekOrigin 
)
overridevirtual

Disambiguate Seek for cStreamBase to cStreamInput for stupid compiler.

Seek to some read position in the stored data.

Returns
current position. used: I haven't read a full packet so try to undo my last read.

Reimplemented from Gray::cStream.

◆ SetSeekSizeMin()

size_t GrayLib::cNetStream::SetSeekSizeMin ( size_t  nSizeMin = k_FILE_BLOCK_SIZE)
overridevirtual

There are no incomplete transactions read.

Reimplemented from Gray::cStreamInput.

◆ SetStateEstablished()

void GrayLib::cNetStream::SetStateEstablished ( )
inline

A successful blocking outgoing connection indicates we are ready to RX. FD_CONNECT.

◆ UNITTEST_FRIEND()

GrayLib::cNetStream::UNITTEST_FRIEND ( cNetStream  )

◆ UpdateQRead()

HRESULT GrayLib::cNetStream::UpdateQRead ( )

◆ UpdateQWrite()

HRESULT GrayLib::cNetStream::UpdateQWrite ( )

◆ WriteX()

HRESULT GrayLib::cNetStream::WriteX ( const void *  pData,
size_t  nDataSize 
)
overridevirtual

Write to cStream

  • pData == nullptr = just test if i could write this much.
    Returns
    How much data did i write ? 0 = full for now.
    Note
    Caller is responsible for throttling output !

Reimplemented from Gray::cStreamOutput.

Member Data Documentation

◆ m_bAsync

bool GrayLib::cNetStream::m_bAsync

Will this socket be using OnEvent FD_READ FD_WRITE events ?

◆ m_binp

cStreamQueue GrayLib::cNetStream::m_binp

raw input buffer from m_Socket. can read from this externally if m_bAsync.

◆ m_bInpThrottled

bool GrayLib::cNetStream::m_bInpThrottled

The m_bAsync input was throttled. we cant rely on m_bAsync OnEvent to notify us again. we must poll.

◆ m_bout

cStreamQueue GrayLib::cNetStream::m_bout

raw output buffer to m_Socket. can write to this externally if m_bAsync.

◆ m_hResultRx

HRESULT GrayLib::cNetStream::m_hResultRx

The status of the connect or disconnect. S_OK = connected. else how to it close ? don't return this until we read all the m_binp data.

◆ m_Socket

cNetSocket GrayLib::cNetStream::m_Socket

the raw TCP SOCKET we are buffering to/from.


The documentation for this class was generated from the following files: