Esempio n. 1
0
        static void Espresso_Listener(ZContext context)
        {
            // The listener receives all messages flowing through the proxy, on its
            // pipe. In CZMQ, the pipe is a pair of ZMQ_PAIR sockets that connect
            // attached child threads. In other languages your mileage may vary:

            using (var listener = new ZSocket(context, ZSocketType.PAIR))
            {
                listener.Connect("inproc://listener");

                ZError error;
                ZFrame frame;
                while (true)
                {
                    if (null != (frame = listener.ReceiveFrame(out error)))
                    {
                        using (frame)
                        {
                            byte first = frame.ReadAsByte();

                            var rest = new byte[9];
                            frame.Read(rest, 0, rest.Length);

                            Console.WriteLine("{0} {1}", (char)first, rest.ToHexString());

                            if (first == 0x01)
                            {
                                // Subscribe
                            }
                            else if (first == 0x00)
                            {
                                // Unsubscribe
                                context.Shutdown();
                            }
                        }
                    }
                    else
                    {
                        if (error == ZError.ETERM)
                            return;	// Interrupted
                        throw new ZException(error);
                    }
                }
            }
        }
Esempio n. 2
0
		public static void Interrupt(string[] args)
		{
			//
			// Interrupt
			//
			// Author: metadings
			//

			if (args == null || args.Length == 0)
			{
				args = new string[] { "World" };
			}

			string name = args[0];

			var error = default(ZError);

			using (var context = new ZContext())
			using (var responder = new ZSocket(context, ZSocketType.REP))
			{
				Console.CancelKeyPress += (s, ea) => 
				{ 
					ea.Cancel = true;
					context.Shutdown(); 
				};

				responder.Bind("tcp://*:5555");

				ZFrame request;

				while (true)
				{
					if (Console.KeyAvailable)
					{
						ConsoleKeyInfo info = Console.ReadKey(true);
						/* if (info.Modifiers == ConsoleModifiers.Control && info.Key == ConsoleKey.C)
						{
							context.Shutdown();
						} /**/
						if (info.Key == ConsoleKey.Escape)
						{
							context.Shutdown();
						}
					}

					if (null == (request = responder.ReceiveFrame(ZSocketFlags.DontWait, out error)))
					{
						if (error == ZError.EAGAIN)
						{
							Thread.Sleep(1);
							continue;
						}
						if (error == ZError.ETERM)
							break;	// Interrupted
						throw new ZException(error);
					}

					using (request)
					{
						Console.Write("Received: {0}!", request.ReadString());

						Thread.Sleep(512);	// See also the much slower reaction

						Console.WriteLine(" Sending {0}... ", name);

						if (!responder.Send(new ZFrame(name), out error))
						{
							if (error == ZError.ETERM)
								break;	// Interrupted
							throw new ZException(error);
						}
					}
				}

				if (error == ZError.ETERM)
				{
					Console.WriteLine("Terminated! You have pressed CTRL+C or ESC.");
					return;
				}
				throw new ZException(error);
			}
		}
Esempio n. 3
0
		public static void FLServer3(string[] args)
		{
			//
			// Freelance server - Model 3
			// Uses an ROUTER/ROUTER socket but just one thread
			//
			// Author: metadings
			//

			// Prepare server socket with predictable identity
			string bind_endpoint = "tcp://*:5555";
			string connect_endpoint = "tcp://127.0.0.1:5555";

			using (var context = new ZContext())
			using (var server = new ZSocket(context, ZSocketType.ROUTER))
			{
				Console.CancelKeyPress += (s, ea) =>
				{
					ea.Cancel = true;
					context.Shutdown();
				};

				server.IdentityString = connect_endpoint;
				server.Bind(bind_endpoint);
				Console.WriteLine("I: service is ready as {0}", bind_endpoint);

				ZError error;
				ZMessage request;
				while (true)
				{
					if (null == (request = server.ReceiveMessage(out error)))
					{
						if (error == ZError.ETERM)
							break;	// Interrupted
						throw new ZException(error);
					}
					using (var response = new ZMessage())
					{
						ZFrame identity;

						using (request)
						{
							if (Verbose) Console_WriteZMessage("Receiving", request);

							// Frame 0: identity of client
							// Frame 1: PING, or client control frame
							// Frame 2: request body

							identity = request.Pop();

							ZFrame control = request.Pop();
							string controlMessage = control.ReadString();

							if (controlMessage == "PING")
							{
								control.Dispose();
								response.Add(new ZFrame("PONG"));
							}
							else
							{
								response.Add(control);
								response.Add(new ZFrame("OK"));
							}
						}

						response.Prepend(identity);

						if (Verbose) Console_WriteZMessage("Sending  ", response);
						if (!server.Send(response, out error))
						{
							if (error == ZError.ETERM)
								break;	// Interrupted
							throw new ZException(error);
						}
					}
				}
				if (error == ZError.ETERM)
				{
					Console.WriteLine("W: interrupted");
				}
			}
		}