# Stream Writer

Functions that write to operating system streams, such as mco_db_save(), must specify a stream writer function which has the following prototype:

        typedef mco_size_sig_t(*mco_stream_write) (/*IN*/ void *stream_handle, /*IN*/ const void * from, /*IN*/ unsigned nbytes);

Note that stream_handle can reference any legitimate stream (for example, a file or a socket). and from is an array from which nbytes will be written to the stream.

Also, the user-defined writer callback is not required to write the entire buffer. It may write a specific number of bytes and then return this value (less than the requested size but greater than zero). For instance if an operation was interrupted by a signal the user-defined callback will be called again by the eXtremeDB runtime to complete the buffer. The runtime option MCO_RT_OPTION_DB_SAVE_CRC can be set (see mco_runtime_setoption()) to validate the data streamed to output.

Internal buffering is implemented for the user-defined stream writer callback function. When the buffer size is reached the runtime calls the user-defined callback function to write. The default buffer size of 16 Kbytes. (A special build of the runtime is required if this needs to be altered to meet application requirements).

The user-defined callback function must return the following value ranges to the runtime:

> < 0 – system dependent error code.

> = 0 – no more data can be written.

> > 0 – number of bytes written.

An example of a stream writer implementation is the following:

        mco_size_sig_t file_writer( void *stream_handle /* FILE *  */, const void *from, mco_size_t nbytes )
        {
            FILE *f = (FILE *)stream_handle;
            mco_size_sig_t nbs;
            nbs = fwrite( from, 1, nbytes, f );
            return nbs;
        }