public static void client_comm() //For the host, communicates TO the client { if (_clientCommInstantiated) { return; } _clientCommInstantiated = true; const string repAddress = "tcp://*:5560"; var rep = new ResponseSocket(); rep.Bind(repAddress); while (true) { var recvStr = rep.ReceiveFrameString(); //Add/update data //Debug.Log(recvStr); var recvJson = JsonConvert.DeserializeObject <Dictionary <string, string> >(recvStr); if (int.Parse(recvJson["ID"]) == 0) //ID 0 means it's a client { var jsonOut = JsonConvert.SerializeObject(HostAndClientInput.goObjectPose, Formatting.Indented); var msg = new Msg(); msg.InitGC(Encoding.ASCII.GetBytes(jsonOut), Encoding.ASCII.GetBytes(jsonOut).Length); rep.Send(ref msg, false); } else if (HostAndClientInput.FalconIDs.Contains(int.Parse(recvJson["ID"]))) //IDs greater than 0 are Falcons { if (!HostAndClientInput.HipForces.ContainsKey(int.Parse(recvJson["ID"]))) { HostAndClientInput.HipForces.Add(int.Parse(recvJson["ID"]), recvJson); } else { HostAndClientInput.HipForces[int.Parse(recvJson["ID"])] = recvJson; } //Debug.Log(recvStr); //ack var msg = new Msg(); msg.InitGC(Encoding.ASCII.GetBytes(recvJson["ID"]), Encoding.ASCII.GetBytes(recvJson["ID"]).Length); rep.Send(ref msg, false); } if (HostAndClientInput.killThread) { break; //Because Unity3D editor doesn't kill this by itself. } } }
private void ProcessMemberInfo(Message message) { MemberInfoRequest request = SerializerUtil <MemberInfoRequest> .Deserialize(message.Content); //Find Member Membership membership = new Membership() { ID = new Guid(12, 34, 56, 78, 90, 13, 24, 25, 06, 77, 88), Name = "未来的风", NickName = "没有风", LastLoginIP = 0x7F_00_00_01, SuccessfullyLoginCount = 0, Department = new string[] { "1", "2", "3", "4" }, DepartmentInfo = "未见", DepartmentLevel = 4, PortraitID = 1, CustomPortrait = null }; Message Reply = MessageFactory.CreateMemberInfoRespMessage(message.To, message.From, membership); byte[] b = SerializerUtil <Message> .Serialize(Reply); responseMsg.InitPool(b.Length); responseMsg.Put(b, 0, b.Length); server.Send(ref responseMsg, false); }
private void OnSnapshotReady(object sender, NetMQSocketEventArgs e) { string command = snapshotSocket.ReceiveString(); // Currently we only have one type of events if (command == SnapshotProtocol.GetTradessCommand) { var tickers = tickerRepository.GetAllTickers(); // we will send all the tickers in one message foreach (var ticker in tickers) { snapshotSocket.SendMore(JsonConvert.SerializeObject(ticker)); } snapshotSocket.Send(SnapshotProtocol.EndOfTickers); } }
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); } } } }
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); }
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) { } } }