/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_BUFFERED_I_NET_STREAM_HPP
#define NMM_BUFFERED_I_NET_STREAM_HPP NMM_BUFFERED_I_NET_STREAM_HPP

#include "nmm/nmm_prefix.hpp"

#include "INetStream.hpp"

#include "nmm/comm/serialize/TValue.hpp"

#include "visibility.hpp"

namespace NMM {

  /** Buffered network input stream.
   * This class is a network input stream with an internal buffer for incoming data.
   *
   * Incoming data is read in large quantities to reduce the number of system calls.
   * The implementation tries to read all available data into the buffer if less than the
   * requested amount of data is already buffered.
   */
  class DLLEXPORT BufferedINetStream
    : public INetStream
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Constructor.
     */
    BufferedINetStream();

    /** Copy constructor.
     *
     * @param s Object to copy from
     */
    BufferedINetStream(const BufferedINetStream& s);

    /** Virtual destructor.
     */
    virtual ~BufferedINetStream();

    /** Virtual copy constructor.
     *
     * @return A copy of this object which has the same type
     */
    virtual BufferedINetStream* copy() const;
    //@}

    /** Check if this stream is empty.
     * This method checks if this stream contains no data to read.
     * If this method returns @c true, serialization operations will fail.
     *
     * This implementation returns @c false iff the internal buffer is empty.
     *
     * @return @c true if this stream is empty, @c false otherwise
     */
    virtual bool isEmpty() const;

    /** Set the size of the internal buffer.
     * This method sets the size of the buffer which is used for storing incoming data
     * within the stream.
     *
     * This method does not reallocate the buffer. The buffer is allocated on demand.
     * The buffer size is a serialized attribute of this class.
     *
     * @param size Size of the buffer (in bytes)
     */
    inline void setReceiveBufferSize(size_t size) { __buffer_size.setValue(size); }

    /** Get the size of the internal buffer.
     * This method gets the size of the buffer which is used for storing incoming data
     * within the stream.
     *
     * The buffer size is a serialized attribute of this class.
     *
     * @return Size of the buffer (in bytes)
     */
    inline size_t getReceiveBufferSize() const    { return __buffer_size.getRValue(); }

  protected:
    /** Subclass constructor.
     * This constructor must be used by subclasses when initializing the base class.
     * Every subclass must also define this subclass constructor as a protected constructor.
     *
     * @param ti Type information for this object
     */
    BufferedINetStream(const TypeInfo& ti);

    /** Socket read method.
     * This method performs the socket read operation. It is invoked by the implementation
     * of the serialization operators.
     *
     * This implementation performs the buffered read operation.
     * It also allocates the internal buffer if the buffer size has changed.
     *
     * @param data Binary data to be serialized
     * @param size Size of binary data to serialize (in bytes)
     */
    void readValue(void* data, size_t size);

    /** Direct socket read method.
     * This method performs the actual socket read operation. It is invoked by the implementation
     * of readValue().
     *
     * @param data Binary data to be serialized
     * @param size Size of binary data to serialize (in bytes)
     * @return Number of bytes read from the socket
     */
    size_t internalReadValue(void* data, size_t size);

    /** Allocate internal buffer.
     * This method reallocates the internal buffer if the buffer size has changed.
     */
    void allocateBuffer();

  private:
    TValue<nmm_uint32>  __buffer_size;        ///< Size of internal buffer.

    vector<char>    __recv_buffer;        ///< Internal buffer for incoming data
    size_t          __recv_buffer_begin;  ///< Pointer to beginning of used part of buffer
    size_t          __recv_buffer_end;    ///< Pointer to end of used part of buffer
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
