/*
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);
}
}
}