static void Main(string[] args)
        {
            IMessageProcessor processor = MessageFactory.GetInstance();

            processor.Init(10000, 5000);
            processor.Register(new MessageContext()
            {
                Key = "IES", Address = new string[] { "10.130.36.225:9015" }, QueueSize = 16
            });
            processor.AddMessage("IES", new IESMessage()
            {
                ID = "IES001", DeviceID = "Device001"
            });
            Thread.Sleep(5000);
            processor.Register(new MessageContext()
            {
                Key = "LIVE", Address = new string[] { "10.130.36.225:9017" }, QueueSize = 16
            });
            processor.AddMessage("LIVE", new LIVEMessage()
            {
                MsgBody = "LIVE001&Device002"
            });
            Thread.Sleep(5000);
            processor.AddMessage("IES", new IESMessage()
            {
                ID = "IES002", DeviceID = "Device001"
            });
            processor.AddMessage("LIVE", new LIVEMessage()
            {
                MsgBody = "LIVE002&Device002"
            });
            //Thread t = new Thread(new ParameterizedThreadStart(SetStatus1));
            //t.Start(processor);
            //Thread.Sleep(5000);
            //Console.WriteLine(DateTime.Now + " Main SetTargetStatus");
            //processor.SetTargetStatus("key1", true);
            //Console.WriteLine(DateTime.Now + " AddMessage");
            //processor.AddMessage("key2", new MyMessage());
            //processor.AddMessage("key1", new MyMessage());
            //Thread.Sleep(5000);
            //processor.SetTargetAddress("key2", new string[] { "10.130.36.100:1111" });
            //processor.SetTargetStatus("key2", true);
            //Thread.Sleep(5000);
            //Thread t2 = new Thread(new ParameterizedThreadStart(SetStatus2));
            //t2.Start(processor);
            //Thread.Sleep(5000);
            //Console.WriteLine(DateTime.Now + " Main SetTargetStatus");
            //processor.SetTargetStatus("key2", true);
            //processor.AddMessage("key2", new MyMessage());
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            IMessageProcessor processor = MessageFactory.GetInstance();

            processor.Init(10000, 3000);
            processor.Register(new MessageContext()
            {
                Key = "IES", Address = new string[] { "10.130.36.225:8013" }, QueueSize = 16
            });
            processor.AddMessage("IES", new MyMessage());
            processor.AddMessage("IES", new MyMessage());
            processor.AddMessage("IES", new MyMessage());
            Thread.Sleep(3005);
            processor.SetUdpPort(11111);
            processor.AddMessage("IES", new MyMessage());
            processor.AddMessage("IES", new MyMessage());
            processor.AddMessage("IES", new MyMessage());
            Thread.Sleep(5000);
            processor.AddMessage("IES", new MyMessage());
            processor.AddMessage("IES", new MyMessage());
            processor.AddMessage("IES", new MyMessage());

            ////processor.Register(new MessageContext() { Key = "key1", Address = new string[] { "10.130.36.225:11000" }, QueueSize = 16 });
            ////processor.AddMessage("key1", new MyMessage());
            //processor.Register(new MessageContext() { Key = "key1", Address = new string[] { "10.130.36.225:10000", "10.130.36.224:10000" }, QueueSize = 16 });
            //processor.AddMessage("key1", new MyMessage());
            //Thread.Sleep(5000);
            //processor.Register(new MessageContext() { Key = "key2", Address = new string[] { "10.130.36.111:10000", "10.130.36.222:10000" }, QueueSize = 16 });
            //processor.AddMessage("key2", new MyMessage());
            //Thread.Sleep(5000);
            //Thread t = new Thread(new ParameterizedThreadStart(SetStatus1));
            //t.Start(processor);
            //Thread.Sleep(5000);
            //Console.WriteLine(DateTime.Now + " Main SetTargetStatus");
            //processor.SetTargetStatus("key1", true);
            //Console.WriteLine(DateTime.Now + " AddMessage");
            //processor.AddMessage("key2", new MyMessage());
            //processor.AddMessage("key1", new MyMessage());
            //Thread.Sleep(5000);
            //processor.SetTargetAddress("key2", new string[] { "10.130.36.100:1111" });
            //processor.SetTargetStatus("key2", true);
            //Thread.Sleep(5000);
            //Thread t2 = new Thread(new ParameterizedThreadStart(SetStatus2));
            //t2.Start(processor);
            //Thread.Sleep(5000);
            //Console.WriteLine(DateTime.Now + " Main SetTargetStatus");
            //processor.SetTargetStatus("key2", true);
            //processor.AddMessage("key2", new MyMessage());
            Console.ReadKey();
        }
예제 #3
0
        public void Start <T>() where T : IMessageProcessor, new()
        {
            if (ServerIP == null)
            {
                throw new ArgumentNullException("ServerIP");
            }

            if (ServerPort <= 0 || ServerPort >= 65536)
            {
                throw new ArgumentOutOfRangeException("ServerPort");
            }

            if (_serverState != 0)
            {
                throw new RpcException("RpcServer Start failed,state error");
            }

            var originState = Interlocked.CompareExchange(ref _serverState, 1, 0);

            if (originState != 0)
            {
                throw new RpcException("RpcServer Start failed,state error");
            }

            try
            {
                _messageProcessor = new T();
                _messageProcessor.Init();

                var inOptionValues = NetworkSettings.GetServerKeepAliveInfo();
                var backlog        = NetworkSettings.ServerListenBacklog;

                _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _server.ExclusiveAddressUse = true;
                _server.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
                _server.LingerState = new System.Net.Sockets.LingerOption(true, 0);

                _server.Bind(new IPEndPoint(ServerIP, ServerPort));
                _server.Listen(backlog);
            }
            catch
            {
                _serverState = -1;
                throw;
            }

            var originState2 = Interlocked.CompareExchange(ref _serverState, 2, 1);

            if (originState2 != 1)
            {
                return;
            }

            try
            {
                _server.BeginAccept(AcceptSocketCallback, _server);
            }
            catch
            {
                Close();
                return;
            }
        }