void Respond() { AsyncIO.ForceDotNet.Force(); var socket = new ResponseSocket("tcp://*:5558"); try{ while (responderIsStarted) { string inMsg; if (!socket.TryReceiveFrameString(out inMsg)) { continue; } Debug.Log("Received: " + inMsg); if (inMsg == "on") { lightIsOn = true; socket.SendFrame("Light is on"); } else if (inMsg == "off") { lightIsOn = false; socket.SendFrame("Light is off"); } } }finally{ if (socket != null) { socket.Close(); ((IDisposable)socket).Dispose(); NetMQConfig.Cleanup(true); } } }
private void DestroyResponseSocket() { if (responseSocket != null) { responseSocket.Close(); } }
public void StopServer() { if (!ServerRunning) { return; } poller?.Stop(); poller?.Dispose(); lock (responseSocketLock) { if (responseSocket != null) { try { responseSocket.Disconnect(responseSocketConnectionString); } finally { responseSocket.Close(); responseSocket = null; } } } }
void TTimeOut(Object obj) { ConsoleEx.DebugLog("HeartBeat Server Sock is timeout.", ConsoleEx.RED); if (TimeOut != null) { TimeOut.Change(Timeout.Infinite, Timeout.Infinite); } if (OnDisconnected != null) { OnDisconnected(); } if (poller != null) { if (poller.IsStarted) { poller.CancelAndJoin(); } poller.Dispose(); } if (respSocket != null) { respSocket.Unbind(BindingAddress); respSocket.Close(); } ConsoleEx.DebugLog("HeartBeat Server socket is Connecting...", ConsoleEx.RED); var context = Core.ZeroMQ; respSocket = context.CreateResponseSocket(); respSocket.Options.SendHighWatermark = EngCfg.HighWatermark; respSocket.Options.ReceiveHighWatermark = EngCfg.HighWatermark; //生成通讯地址和协议方式 BindingAddress = BindAddr(typeof(HeartBeatServer)); respSocket.Bind(BindingAddress); respSocket.ReceiveReady += Server_ReceiveReady; poller.AddSocket(respSocket); ThreadPool.QueueUserWorkItem((ob) => { poller.PollTillCancelled(); }); }
public void Stop() { alive = false; thread.Interrupt(); socket?.Close(); Join(); thread = null; }
public void Stop() { _running = false; if (_activeEndPoint != null) { _activeResponder.Unbind(_activeEndPoint); _activeResponder.Close(); } }
protected override Task DestroyInternal() { _cancel.Cancel(); _poller.Stop(); _heartbeatSocket.Close(); _heartbeatSocket.Dispose(); _stateRequestSocket.Close(); _stateRequestSocket.Dispose(); return(Task.CompletedTask); }
protected override void Run() { ForceDotNet.Force(); // this line is needed to prevent unity freeze after one use, not sure why yet using (server = new ResponseSocket()) // check if its response or request ? { server.Bind("tcp://*:1234"); //SEND using (client = new RequestSocket()) { client.Connect("tcp://localhost:2222"); // RECIEVE // for (int i = 0; i < 2 && Running; i++) while (Running) { // TO BE ABLE TO SEND USING SERVER SOCKET: string DummyServerReceive = server.ReceiveFrameString(); //Debug.Log("MY DUMMY SERVER REC: " + DummyServerReceive); //SENDING TO SERVER : //SEND A VARIABLE BOOLEAN HERE server.SendFrame(Mode + "," + Yindex + "," + Xindex + "," + Resign + "," + Pass + "," + HumanColor); //Pass = "******"; //Resign = "-1"; //Debug.Log("SERVER IS DONE "); // DUMMY SEND OF CLIENT TO RECEIVE client.SendFrame("HELLOOOOOOO"); while (Running) { message = client.ReceiveFrameString(); // this returns true if it's successful //Debug.Log("MESSAGE IS :" + this.message); break; } } client.Disconnect("tcp://localhost:2222"); client.Close(); client.Dispose(); } server.Disconnect("tcp://*:1234"); server.Close(); server.Dispose(); } NetMQConfig.Cleanup(); }
// OnDisable() is called when the behaviour becomes disabled. void OnDisable() { try{ // Stop receiving thread serverThread.Abort(); // Close the server socket responseSocket.Close(); NetMQConfig.Cleanup(false); }catch (Exception exp) { if (exp.Source != null) { Console.WriteLine("Exeption source: {0}", exp.Source); } } }
private void ResponseEntry() { using (ResponseSocket serverSocket = new ResponseSocket()) { string endpoint = "tcp://*:" + ModResponse.port; try { serverSocket.Bind(endpoint); } catch (Exception err) { Logging.logger.Error(ModName + " bind response socket failed " + endpoint + " " + err.Message); throw (err); } ResponseRM.ressock = serverSocket; string received = string.Empty; bool result; string resdata = string.Empty; ResponseRM.Working = true; while (ResponseRM.Running) { //string received = serverSocket.ReceiveFrameString(); received = string.Empty; result = serverSocket.TryReceiveFrameString(out received); if (result == true) { try { resdata = this.ResponseEntry(received); serverSocket.SendFrame(resdata); } catch (Exception err) { Logging.logger.Error(err.Message); resdata = Entry4ExceptionMsg(err.Message); serverSocket.SendFrame(resdata); throw (err); } } DelayTime(); } ResponseRM.Working = false; serverSocket.Close(); } }
public void Kill() { _poller.Stop(); _heartbeat.Close(); _heartbeat.Dispose(); _frontend.Close(); _frontend.Dispose(); _backend.Close(); _backend.Dispose(); _workerQueue.Dispose(); _workQueue.Dispose(); }
public ServiceStarter() : base() { var free_sock = new ResponseSocket(); free_sock_numb = free_sock.BindRandomPort("tcp://*"); Console.WriteLine("free socket is {0}", free_sock_numb); free_sock.Close(); service_addres = String.Format("tcp://*****:*****@" C:\programming\TopLevelProgram\cnn_service\start.py --port {0}", free_sock_numb); this.StartInfo.WorkingDirectory = @"C:\programming\TopLevelProgram\cnn_service\"; this.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; this.StartInfo.UseShellExecute = false; this.StartInfo.RedirectStandardOutput = true; this.StartInfo.RedirectStandardError = true; string cnn_outputPath = @"./log/output_cnn.txt"; using (StreamWriter sw = new StreamWriter(cnn_outputPath, false, System.Text.Encoding.Default)) { sw.WriteLine(DateTime.Now); } this.OutputDataReceived += new DataReceivedEventHandler((s, e) => { if (!String.IsNullOrEmpty(e.Data)) { using (StreamWriter sw = new StreamWriter(cnn_outputPath, true, System.Text.Encoding.Default)) { sw.WriteLine(e.Data); } } }); string cnn_errorPath = @"./log/error_cnn.txt"; using (StreamWriter sw = new StreamWriter(cnn_errorPath, false, System.Text.Encoding.Default)) { sw.WriteLine(DateTime.Now); } this.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { if (!String.IsNullOrEmpty(e.Data)) { using (StreamWriter sw = new StreamWriter(cnn_errorPath, true, System.Text.Encoding.Default)) { sw.WriteLine(e.Data); } } }); }
public override Task Stop() { _cancel.Cancel(); _poller.Stop(); _heartbeat.Close(); _heartbeat.Dispose(); _frontend.Close(); _frontend.Dispose(); _backend.Close(); _backend.Dispose(); return(Task.CompletedTask); }
public void Stop() { _cancel.Cancel(); _poller.Stop(); _heartbeat.Close(); _heartbeat.Dispose(); _publishStateUpdate.Close(); _publishStateUpdate.Dispose(); _subscribeToUpdates.Close(); _subscribeToUpdates.Dispose(); _stateRequest.Close(); _stateRequest.Dispose(); }
public void Quit() { if (poller != null) { bool started = poller.IsStarted; if (started) { poller.CancelAndJoin(); } poller.Dispose(); } if (respSocket != null) { respSocket.Unbind(BindingAddress); respSocket.Close(); respSocket = null; } }
// Client thread which does not block Update() void NetMQServer() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string msg; var timeout = new System.TimeSpan(0, 0, 1); //1sec Log("Connect."); var socket = new ResponseSocket("@tcp://localhost:5557"); while (true) { lock (thisLock_) { if (stop_thread_) { break; } } // Log("Get request."); try { if (socket.TryReceiveFrameString(timeout, out msg)) { Log("recived: " + msg); socket.SendFrame("world"); } else { Log("Timed out, sleep"); Thread.Sleep(1000); } } catch (System.Exception ex) { Log(ex.Message); throw ex; } } socket.Close(); Log("ContextTerminate."); NetMQConfig.ContextTerminate(); }
static void Main(string[] args) { var proxy1 = new Proxy(new RouterSocket("@tcp://*:5001"), new DealerSocket("@tcp://*:5002")); Task.Factory.StartNew(proxy1.Start); NetMQPoller poller1 = new NetMQPoller(); for (byte i = 0; i < 2; i++) { Task.Factory.StartNew((o) => { var response = new ResponseSocket(">tcp://127.0.0.1:5002"); response.Options.Identity = new byte[] { (byte)o }; try { while (true) { var msg = response.ReceiveFrameString(); if (response.Options.Identity[0] == 0) { Thread.Sleep(5000); throw new Exception(); } response.SendFrame($"{response.Options.Identity[0]}-{msg}"); } } catch { response.Close(); Console.WriteLine($"close {i}"); } }, i); } poller1.RunAsync(); Console.WriteLine("Press any key to stop!"); Console.Read(); }
private void dataServer() { AsyncIO.ForceDotNet.Force(); using (var dataSender = new ResponseSocket()) { dataSender.Bind("tcp://*:5565"); Debug.Log("Enter while.. "); while (isRunning) { string message = dataSender.ReceiveFrameString(); print("Got request message: " + message); // re-run scene iteration if true if (doGatherOnRequest) { gatherSceneData(); } switch (message) { case "header": print("Send Header.. "); dataSender.SendFrame(headerByteData); print(string.Format(".. Nodes ({0} bytes) sent ", headerByteData.Length)); break; case "nodes": print("Send Nodes.. "); dataSender.SendFrame(nodesByteData); print(string.Format(".. Nodes ({0} bytes) sent ", nodesByteData.Length)); break; case "objects": print("Send Objects.. "); dataSender.SendFrame(objectsByteData); print(string.Format(".. Objects ({0} bytes) sent ", objectsByteData.Length)); break; case "textures": print("Send Textures.. "); dataSender.SendFrame(texturesByteData); print(string.Format(".. Textures ({0} bytes) sent ", texturesByteData.Length)); break; #if TRUNK case "materials": print("Send Materials.. "); dataSender.SendFrame(materialsByteData); print(string.Format(".. Materials ({0} bytes) sent ", materialsByteData.Length)); break; #endif default: break; } } dataSender.Unbind("tcp://127.0.0.1:5565"); dataSender.Close(); dataSender.Dispose(); } //NetMQConfig.Cleanup(); }
public void Kill() { _heartbeat.Close(); _heartbeat.Dispose(); }
private void dataServer() { AsyncIO.ForceDotNet.Force(); using (var dataSender = new ResponseSocket()) { dataSender.Bind("tcp://" + VPETSettings.Instance.serverIP + ":5565"); Debug.Log("Enter while.. "); while (isRunning) { string message = ""; dataSender.TryReceiveFrameString(out message); //print ("Got request message: " + message); // re-run scene iteration if true if (doGatherOnRequest) { gatherSceneData(); } switch (message) { case "header": print("Send Header.. "); dataSender.SendFrame(headerByteData); print(string.Format(".. Nodes ({0} bytes) sent ", headerByteData.Length)); break; case "nodes": print("Send Nodes.. "); dataSender.SendFrame(nodesByteData); print(string.Format(".. Nodes ({0} bytes) sent ", nodesByteData.Length)); break; case "objects": print("Send Objects.. "); dataSender.SendFrame(objectsByteData); print(string.Format(".. Objects ({0} bytes) sent ", objectsByteData.Length)); break; case "characters": print("Send Characters.. "); dataSender.SendFrame(charactersByteData); print(string.Format(".. Characters ({0} bytes) sent ", charactersByteData.Length)); break; case "textures": print("Send Textures.. "); dataSender.SendFrame(texturesByteData); print(string.Format(".. Textures ({0} bytes) sent ", texturesByteData.Length)); break; #if TRUNK case "materials": print("Send Materials.. "); dataSender.SendFrame(materialsByteData); print(string.Format(".. Materials ({0} bytes) sent ", materialsByteData.Length)); break; #endif default: break; } } // TODO: check first if closed try { dataSender.Disconnect("tcp://" + VPETSettings.Instance.serverIP + ":5565"); dataSender.Dispose(); dataSender.Close(); } finally { NetMQConfig.Cleanup(false); } } }
public void Close() { socket.Close(); }