protected internal override System.DayOfWeek UnpackFromUnderlyingValue(MsgPack.MessagePackObject messagePackObject) { return((System.DayOfWeek)(messagePackObject.AsInt32())); }
protected internal override MsgPack.Serialization.EnumUInt64 UnpackFromUnderlyingValue(MsgPack.MessagePackObject messagePackObject) { return((MsgPack.Serialization.EnumUInt64)(messagePackObject.AsUInt64())); }
protected internal override MsgPack.Serialization.EnumDefault UnpackFromUnderlyingValue(MsgPack.MessagePackObject messagePackObject) { return((MsgPack.Serialization.EnumDefault)(messagePackObject.AsInt32())); }
protected internal override MsgPack.Serialization.EnumInt16Flags UnpackFromUnderlyingValue(MsgPack.MessagePackObject messagePackObject) { return((MsgPack.Serialization.EnumInt16Flags)(messagePackObject.AsInt16())); }
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(); }
protected override IO.Ably.Types.ProtocolMessage.MessageAction UnpackFromUnderlyingValue(MsgPack.MessagePackObject messagePackObject) { return((IO.Ably.Types.ProtocolMessage.MessageAction)(messagePackObject.AsInt32())); }
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); // 1 sec // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); Debug.Log("Connect to the server: " + IPHeader + ServicePort + "."); _requestSocket = new RequestSocket(IPHeader + ServicePort); string subport = ""; _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 > TrackingConfidenceThreshold) { OnPacket(_pupilData); } } } //Debug.Log(message); } catch { // Debug.Log("Failed to unpack."); } } else { Debug.LogWarning("Failed to receive a message."); Thread.Sleep(50); } } 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(); }
static void Main(string[] args) { logger = new AllPet.Common.Logger(); logger.Warn("Allpet.Node v0.001 Peer 01"); var config = new Config(logger); //init current path. //把当前目录搞对,怎么启动都能找到dll了 var lastpath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);; Console.WriteLine("exepath=" + lastpath); Environment.CurrentDirectory = lastpath; var system = AllPet.Pipeline.PipelineSystem.CreatePipelineSystemV1(new AllPet.Common.Logger()); var config_node = config.GetJson("config.json", ".ModulesConfig.Node") as JObject; if (Config.IsOpen(config_node)) { system.RegistModule("node", new AllPet.Module.Module_Node(logger, config_node)); } else { logger.Error("cant find config for node"); return; } system.OpenNetwork(new AllPet.peer.tcp.PeerOption() { }); var endpoint = config.GetIPEndPoint("config.json", ".ListenEndPoint"); if (endpoint != null && endpoint.Port != 0) { try { system.OpenListen(endpoint); } catch (Exception err) { logger.Error("listen error:" + err.ToString()); } } system.Start(); //等待cli结束才退出 var pipeline = system.GetPipeline(null, "this/node"); while (pipeline.IsVaild) { var line = Console.ReadLine(); if (string.IsNullOrEmpty(line) == false) { if (line == "exit") { break; } var cmds = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); var dict = new MsgPack.MessagePackObjectDictionary(); dict["cmd"] = (UInt16)AllPet.Module.CmdList.Local_Cmd; var list = new MsgPack.MessagePackObject[cmds.Length]; for (var i = 0; i < cmds.Length; i++) { list[i] = cmds[i]; } dict["params"] = list; pipeline.Tell(new MsgPack.MessagePackObject(dict)); } } system.Dispose(); }
object unpackData(byte[] data) { MsgPack.MessagePackObject mpobj = MsgPack.Unpacking.UnpackObject(data).Value; return(convertFromMsgPackObject(mpobj)); }
protected override System.Net.HttpStatusCode UnpackFromUnderlyingValue(MsgPack.MessagePackObject messagePackObject) { return((System.Net.HttpStatusCode)(messagePackObject.AsInt32())); }
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(); }