Exemplo n.º 1
0
        public void MessagePoll()
        {
            NetMQ.Msg msg = new NetMQ.Msg();
            msg.InitEmpty();
            if (_socket.TryReceive(ref msg, TimeSpan.FromMilliseconds(1)))
            {
                switch ((MessageType)msg.Data[0])
                {
                case MessageType.Heartbeat:     //heartbeat

                    break;

                case MessageType.Debug:     // debug
                    if (_socket.TryReceive(ref msg, TimeSpan.FromMilliseconds(1)))
                    {
                        string text = System.Text.Encoding.ASCII.GetString(msg.Data);
                        if (text == "{}")
                        {
                            break;
                        }
                        JObject jo   = (JObject)JsonConvert.DeserializeObject(text);
                        var     type = jo["type"].Value <string>();
                        if (type == "get-program")
                        {
                            GameState.Program = DreyProgram.FromJson(jo);
                            DataArrived?.Invoke(new GetProgramEventArgs()
                            {
                                Program = GameState.Program
                            });
                        }
                        else if (type == "announce")
                        {
                            var deserialized = JsonConvert.DeserializeObject <GameState>(text, new ObjectTypeDeserializer());
                            GameState.Announce(deserialized);
                            DataArrived?.Invoke(new AnnounceEventArgs()
                            {
                                State = GameState
                            });
                        }
                        else if (type == "set-breakpoint")
                        {
                            var address = jo["address"].Value <int>();
                            GameState.Breakpoints.Add(address);
                            DataArrived?.Invoke(new BreakPointEventArgs()
                            {
                                Address = address, Set = true
                            });
                        }
                        else if (type == "clear-breakpoint")
                        {
                            var address = jo["address"].Value <int>();
                            GameState.Breakpoints.Remove(address);
                            DataArrived?.Invoke(new BreakPointEventArgs()
                            {
                                Address = address, Set = false
                            });
                        }
                        else if (type == "debug-msg")
                        {
                            var msg2 = jo["message"].Value <string>();
                            DataArrived?.Invoke(new DebugMessageEventArgs()
                            {
                                Message = msg2
                            });
                        }
                        else if (type == "debug-msg-line")
                        {
                            var msg3 = jo["message"].Value <string>();
                            DataArrived?.Invoke(new DebugMessageEventArgs()
                            {
                                Message = msg3 + Environment.NewLine
                            });
                        }
                    }
                    break;

                case MessageType.Data:     // data
                    if (_socket.TryReceive(ref msg, TimeSpan.FromMilliseconds(1)))
                    {
                        string text = System.Text.Encoding.ASCII.GetString(msg.Data);

                        var ser = new Newtonsoft.Json.JsonSerializer();
                        using (var reader = new JsonTextReader(new StringReader(text)))
                        {
                            PendingChoice choice = ser.Deserialize <PendingChoice>(reader);
                            GameState.PendingChoice = choice;
                        }

                        DataArrived?.Invoke(new DataEventArgs()
                        {
                            Data = text
                        });
                    }

                    break;
                }
            }
        }