Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C#

A Fast/Compact Serialization Framework

Rate me:
Please Sign up or sign in to vote.
4.85/5 (37 votes)
13 Oct 2010GPL35 min read 283.5K   1.2K   175  
A framework for object serializiation/deserialization that is many times faster and yields a compact output.
/*

OpenNxSerialization Framework
Copyright (C) 2006 - 2010 "NeXtreme Innovations"
[The Next Xtreme Innovations]

This program is free software, distributed under the terms of
the GNU General Public License Version 2. See the "License.txt" file
at the top of the source tree.

*/
using System;
using System.Runtime.Remoting;
using System.Collections;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Http;

using NeXtreme.OpenNxSerialization.Remoting.Channels;
using NeXtreme.OpenNxSerialization;
using NeXtreme.CommonTypes;
using NeXtreme.OpenNxSerialization.Native;

namespace NeXtreme.Remoting.Client 
{
    enum ChannelType
    {
        Ipc,
        Tcp,
        Http
    };

    public partial class MyClient : MarshalByRefObject
	{
        private static void CreateFormatterProviders(bool useNx, 
                                                    out IClientChannelSinkProvider ccfProv,
                                                    out IServerChannelSinkProvider scfProv)
        {
            IDictionary properties = new Hashtable();
            properties["useNativeProvider"] = useNx.ToString();

            ccfProv = null;
            scfProv = null;

            if (useNx)
            {
                NxFormatterServices.Default.RegisterKnownType(typeof(Service), 10001);
                NxFormatterServices.Default.RegisterKnownType(typeof(ServiceToken), 10002);
                NxFormatterServices.Default.RegisterKnownType(typeof(NeXtreme.Remoting.Client.MyClient), 10003);

                scfProv = new NxBinaryServerFormatterSinkProvider(properties, null);
                ccfProv = new NxBinaryClientFormatterSinkProvider(properties, null);
            }
            else
            {
                BinaryServerFormatterSinkProvider bsf = new BinaryServerFormatterSinkProvider(null, null);
                BinaryClientFormatterSinkProvider csf = new BinaryClientFormatterSinkProvider(null, null);

                bsf.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

                scfProv = bsf;
                ccfProv = csf;
            }
        }


        private static void RegisterChannels(int port, 
                                            ChannelType type,
                                            IClientChannelSinkProvider ccfProv,
                                            IServerChannelSinkProvider scfProv)
        {
            IDictionary properties = new Hashtable();
            IChannel channel = null;

            properties["name"] = "_ClientChannel";
            switch (type)
            { 
                case ChannelType.Ipc:
                    properties["priority"] = "20";
                    properties["portName"] = "localhost:" + port.ToString();
                    channel = new IpcChannel(properties, ccfProv, scfProv);
                    break;
                case ChannelType.Tcp:
                    properties["port"] = port;
                    channel = new TcpChannel(properties, ccfProv, scfProv);
                    break;
                case ChannelType.Http:
                    properties["port"] = port;
                    channel = new HttpChannel(properties, ccfProv, scfProv);
                    break;
            }

            ChannelServices.RegisterChannel(channel, false);
        }

        private static void Configure(int port, ChannelType type, bool useNx)
        {
            IClientChannelSinkProvider ccfProv;
            IServerChannelSinkProvider scfProv;
            CreateFormatterProviders(useNx, out ccfProv, out scfProv);
            RegisterChannels(port, type, ccfProv, scfProv);

            Console.WriteLine("Channel: " + type.ToString());
            Console.WriteLine("NxFormmater: " + useNx);
            Console.WriteLine("Port: " + port);
        }
    }
}

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 GNU General Public License (GPLv3)


Written By
Architect
Pakistan Pakistan
Let a = b ....... (1)
a - b = a - b
a^2 - ab = a^2 - ab
a^2 - ab = a^2 - b^2 (from 1)
a (a - b) = (a + b) (a - b)
a = (a + b) ...... (2)

if a = 1
1 = (1 + 1) (from 1 & 2)
1 = 2 !!

Comments and Discussions