void SocketThreadLoop() { while (true) { threadRunning = true; senderCancelled = false; AsyncIO.ForceDotNet.Force(); using (var sock = new PairSocket()) { sock.Connect("tcp://" + ip + ":" + port); while (!senderCancelled) { if (!framePending) { continue; } sock.SendFrame(msgBuffer); framesSent++; framePending = false; } sock.Close(); } NetMQConfig.Cleanup(); threadRunning = false; } }
public override bool HandleMessage(PairSocket me, PairSocket remote, Envelope envelop) { var msg = envelop.GetMessage(); if (me.IdentityConnectionSocketsMap.ContainsKey(remote)) { //Evita de várias conexões fake no servidor remote.Close(); } byte[] identifier; if (msg.Length >= 16) { identifier = msg.ReadNext(16); remote.ConnectionId = new Guid(identifier); } else { remote.ConnectionId = Guid.NewGuid(); identifier = remote.ConnectionId.ToByteArray(); } me.IdentitySocketsMap[identifier] = remote; SendResultCode(remote, identifier); return(true); }
private void ConnectionWork() { AsyncIO.ForceDotNet.Force(); using (var pairSocket = new PairSocket()) { pairSocket.Options.ReceiveHighWatermark = 1000; if (_createConnection) { pairSocket.Bind("tcp://*:12345"); } else { pairSocket.Connect("tcp://localhost:12345"); } //Do one more loop in-case we send out a closing msg and then cancel the connection bool flushedBuffer = true; while (!_connectionCancelled || !flushedBuffer) { string frameString; while (pairSocket.TryReceiveFrameString(out frameString)) { _incomingMessageQueue.Enqueue(frameString); } while (_outgoingMessageQueue.TryDequeue(out frameString)) { pairSocket.SendFrame(frameString); } flushedBuffer = _connectionCancelled; } pairSocket.Close(); } NetMQConfig.Cleanup(); }
private void ListenerWork() { while (true) { _threadRunning = true; _listenerCancelled = false; AsyncIO.ForceDotNet.Force(); using (var server = new PairSocket()) { server.Bind("tcp://*:" + port); while (!_listenerCancelled) { Connected = _contactWatch.ElapsedMilliseconds < ContactThreshold; byte[] frameBytes; if (!server.TryReceiveFrameBytes(out frameBytes)) { continue; } _contactWatch.Restart(); if (OnBytes != null && frameBytes != null) { OnBytes.Invoke(frameBytes); } } server.Close(); } NetMQConfig.Cleanup(); _threadRunning = false; } }
/// <summary> /// My basic protocol send first frame with a uint, this contains the command /// </summary> private void DoCommunication() { Console.WriteLine("> Iniciando comunicação básica"); Console.WriteLine("> Comandos (uint): "); Console.WriteLine("> 0: Escreve no console remoto"); Console.WriteLine("> 1: Envia um dado para ser salvos"); Console.WriteLine("> 2: Solicita todos os dados salvos"); Console.WriteLine("> 3: Envia um Echo"); Console.WriteLine("> "); var keepCommunication = true; while (keepCommunication) { Console.Write("> Operação (uint): "); var operation = Console.ReadLine(); uint opCode; if (uint.TryParse(operation, out opCode)) { Console.Write("> Mensagem (string): "); var txtMessage = Console.ReadLine(); var package = new Message(); package.Append(opCode); package.Append(txtMessage); var response = pairSocket.Request(package); if (response.Success) { Console.WriteLine(">>> Response:"); var serverOperationCode = response.ReadNext <uint>(); Console.WriteLine(""); Console.WriteLine("Operation from Remote: " + serverOperationCode); string frame = ""; while ((frame = response.ReadNext <string>()) != "") { Console.WriteLine(frame); } Console.WriteLine(""); } else { Console.WriteLine(">>> Request fail!"); } } else { pairSocket.Close(); Console.WriteLine("> Operação não é um valor válido, informe um uint válido"); } } }
void OnApplicationQuit()//cleanup { VisionClient.Close(); VisionClient.Dispose(); SimulationClient.Close(); SimulationClient.Dispose(); ScreenshotServer.Close(); ScreenshotServer.Dispose(); try { VisionPoller.StopAsync(); SimulationPoller.StopAsync(); ScreenshotPoller.StopAsync(); } catch (System.Exception e) { UnityEngine.Debug.Log("Tried to stopasync while the poller wasn't running! Oops."); } VisionPoller.Dispose(); SimulationPoller.Dispose(); ScreenshotPoller.Dispose(); }