Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / XML

Using InsomniaServer to Build a Web-interface for your Application

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
15 Jul 2011CPOL3 min read 83K   1.2K   41  
InsomniaServer enables you to add a fully-featured, customizable webserver to your projects. See how it works.
//All code is copyright � 2007, InsomniaSoftware | Manuel Then, All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace InsomniaSoftware.Server.Sources
{
    /// <summary>
    /// Represents a source which is able to send a stream to the client
    /// </summary>
    public class StreamSource : Source
    {
        Stream stream;
        ContentType type;
        SourceInfo sourceInfo = null;
        bool closeAfterRequest;


        /// <summary>
        /// Creates a new StreamSource instance
        /// </summary>
        /// <param name="stream">Stream to be sent to the client</param>
        /// <param name="contentType">ContentType of the data within the stream</param>
        /// <param name="closeAfterRequest">If true, the stream is disposed after the end of this request</param>
        public StreamSource(Stream stream, ContentType contentType, bool closeAfterRequest)
        {
            if (stream == null)
                throw new Exception("Passed stream must not be null");
            if (!stream.CanRead)
                throw new Exception("Passed stream must be readable");
            if (!stream.CanSeek)
                throw new Exception("Passed stream must be seekable");
            this.stream = stream;
            this.type = contentType;
            this.closeAfterRequest = closeAfterRequest;
        }

        /// <summary>
        /// Creates a new StreamSource instance
        /// </summary>
        /// <param name="stream">Stream to be sent to the client</param>
        /// <param name="contentType">ContentType of the data within the stream</param>
        /// <param name="info">SourceInfo for this source</param>
        /// /// <param name="closeAfterRequest">If true, the stream is disposed after the end of this request</param>
        public StreamSource(Stream stream, ContentType contentType, SourceInfo info, bool closeAfterRequest)
            : this(stream, contentType, closeAfterRequest)
        {
            this.sourceInfo = info;
        }

        /// <summary>
        /// Destructor
        /// </summary>
        ~StreamSource()
        {
            Dispose();
        }

        /// <summary>
        /// Disposes the StreamSource
        /// </summary>
        public override void Dispose()
        {
            try
            {
                if (closeAfterRequest)
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                        stream = null;
                    }
                }
            }
            catch { }
        }


        /// <summary>
        /// Gets the length of the source's content
        /// </summary>
        /// <returns>Content length</returns>
        public override int GetLength()
        {
            return (int)stream.Length;
        }

        /// <summary>
        /// Moves current the position withing the binary data
        /// </summary>
        /// <param name="offset">Offset by which the location will move</param>
        /// <param name="origin">Position to start moving at</param>
        /// <returns>The absolute position within the data</returns>
        public override int Seek(int offset, SeekOrigin origin)
        {
            return (int)stream.Seek(offset, origin);
        }

        /// <summary>
        /// Reads a specified number of bytes into the given buffer
        /// </summary>
        /// <param name="buffer">Buffer to be filled</param>
        /// <param name="offset">First position in the buffer to be filled</param>
        /// <param name="count">Number of bytes to be read</param>
        /// <returns>The actual number of bytes read</returns>
        public override int Read(ref byte[] buffer, int offset, int count)
        {
            return stream.Read(buffer, offset, count);
        }

        /// <summary>
        /// Gets the content type of this source's content
        /// </summary>
        /// <returns>ContentType instance</returns>
        public override ContentType GetContentType()
        {
            return type;
        }

        /// <summary>
        /// Gets the SourceInfo
        /// </summary>
        /// <returns>SourceInfo of this StreamSource</returns>
        public override SourceInfo  GetInformation()
        {
            if (sourceInfo != null)
                return sourceInfo;
            else
                return new SourceInfo();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
I was born in 1987. Unfortunately too late to experience the real rise of the PC. But fortunately late enough to enjoy things like MS's .net during my school time Wink | ;)

From the time when some relative taught me a little BASIC under MS DOS, I loved to tell computers what to do - even though my real start in programming was around the age of 16.

At the moment, I am studying Software Engineering at University of Augsburg, always hoping to find time to design and program.
Besides, I like meeting friends, spent time with my girlfriend and enjoy life Smile | :)

Comments and Discussions