Ejemplo n.º 1
0
        public void Init()
        {
            bool working = true;

            server.Bind("tcp://localhost:12553");

            while (working)
            {
                requestMsg.InitEmpty();
                server.Receive(ref requestMsg);
                Message message = Message.Deserialize(requestMsg.Data);
                if (message.MessageType.HasFlag(MessageType.MT_MEMBER_FLAG))
                {
                    MessageType mt = message.MessageType & MessageType.MT_MEMBER_FLAG_MASK;
                    if (dealer.ContainsKey(mt))
                    {
                        dealer[mt](message);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            const string SERVER_ENDPOINT = "tcp://127.0.0.1:5555";

            using (var context = NetMQContext.Create())
            {
                var randomizer = new Random();

                using (ResponseSocket server = context.CreateResponseSocket())
                {
                    Console.WriteLine("S: Binding address {0}", SERVER_ENDPOINT);
                    server.Bind(SERVER_ENDPOINT);

                    var cycles = 0;

                    while (true)
                    {
                        byte[] request = server.Receive();
                        cycles++;

                        if (cycles > 3 && randomizer.Next(0, 10) == 0)
                        {
                            Console.WriteLine("S: Simulating a crash");
                            Thread.Sleep(5000);
                        }
                        else if (cycles < 3 && randomizer.Next(0, 10) == 0)
                        {
                            Console.WriteLine("S: Simulating CPU overload");
                            Thread.Sleep(1000);
                        }

                        Console.WriteLine("S: Normal request ({0})", Encoding.Unicode.GetString(request));
                        //Thread.Sleep(1000);
                        server.Send(request);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("usage: local_lat <bind-to> <message-size> <roundtrip-count>");
                return(1);
            }

            string bindTo         = args[0];
            int    messageSize    = int.Parse(args[1]);
            int    roundtripCount = int.Parse(args[2]);

            using (var rep = new ResponseSocket())
            {
                rep.Bind(bindTo);

                var msg = new Msg();
                msg.InitEmpty();

                for (int i = 0; i != roundtripCount; i++)
                {
                    rep.Receive(ref msg);
                    if (msg.Size != messageSize)
                    {
                        Console.WriteLine("message of incorrect size received. Received: " + msg.Size + " Expected: " + messageSize);
                        return(-1);
                    }

                    rep.Send(ref msg, more: false);
                }

                msg.Close();
            }

            return(0);
        }
Ejemplo n.º 4
0
        private void BackgroundTask(object state)
        {
            var            cancellationToken = (CancellationToken)state;
            ResponseSocket socket            = _ctx.CreateResponseSocket();

            try
            {
                switch (_cxMode)
                {
                case ConnectionMode.Connect:
                    socket.Connect(_address);
                    break;

                case ConnectionMode.Bind:
                    socket.Bind(_address);
                    break;
                }

                byte[] receiveBuffer;
                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        receiveBuffer = socket.Receive();
                        if (receiveBuffer == null)
                        {
                            continue; // NetMQ > 3.3.0.11
                        }
                    }
                    catch (AgainException)
                    {
                        continue; // NetMQ = 3.3.0.11
                    }

                    #region Always send a reply...

                    try
                    {
                        Thread.Sleep(500); // simulates processing...
                        socket.Send(string.Format("Reply ({0})", Encoding.UTF8.GetString(receiveBuffer)));
                    }
                    catch (TerminatingException)
                    {
                        try
                        {
                            socket.Send("Exit...");
                        }
                        catch
                        {
                        }
                        throw;
                    }

                    #endregion
                }
            }
            catch (TerminatingException)
            {
            }
            finally
            {
                try
                {
                    socket.Dispose();
                }
                catch (NetMQException)
                {
                }
            }
        }