Пример #1
0
        //TODO: potential too severe bottleneck, might have to use a more efficient JSON decoding library
        internal static PipeMessage Deserialize(string rawJson)
        {
            var currProp = string.Empty;
            var msg      = new PipeMessage();

            // Use JsonTextReader instead of DeserializeObject for improved performance
            using (var strReader = new StringReader(rawJson))
                using (var jsonReader = new JsonTextReader(strReader)) {
                    var flag = jsonReader.Read();
                    if (!flag || jsonReader.TokenType != JsonToken.StartObject)
                    {
                        throw new Exception("Malformed or unknown JSON received.");
                    }

                    // MessageType property name
                    flag = jsonReader.Read();
                    Debug.Assert(jsonReader.TokenType == JsonToken.PropertyName);
                    Debug.Assert((string)jsonReader.Value == "type");

                    // MessageType value
                    flag = jsonReader.Read();
                    Debug.Assert(jsonReader.TokenType == JsonToken.String);
                    msg.MessageType = (MessageType)int.Parse((string)jsonReader.Value);

                    // Data property name
                    flag = jsonReader.Read();
                    // Data value
                    flag = jsonReader.Read();

                    // If there are no values in Data, return
                    if (jsonReader.TokenType != JsonToken.StartObject)
                    {
                        return(msg);
                    }

                    while (jsonReader.Read())
                    {
                        if (jsonReader.Value == null)
                        {
                            continue;
                        }

                        switch (jsonReader.TokenType)
                        {
                        case JsonToken.PropertyName:
                            currProp = jsonReader.Value.ToString();
                            break;

                        default:
                            msg.MessageData.Add(currProp, jsonReader.Value);
                            break;
                        }
                    }
                }

            return(msg);
        }
Пример #2
0
        private void ListenForMessages()
        {
            _pipeStream.ReadAsync(_msgBuff, 0, _msgBuff.Length).ContinueWith(t => {
                if (t.Result == 0)
                {
                    OnClientDisconnected?.Invoke(this, null);
                    return;
                }

                var decodedData   = Encoding.Default.GetString(_msgBuff, 0, t.Result);
                var parsedMessage = PipeMessage.Deserialize(decodedData);

                switch (parsedMessage.MessageType)
                {
                case MessageType.Init:
                    if (!_busy)
                    {
                        _busy = true;
                        SendMessage(new PipeMessage(MessageType.Init, new Dictionary <string, object> {
                            { "error", PipeErrorCode.Success }
                        }));

                        OnClientConnected?.Invoke(this, null);
                    }
                    else
                    {
                        SendMessage(new PipeMessage(MessageType.Init, new Dictionary <string, object> {
                            { "error", PipeErrorCode.PipeBusy }
                        }));
                    }
                    break;

                case MessageType.TracerError:
                    //TODO: error message
                    OnTracerError?.Invoke(this,
                                          new TracerErrorEventArgs(
                                              (PipeErrorCode)int.Parse((string)parsedMessage.MessageData["error"]), ""));
                    break;

                default:
                    OnPipeMessageReceived?.Invoke(this, new PipeMessageEventArgs(parsedMessage));
                    break;
                }

                // Keep reading
                ListenForMessages();
            });
        }
Пример #3
0
 public async void SendMessage(PipeMessage msg)
 {
     var jsonBuf = Encoding.UTF8.GetBytes(msg.Serialize());
     await _pipeStream.WriteAsync(jsonBuf, 0, jsonBuf.Length);
 }
Пример #4
0
 public PipeMessageEventArgs(PipeMessage message)
 {
     Message = message;
 }