//static void SetSocketOpt(NetMQSocket socket) //{ // socket.Options.TcpKeepalive = true; // socket.Options.ReconnectIntervalMax = TimeSpan.FromMilliseconds(30000); // socket.Options.Linger = TimeSpan.FromMilliseconds(0); // //socket.KeepAlive = true; // //socket.ReconnectIntervalMax = TimeSpan.FromMilliseconds(30000); // //socket.Linger = TimeSpan.FromMilliseconds(0); //} static public Socket_NanoMsg Create(SocketType socketType) { NanomsgSocketBase socket; switch (socketType) { case SocketType.Pub: socket = new PublishSocket(); return(new Socket_NanoMsg(socket)); case SocketType.Pull: socket = new PullSocket(); return(new Socket_NanoMsg(socket)); case SocketType.Push: socket = new PushSocket(); return(new Socket_NanoMsg(socket)); case SocketType.Sub: socket = new SubscribeSocket(); return(new Socket_NanoMsg(socket)); default: throw new Exception("Non support SocketType."); } }
public static void Execute(string[] args) { string[] values = args; if (values.Length != 3) { throw new ArgumentException("Invalid number parameters."); } switch (values[1].Trim().ToLower()) { case "publisher": using (var s = new PublishSocket()) { s.Bind(values[2]); int i = 0; while (true) { byte[] b = Encoding.ASCII.GetBytes("Publish Counter is " + ++i); s.Send(b); Console.Write("."); Thread.Sleep(2000); } } break; case "subscriber": using (var s = new SubscribeSocket()) { //Needs to match the first portion of the message being received. s.Subscribe("Publish Counter"); s.Connect(values[2]); while (true) { byte[] b = s.Receive(); if (b != null) { Console.WriteLine("Received: " + Encoding.ASCII.GetString(b)); } else { Console.WriteLine("x"); } } } break; } }
/* * The World Server is simply a Publish server which loops over a queue of * events and a list of connections and sends each connection those events. * * Events from the world server will be sent in the form of: * "<id> <event> <arg0> [... [<argN>]] */ public void WorldThreadProc () { using (var sock = new PublishSocket()) { /* * World Server does nothing except output the received events from * the channel. The events are simply strings. Thus this means * there needs to be another thread crunching on the events and * connected ids. Thus, something else generates the events sent on * the world server. */ sock.Bind(url + ":8888"); while (running) { string e = eventChannel.Receive(); Console.WriteLine("Sending: `" + e + "`"); sock.Send(Encoding.UTF8.GetBytes(e)); } } }
public static void Execute() { Console.WriteLine("Executing pubsub test "); int receiveCount = 0; var clientThread = new Thread( () => { var subscriber = new SubscribeSocket(); subscriber.Connect(InprocAddress); subscriber.Subscribe("TestMessage"); byte[] streamOutput = new byte[BufferSize]; while (true) { var sw = Stopwatch.StartNew(); for (int i = 0; i < Iter; i++) { int read = 0; streamOutput = subscriber.Receive(); read = streamOutput.Length; //using (var stream = subscriber.ReceiveStream()) // while (stream.Length != stream.Position) // { // read += stream.Read(streamOutput, 0, streamOutput.Length); // var message = Encoding.ASCII.GetString(streamOutput, 0, read); // Trace.Assert(message.StartsWith("TestMessage")); // // break; // } ++receiveCount; } sw.Stop(); var secondsPerSend = sw.Elapsed.TotalSeconds / (double)Iter; Console.WriteLine("Time {0} us, {1} per second, {2} mb/s ", (int)(secondsPerSend * 1000d * 1000d), (int)(1d / secondsPerSend), (int)(DataSize * 2d / (1024d * 1024d * secondsPerSend))); } }); clientThread.Start(); { var publisher = new PublishSocket(); publisher.Bind(InprocAddress); Thread.Sleep(100); var sw = Stopwatch.StartNew(); int sendCount = 0; var text = "TestMessage" + new string('q', 10); var data = Encoding.ASCII.GetBytes(text); while (sw.Elapsed.TotalSeconds < 10) { publisher.Send(data); ++sendCount; } Thread.Sleep(100); clientThread.Abort(); Console.WriteLine("Send count {0} receive count {1}", sendCount, receiveCount); publisher.Dispose(); } }