Esempio n. 1
0
 /// <summary>
 ///     第三方数据转换成元数据
 /// </summary>
 /// <param name="proxy">内存段实例</param>
 /// <param name="baseValueMessage">存储属性的实例对象</param>
 /// <exception cref="ArgumentNullException">参数不能为空</exception>
 public void ValueProcessor(IMemorySegmentProxy proxy, BaseValueStored baseValueMessage)
 {
     if (proxy == null)
     {
         throw new ArgumentNullException("proxy");
     }
     if (baseValueMessage == null)
     {
         throw new ArgumentNullException("baseValueMessage");
     }
     ResourceBlock[] value = baseValueMessage.GetValue <ResourceBlock[]>();
     proxy.WriteUInt32((uint)value.Length);
     for (int i = 0; i < value.Length; i++)
     {
         if (value[i] == null)
         {
             proxy.WriteUInt32(0);
             continue;
         }
         MetadataObjectEngine.ToBytes(value[i], proxy);
     }
 }
Esempio n. 2
0
        private void InnerHightSpeedSendTest(int destinationInstanceCount, int destinationMsgCount, int port)
        {
            int            msgRecvCount = 0, sendMsgCount = 0;
            int            previousMsgRecvCount = 0, previousSendMsgCount = 0;
            AutoResetEvent msgEvent = new AutoResetEvent(false);

            Console.Write("#Begining prepare Host channel resource...");
            TcpHostTransportChannel hostChannel = new TcpHostTransportChannel(port);

            Assert.IsTrue(hostChannel.Regist());
            hostChannel.ChannelCreated += delegate(object sender, LightSingleArgEventArgs <ITransportChannel> args)
            {
                MessageTransportChannel <MetadataContainer> msgChannel = new MessageTransportChannel <MetadataContainer>((IRawTransportChannel)args.Target, new MetadataProtocolStack());
                msgChannel.ReceivedMessage += delegate(object s, LightSingleArgEventArgs <List <MetadataContainer> > a)
                {
                    Interlocked.Add(ref msgRecvCount, a.Target.Count);
                };
            };
            Console.WriteLine("Done");
            Console.Write("#Begining prepare {0} numbers of TCP channel...", destinationInstanceCount);
            IList <TcpTransportChannel> clients = new List <TcpTransportChannel>();

            for (int i = 0; i < destinationInstanceCount; i++)
            {
                TcpTransportChannel transportChannel = new TcpTransportChannel("127.0.0.1", port);
                Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Unknown);
                transportChannel.Connect();
                Assert.IsTrue(transportChannel.IsConnected);
                Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Opened);
                clients.Add(transportChannel);
            }
            Console.WriteLine("Done");
            MetadataContainer container = new MetadataContainer();

            container.SetAttribute(0x00, new StringValueStored("Test-Name"));
            container.SetAttribute(0x01, new StringValueStored("Test-Value"));
            byte[] data = MetadataObjectEngine.ToBytes(container);
            Assert.IsNotNull(data);
            Console.WriteLine("#Sending message on those of transport channels...");
            ThreadPool.QueueUserWorkItem(delegate
            {
                while (true)
                {
                    previousMsgRecvCount = msgRecvCount;
                    previousSendMsgCount = sendMsgCount;
                    Thread.Sleep(1000);
                    if (destinationMsgCount != previousSendMsgCount || destinationMsgCount != previousMsgRecvCount)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Send Count: {0}    <---{1} messages/per-second.", sendMsgCount, sendMsgCount - previousSendMsgCount);
                        Console.WriteLine("Recv Count: {0}    <---{1} messages/per-second.", msgRecvCount, msgRecvCount - previousMsgRecvCount);
                        Console.WriteLine();
                    }
                }
            });
            ThreadPool.QueueUserWorkItem(delegate
            {
                while (sendMsgCount != destinationMsgCount)
                {
                    for (int i = 0; i < clients.Count; i++)
                    {
                        clients[i].Send(data);
                        Interlocked.Increment(ref sendMsgCount);
                        if (sendMsgCount == destinationMsgCount)
                        {
                            break;
                        }
                    }
                    //Thread.Sleep(100);
                }
                msgEvent.Set();
            });
            msgEvent.WaitOne();
            Thread.Sleep(2000);
            Assert.IsTrue(sendMsgCount == destinationMsgCount);
            Assert.IsTrue(sendMsgCount == msgRecvCount);
            foreach (TcpTransportChannel clientChannel in clients)
            {
                clientChannel.Disconnect();
            }
            Thread.Sleep(2000);
        }
Esempio n. 3
0
        public void SendMessageTest()
        {
            ITransportChannel       connectedChannel = null;
            AutoResetEvent          resetEvent       = new AutoResetEvent(false);
            AutoResetEvent          msgEvent         = new AutoResetEvent(false);
            AutoResetEvent          channelEvent     = new AutoResetEvent(false);
            TcpHostTransportChannel hostChannel      = new TcpHostTransportChannel(20005);

            Assert.IsTrue(hostChannel.Regist());
            hostChannel.ChannelCreated += delegate(object sender, LightSingleArgEventArgs <ITransportChannel> args)
            {
                connectedChannel = args.Target;
                channelEvent.Set();
            };
            TcpTransportChannel transportChannel = new TcpTransportChannel("127.0.0.1", 20005);

            Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Unknown);
            transportChannel.Connect();
            if (!channelEvent.WaitOne(2000))
            {
                throw new System.Exception("#Cannot passed channel be connect test.");
            }
            Assert.IsNotNull(connectedChannel);
            Assert.IsTrue(connectedChannel.IsConnected);
            Assert.IsTrue(transportChannel.IsConnected);
            Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Opened);
            MetadataContainer msg = null;
            IMessageTransportChannel <MetadataContainer> msgChannel = new MessageTransportChannel <MetadataContainer>((IRawTransportChannel)connectedChannel, new MetadataProtocolStack());

            msgChannel.ReceivedMessage += delegate(object sender, LightSingleArgEventArgs <List <MetadataContainer> > args)
            {
                msg = args.Target[0];
                msgEvent.Set();
            };
            MetadataContainer container = new MetadataContainer();

            container.SetAttribute(0x00, new StringValueStored("Test-Name"));
            container.SetAttribute(0x01, new StringValueStored("Test-Value"));
            byte[] data = MetadataObjectEngine.ToBytes(container);
            Assert.IsNotNull(data);
            int sendCount = transportChannel.Send(data);

            Assert.IsTrue(sendCount == data.Length || sendCount == 1);
            if (!msgEvent.WaitOne(2000))
            {
                throw new System.Exception("#Cannot passed message channel receive test.");
            }
            Assert.IsNotNull(msg);
            Console.WriteLine(msg);

            msgChannel.Disconnected += delegate { resetEvent.Set(); };
            transportChannel.Disconnect();
            if (!resetEvent.WaitOne(10000))
            {
                throw new System.Exception("#Cannot passed transport channel disconnect test.");
            }
            Assert.IsTrue(transportChannel.CommunicationState == CommunicationStates.Closed);
            Thread.Sleep(2000);
            Assert.IsTrue(!connectedChannel.IsConnected);
            Assert.IsTrue(hostChannel.UnRegist());
        }
Esempio n. 4
0
 /// <summary>
 ///     初始化普通类型第三方数据转元数据的处理器
 /// </summary>
 /// <exception cref="ArgumentNullException">参数不能为空</exception>
 private static void InitializeValueProcessorDictionary()
 {
     ValueActions.Add((byte)PropertyTypes.Boolean,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <bool>() == DefaultValue.Boolean)
         {
             return;
         }
         proxy.WriteBoolean(valueStored.GetValue <bool>());
     });
     ValueActions.Add((byte)PropertyTypes.Byte,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <byte>() == DefaultValue.Byte)
         {
             return;
         }
         proxy.WriteByte(valueStored.GetValue <byte>());
     });
     ValueActions.Add((byte)PropertyTypes.SByte,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <sbyte>() == DefaultValue.SByte)
         {
             return;
         }
         proxy.WriteSByte(valueStored.GetValue <sbyte>());
     });
     ValueActions.Add((byte)PropertyTypes.Char,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <char>() == DefaultValue.Char)
         {
             return;
         }
         proxy.WriteChar(valueStored.GetValue <char>());
     });
     ValueActions.Add((byte)PropertyTypes.Int16,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <short>() == DefaultValue.Int16)
         {
             return;
         }
         proxy.WriteInt16(valueStored.GetValue <short>());
     });
     ValueActions.Add((byte)PropertyTypes.UInt16,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <ushort>() == DefaultValue.UInt16)
         {
             return;
         }
         proxy.WriteUInt16(valueStored.GetValue <ushort>());
     });
     ValueActions.Add((byte)PropertyTypes.Int32,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <int>() == DefaultValue.Int32)
         {
             return;
         }
         proxy.WriteInt32(valueStored.GetValue <int>());
     });
     ValueActions.Add((byte)PropertyTypes.UInt32,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <uint>() == DefaultValue.UInt32)
         {
             return;
         }
         proxy.WriteUInt32(valueStored.GetValue <uint>());
     });
     ValueActions.Add((byte)PropertyTypes.Int64,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <long>() == DefaultValue.Int64)
         {
             return;
         }
         proxy.WriteInt64(valueStored.GetValue <long>());
     });
     ValueActions.Add((byte)PropertyTypes.UInt64,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <ulong>() == DefaultValue.UInt64)
         {
             return;
         }
         proxy.WriteUInt64(valueStored.GetValue <ulong>());
     });
     ValueActions.Add((byte)PropertyTypes.Float,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <float>() == DefaultValue.Float)
         {
             return;
         }
         proxy.WriteFloat(valueStored.GetValue <float>());
     });
     ValueActions.Add((byte)PropertyTypes.Double,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <double>() == DefaultValue.Double)
         {
             return;
         }
         proxy.WriteDouble(valueStored.GetValue <double>());
     });
     ValueActions.Add((byte)PropertyTypes.Decimal,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <decimal>() == DefaultValue.Decimal)
         {
             return;
         }
         proxy.WriteDecimal(valueStored.GetValue <decimal>());
     });
     ValueActions.Add((byte)PropertyTypes.String, (proxy, valueStored) => proxy.WriteString(valueStored.GetValue <string>()));
     ValueActions.Add((byte)PropertyTypes.DateTime,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <DateTime>() == DefaultValue.DateTime)
         {
             return;
         }
         proxy.WriteDateTime(valueStored.GetValue <DateTime>());
     });
     ValueActions.Add((byte)PropertyTypes.Guid,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <Guid>() == DefaultValue.Guid)
         {
             return;
         }
         proxy.WriteGuid(valueStored.GetValue <Guid>());
     });
     ValueActions.Add((byte)PropertyTypes.IPEndPoint, (proxy, valueStored) => proxy.WriteIPEndPoint(valueStored.GetValue <IPEndPoint>()));
     ValueActions.Add((byte)PropertyTypes.IntPtr,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <IntPtr>() == DefaultValue.IntPtr)
         {
             return;
         }
         proxy.WriteIntPtr(valueStored.GetValue <IntPtr>());
     });
     ValueActions.Add((byte)PropertyTypes.ResourceBlock, (proxy, valueStored) => MetadataObjectEngine.ToBytes(valueStored.GetValue <ResourceBlock>(), proxy));
     ValueActions.Add((byte)PropertyTypes.TimeSpan,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (valueStored.GetValue <TimeSpan>() == DefaultValue.TimeSpan)
         {
             return;
         }
         proxy.WriteTimeSpan(valueStored.GetValue <TimeSpan>());
     });
     ValueActions.Add((byte)PropertyTypes.BitFlag, (proxy, valueStored) => proxy.WriteBitFlag(valueStored.GetValue <BitFlag>()));
     ValueActions.Add((byte)PropertyTypes.Blob,
                      delegate(IMemorySegmentProxy proxy, BaseValueStored valueStored)
     {
         if (proxy == null)
         {
             throw new ArgumentNullException("proxy");
         }
         if (valueStored == null)
         {
             throw new ArgumentNullException("valueStored");
         }
         byte[] data = valueStored.GetValue <Blob>().Compress();
         proxy.WriteMemory(data, 0U, (uint)data.Length);
     });
     ValueActions.Add((byte)PropertyTypes.Null, delegate { });
 }
Esempio n. 5
0
 /// <summary>
 ///   内部指定类型序列化方法
 /// </summary>
 /// <param name="proxy">内存代理器</param>
 public override void ToBytes(IMemorySegmentProxy proxy)
 {
     MetadataObjectEngine.ToBytes(_value, proxy);
 }
 /// <summary>
 /// 将一个消息转换为2进制形式
 /// </summary>
 /// <param name="message">需要转换的消息</param>
 /// <returns>
 /// 返回转换后的2进制
 /// </returns>
 public override byte[] ConvertToBytes(object message)
 {
     return(MetadataObjectEngine.ToBytes((MetadataContainer)message));
 }