Sourceforge.net - The VCF's Project Host
   The VCF Website Home   |   Online Discussion Forums   |   Sourceforge.net Project Page   

VCF::TCPSocketInputStream Class Reference

An input stream class used for reading data from a TCP socket. More...

#include <vcf/NetworkKit/Socket.h>

Inheritance diagram for VCF::TCPSocketInputStream:

VCF::InputStream VCF::Stream List of all members.

Public Member Functions

 TCPSocketInputStream (TCPSocket &socket)
virtual void seek (const uint64 &offset, const SeekType &offsetFrom)
 Seeking is not permitted!
virtual uint64 getSize ()
 returns the size of the stream.
virtual uchargetBuffer ()
 Throws an exception - this is not permitted.
virtual uint64 getCurrentSeekPos ()
 Throws an exception - this is not permitted.
virtual bool isEOS ()
virtual uint64 read (unsigned char *bytesToRead, uint64 sizeOfBytes)
 Attempts to read sizeOfBytes from the TCP socket associated with this stream.

Protected Attributes

uint64 totalBytesRecvd_
Socketsocket_

Detailed Description

An input stream class used for reading data from a TCP socket.

The class must be used in conjunction with a valid socket instance, you cannot create in instance otherwise. Not all of the InputStream class's methods can be implemented, for example, the seek method is meaningless in this context, and calling it will result in a RuntimeException being thrown. The only method that makes sense to call is the read() method.

An example of how to use the class might look like this:
    Socket* socket = \\get valid socket instance from somewhere...
    SocketInputStream sis(*socket);
    uchar tmp[256];
    uint64 err = sis.read( tmp, sizeof(tmp) );
You might choose to use it in a loop, something like this:
    Socket* socket = \\get valid socket instance from somewhere...
    SocketInputStream sis(*socket);
    uchar tmp[256];
    while ( true ) {
        socket->selectFor( Socket::SelectWaitForever, Socket::ssReadable );

        do {            
            try {
                uint64 err = sis.read( tmp, sizeof(tmp) );

                if ( err == 0 ) {                   
                    break;
                }
                else {                  
                    //do something with the data stored in the 
                    //tmp buffer...
                }
            }
            catch ( BasicException& ) {
                //exit the loop!
                break;
            }
        } while( socket->pending() && socket->isReadable() ) ;


        if ( socket->isClosed() || socket->hasError() ) {
            //exit the loop if we detect problems or the socket has
            //been closed...
            break;
        }
    }

See also:
Socket::selectFor

Socket::pending


Constructor & Destructor Documentation

VCF::TCPSocketInputStream::TCPSocketInputStream TCPSocket socket  ) 
 


Member Function Documentation

virtual uchar* VCF::TCPSocketInputStream::getBuffer  )  [inline, virtual]
 

Throws an exception - this is not permitted.

Implements VCF::Stream.

virtual uint64 VCF::TCPSocketInputStream::getCurrentSeekPos  )  [inline, virtual]
 

Throws an exception - this is not permitted.

Implements VCF::Stream.

virtual uint64 VCF::TCPSocketInputStream::getSize  )  [inline, virtual]
 

returns the size of the stream.

The size represents the number of bytes that have been written, or read to/from the stream

Implements VCF::Stream.

virtual bool VCF::TCPSocketInputStream::isEOS  )  [inline, virtual]
 

Implements VCF::InputStream.

virtual uint64 VCF::TCPSocketInputStream::read unsigned char *  bytesToRead,
uint64  sizeOfBytes
[virtual]
 

Attempts to read sizeOfBytes from the TCP socket associated with this stream.

The method will return the number of bytes read from the socket. The return may be the following:

  • A number greater than 0 but less than sizeOfBytes that indicates the number of bytes successfully read from the socket.
  • 0 which may indicate a disconnect has happened and no bytes were read. If the socket is marked as non-blocking then the method will return 0 bytes, but the sockets Socket::wouldOperationBlock() method will return true. This indicates that the read operation will be completable in the future. If the socket is a blocking socket, then this is probably a disconnect.
  • If the socket's peer reports an error in reading from the socket, then the method will throw a NetworkException.

Implements VCF::InputStream.

virtual void VCF::TCPSocketInputStream::seek const uint64 offset,
const SeekType offsetFrom
[inline, virtual]
 

Seeking is not permitted!

Implements VCF::Stream.


Member Data Documentation

Socket* VCF::TCPSocketInputStream::socket_ [protected]
 

uint64 VCF::TCPSocketInputStream::totalBytesRecvd_ [protected]
 


The documentation for this class was generated from the following file:
   Comments or Suggestions?    License Information