void NetMQClient() { //thanks for Yuta Itoh sample code to connect via NetMQ with Pupil Service string IPHeader = ">tcp://" + ServerIP + ":"; var timeout = new System.TimeSpan(0, 0, 1); //1sec // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string subport = ""; print("Connect to the server: " + IPHeader + ServicePort + "."); Thread.Sleep(ServiceStartupDelay); _requestSocket = new RequestSocket(IPHeader + ServicePort); _requestSocket.SendFrame("SUB_PORT"); _isconnected = _requestSocket.TryReceiveFrameString(timeout, out subport); print(_isconnected + " isconnected"); _gazeFps.Reset(); _eyeFps [0].Reset(); _eyeFps [1].Reset(); if (_isconnected) { //_serviceStarted = true; StartProcess(); var subscriberSocket = new SubscriberSocket(IPHeader + subport); subscriberSocket.Subscribe("gaze"); //subscribe for gaze data subscriberSocket.Subscribe("notify."); //subscribe for all notifications _setStatus(EStatus.ProcessingGaze); var msg = new NetMQMessage(); while (_isDone == false) { _isconnected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg)); if (_isconnected) { try { string msgType = msg[0].ConvertToString(); UnityEngine.Debug.Log(msgType); if (msgType == "gaze") { var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; lock (_dataLock) { _pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString()); if (_pupilData.confidence > 0.5f) { //UnityEngine.Debug.Log(_pupilData.base_data[0].id); OnPacket(_pupilData); } } } else if (msgType == "notify.eye_process.started") { var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; var id = JsonUtility.FromJson <Pupil.EyeStatus>(mmap.ToString()); UnityEngine.Debug.Log(id.eye_id); } //Debug.Log(message); } catch { // Debug.Log("Failed to unpack."); } } else { print("Failed to receive a message."); Thread.Sleep(500); } } StopProcess(); subscriberSocket.Close(); } else { print("Failed to connect the server."); //If needed here could come a retry connection. } //Can only send request via IPC if the connection has been established, otherwise we are facing, errors and potential freezing. if (_serviceStarted && _isconnected) { StopService(); } //Kill process if (serviceProcess != null) { UnityEngine.Debug.Log("Killing Pupil service"); serviceProcess.Kill(); serviceProcess.Close(); } _requestSocket.Close(); // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 print("ContextTerminate."); NetMQConfig.ContextTerminate(); }
void NetMQClient() { //thanks for Yuta Itoh sample code to connect via NetMQ with Pupil Service string IPHeader = ">tcp://" + ServerIP + ":"; var timeout = new System.TimeSpan(0, 0, 1); //1sec // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string subport = ""; Debug.Log("Connect to the server: " + IPHeader + ServicePort + "."); _requestSocket = new RequestSocket(IPHeader + ServicePort); _requestSocket.SendFrame("SUB_PORT"); _isconnected = _requestSocket.TryReceiveFrameString(timeout, out subport); _lastT = DateTime.Now; if (_isconnected) { StartProcess(); var subscriberSocket = new SubscriberSocket(IPHeader + subport); subscriberSocket.Subscribe("gaze"); //subscribe for gaze data subscriberSocket.Subscribe("notify."); //subscribe for all notifications _setStatus(EStatus.ProcessingGaze); var msg = new NetMQMessage(); while (_isDone == false) { _isconnected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg)); if (_isconnected) { try { string msgType = msg[0].ConvertToString(); //Debug.Log(msgType); if (msgType == "gaze") { var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; lock (_dataLock) { _pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString()); if (_pupilData.confidence > 0.5f) { OnPacket(_pupilData); } } } //Debug.Log(message); } catch { // Debug.Log("Failed to unpack."); } } else { // Debug.Log("Failed to receive a message."); Thread.Sleep(500); } } StopProcess(); subscriberSocket.Close(); } else { Debug.Log("Failed to connect the server."); } _requestSocket.Close(); // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 Debug.Log("ContextTerminate."); NetMQConfig.ContextTerminate(); }
void NetMQClient() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); List <string> subports = new List <string>(); // subports for each client connection // loop through all clients and try to connect to them foreach (PupilConfiguration.PupilClient c in clients) { string subport = ""; string IPHeader = ">tcp://" + c.ip + ":"; bool frameReceived = false; Debug.LogFormat("Requesting socket for {0}:{1} ({2})", c.ip, c.port, c.name); RequestSocket requestSocket; try { // validate ip header if (!validateIPHeader(c.ip, c.port)) { Debug.LogWarningFormat("{0}:{1} is not a valid ip header for client {2}", c.ip, c.port, c.name); string failHeader = ""; subports.Add(failHeader); IPHeaders.Add(failHeader); c.is_connected = false; continue; } requestSocket = new RequestSocket(IPHeader + c.port); if (requestSocket != null) { requestSocket.SendFrame("SUB_PORT"); timeout = new System.TimeSpan(0, 0, 0, 100); frameReceived = requestSocket.TryReceiveFrameString(timeout, out subport); // request subport, will be saved in var subport for this client if (frameReceived) { if (c.initially_active) { subports.Add(subport); IPHeaders.Add(IPHeader); c.is_connected = true; } else { string failHeader = ""; subports.Add(failHeader); IPHeaders.Add(failHeader); c.is_connected = false; Debug.LogWarningFormat("Skipped connection to client {0}:{1} ({2})", c.ip, c.port, c.name); } } else { string failHeader = ""; subports.Add(failHeader); IPHeaders.Add(failHeader); c.is_connected = false; Debug.LogWarningFormat("Could not connect to client {0}:{1} ({2}). Make sure address is corect and pupil remote service is running", c.ip, c.port, c.name); } requestSockets.Add(requestSocket); } } catch (Exception e) { Debug.LogWarningFormat("Could not reach to client {0}:{1} ({2}): {4}", c.ip, c.port, c.name, e.ToString()); } } isConnected = true; // check if all clients are connected if (isConnected) { Debug.LogFormat("Connected to {0} sockets", IPHeaders.Count); foreach (String header in IPHeaders) { if (header.Equals("")) { subscriberSockets.Add(new SubscriberSocket()); continue; } else { SubscriberSocket subscriberSocket = new SubscriberSocket(header + subports[IPHeaders.IndexOf(header)]); if (clients[IPHeaders.IndexOf(header)].detect_surface) { subscriberSocket.Subscribe("surface"); } subscriberSocket.Subscribe("pupil."); subscriberSocket.Subscribe("notify."); //subscriberSocket.Subscribe("calibration."); //subscriberSocket.Subscribe("logging.info"); //subscriberSocket.Subscribe("calibration_routines.calibrate"); //subscriberSocket.Subscribe("frame."); //subscriberSocket.Subscribe("gaze."); subscriberSockets.Add(subscriberSocket); } } var msg = new NetMQMessage(); turn = 0; // used receive a message from each client in turn while (!stop_thread_) { if (IPHeaders.Count != clients.Count) { break; } turn = ++turn % IPHeaders.Count; if (IPHeaders[turn].Equals("") || clients[turn].is_connected == false) { continue; } timeout = new System.TimeSpan(0, 0, 0, 0, 1); // wait 200ms to receive a message bool stillAlive = subscriberSockets[turn].TryReceiveMultipartMessage(timeout, ref (msg)); if (stillAlive) { try { string msgType = msg[0].ConvertToString(); var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; if (msgType.Contains("pupil")) { // pupil detected lock (thisLock_) { pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString()); } } if (msgType.Contains("frame")) { } if (msgType.Contains("gaze")) { } if (msgType.Contains("surfaces")) { // surface detected lock (thisLock_) { if (!newData) { newData = true; surfaceData = JsonUtility.FromJson <Pupil.SurfaceData3D>(mmap.ToString()); currentClient = clients[turn]; } } } if (msgType.Equals("notify.calibration.started")) { //Debug.LogFormat("Calibration for client {0} started: {1}", clients[turn].name, mmap.ToString()); } if (msgType.Equals("notify.calibration.failed")) { calibrationDoneClient = clients[turn]; //Debug.LogFormat("Calibration for client {0} failed", clients[turn].name); } if (msgType.Equals("notify.calibration.successful")) { clients[turn].is_calibrated = true; calibrationDoneClient = clients[turn]; //Debug.LogFormat("Calibration for client {0} successful", clients[turn].name); } if (msgType.Equals("notify.calibration.calibration_data")) { //Debug.LogFormat("New calibration data for client {0}: {1}", clients[turn].name, mmap.ToString()); } if (msgType.Equals("logging.info")) { //Debug.LogFormat("logging info for client {0}: {1}", clients[turn].name, mmap.ToString()); } if (msgType.Equals("calibration_routines.calibrate")) { //Debug.LogFormat("Calibration info for client {0}: {1}", clients[turn].name, mmap.ToString()); } } catch { Debug.LogWarningFormat("Failed to deserialize pupil data for client {0}", clients[turn].name); } } } foreach (SubscriberSocket s in subscriberSockets) { s.Close(); } subscriberSockets.Clear(); } else { Debug.LogWarning("Failed to connect to all clients specified in config file"); } NetMQConfig.ContextTerminate(); }