/* 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_O_NET_STREAM_HPP
#define NMM_BUFFERED_O_NET_STREAM_HPP NMM_BUFFERED_O_NET_STREAM_HPP

#include "nmm/nmm_prefix.hpp"

#include "ONetStream.hpp"

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

#include "visibility.hpp"

namespace NMM {

  /** Buffered network output stream.
   * This class is a network output stream with an internal buffer for outgoing data.
   *
   * Outgoing data is first written into the buffer and sent when the buffer is full.
   * Larger quantities of data are sent immediately.
   * The output buffer is flushed when writeData() is called.
   * This reduces the number of system calls and TCP packets.
   */
  class DLLEXPORT BufferedONetStream
    : public ONetStream
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Constructor.
     */
    BufferedONetStream();

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

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

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

    /** Write serialized data.
     * This method must be called after all data has been serialized.
     * It flushes written data internally.
     *
     * This implementation writes all buffered data to the socket.
     */
    virtual void writeData();

    /** Set the size of the internal buffer.
     * This method sets the size of the buffer which is used for storing outgoing 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 setSendBufferSize(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 outgoing data
     * within the stream.
     *
     * The buffer size is a serialized attribute of this class.
     *
     * @return Size of the buffer (in bytes)
     */
    inline size_t getSendBufferSize() 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
     */
    BufferedONetStream(const TypeInfo& ti);

    /** Socket write method.
     * This method performs the actual socket write operation. It is invoked by the implementation
     * of the serialization operators. Subclasses must implement this method.
     *
     * This implementation performs the buffered write 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 writeValue(const void* data, size_t size);

    /** Flush the internal buffer.
     * This method writes all buffered data to the socket.
     */
    void flush();

    /** Direct socket write method.
     * This method performs the actual socket write operation. It is invoked by the implementation
     * of writeValue().
     *
     * @param data Binary data to be serialized
     * @param size Size of binary data to serialize (in bytes)
     */
    void internalWriteValue(const 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>    __send_buffer;        ///< Internal buffer for outgoing data
    size_t          __send_buffer_used;   ///< Number of bytes in the internal buffer
  };

}

#include "nmm/nmm_suffix.hpp"

#endif
