Пример #1
0
        public static async Task <RavenJToken> ReadFromAsync(JsonTextReaderAsync reader)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync())
                {
                    throw new Exception("Error reading RavenJToken from JsonReader.");
                }
            }

            switch (reader.TokenType)
            {
            case JsonToken.StartObject:
                return(await RavenJObject.LoadAsync(reader));

            case JsonToken.StartArray:
                return(await RavenJArray.LoadAsync(reader));

            case JsonToken.String:
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Date:
            case JsonToken.Boolean:
            case JsonToken.Bytes:
            case JsonToken.Null:
            case JsonToken.Undefined:
                return(new RavenJValue(reader.Value));
            }

            throw new Exception("Error reading RavenJToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
        }
Пример #2
0
        public static RavenJToken Load(JsonTextReaderAsync reader)
        {
            RavenJValue v;

            switch (reader.TokenType)
            {
            case JsonToken.String:
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Date:
            case JsonToken.Boolean:
            case JsonToken.Bytes:
                v = new RavenJValue(reader.Value);
                break;

            case JsonToken.Null:
                v = new RavenJValue(null, JTokenType.Null);
                break;

            case JsonToken.Undefined:
                v = new RavenJValue(null, JTokenType.Undefined);
                break;

            default:
                throw new InvalidOperationException(StringUtils.FormatWith("The JsonReader should not be on a token of type {0}.", CultureInfo.InvariantCulture,
                                                                           reader.TokenType));
            }
            return(v);
        }
Пример #3
0
 public YieldStreamResults(HttpJsonRequest request, Stream stream)
 {
     this.request = request;
     this.stream  = stream;
     streamReader = new StreamReader(stream);
     reader       = new JsonTextReaderAsync(streamReader);
 }
Пример #4
0
        public static async Task <RavenJToken> TryLoadAsync(Stream stream)
        {
            var jsonTextReader = new JsonTextReaderAsync(new StreamReader(stream));

            if (await jsonTextReader.ReadAsync() == false || jsonTextReader.TokenType == JsonToken.None)
            {
                return(null);
            }
            return(await ReadFromAsync(jsonTextReader));
        }
Пример #5
0
        public static async Task <RavenJToken> LoadAsync(JsonTextReaderAsync reader)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync().ConfigureAwait(false))
                {
                    throw new Exception("Error reading RavenJArray from JsonReader.");
                }
            }

            if (reader.TokenType != JsonToken.StartArray)
            {
                throw new Exception("Error reading RavenJArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            if (await reader.ReadAsync().ConfigureAwait(false) == false)
            {
                throw new Exception("Unexpected end of json array");
            }

            var         ar  = new RavenJArray();
            RavenJToken val = null;

            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.Comment:
                    // ignore comments
                    break;

                case JsonToken.EndArray:
                    return(ar);

                case JsonToken.StartObject:
                    val = await RavenJObject.LoadAsync(reader).ConfigureAwait(false);

                    ar.Items.Add(val);
                    break;

                case JsonToken.StartArray:
                    val = await LoadAsync(reader).ConfigureAwait(false);

                    ar.Items.Add(val);
                    break;

                default:
                    val = RavenJValue.Load(reader);
                    ar.Items.Add(val);
                    break;
                }
            } while (await reader.ReadAsync().ConfigureAwait(false));

            throw new Exception("Error reading RavenJArray from JsonReader.");
        }
Пример #6
0
 private async Task <SubscriptionConnectionServerMessage> ReadNextObject(JsonTextReaderAsync jsonReader, bool waitForAck)
 {
     do
     {
         if ((_proccessingCts.IsCancellationRequested || _tcpClient.Connected == false) && waitForAck == false)
         {
             return(null);
         }
         jsonReader.ResetState();
     } while (await jsonReader.ReadAsync().ConfigureAwait(false) == false &&
              _proccessingCts.Token.IsCancellationRequested);// need to do that to handle the heartbeat whitespace
     if (_proccessingCts.Token.IsCancellationRequested && waitForAck == false)
     {
         return(null);
     }
     return((await RavenJObject.LoadAsync(jsonReader).ConfigureAwait(false)).JsonDeserialization <SubscriptionConnectionServerMessage>());
 }
Пример #7
0
        public IEnumerable <Task> ParseJson()
        {
            string text = string.Format(@"{{
  ""AlbumArtUrl"": ""/Content/Images/placeholder.gif"",
  ""Genre"": {{
    ""Id"": ""genres/1"",
    ""Name"": ""Rock""
  }},
  ""Price"": 8.99,
  ""Title"": ""No More Tears (Remastered)"",
  ""CountSold"": 0,
  ""Artist"": {{
    ""Id"": ""artists/114"",
    ""Name"": ""Ozzy Osbourne""
  }}
}}");

            var reader = new JsonTextReaderAsync(new StringReader(text));
            var task   = RavenJObject.LoadAsync(reader);

            yield return(task);

            Assert.IsNotNull(task.Result);
        }
Пример #8
0
        public static async Task <RavenJToken> LoadAsync(JsonTextReaderAsync reader)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync())
                {
                    throw new Exception("Error reading RavenJObject from JsonReader.");
                }
            }

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new Exception(
                          "Error reading RavenJObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            if (await reader.ReadAsync() == false)
            {
                throw new Exception("Unexpected end of json object");
            }

            string propName = null;
            var    o        = new RavenJObject();

            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.Comment:
                    // ignore comments
                    break;

                case JsonToken.PropertyName:
                    propName = reader.Value.ToString();
                    break;

                case JsonToken.EndObject:
                    return(o);

                case JsonToken.StartObject:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = await RavenJObject.LoadAsync(reader);

                        o[propName] = val;                                 // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;

                case JsonToken.StartArray:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = await RavenJArray.LoadAsync(reader);

                        o[propName] = val;                                 // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;

                default:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = RavenJValue.Load(reader);
                        o[propName] = val;                                 // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;
                }
            } while (await reader.ReadAsync());

            throw new Exception("Error reading RavenJObject from JsonReader.");
        }
Пример #9
0
        private async Task ProccessSubscription(TaskCompletionSource <object> successfullyConnected)
        {
            try
            {
                _proccessingCts.Token.ThrowIfCancellationRequested();

                using (var tcpStream = await ConnectToServer().ConfigureAwait(false))
                    using (var reader = new StreamReader(tcpStream))
                        using (var jsonReader = new JsonTextReaderAsync(reader))
                        {
                            _proccessingCts.Token.ThrowIfCancellationRequested();
                            var readObjectTask = ReadNextObject(jsonReader, false);
                            var done           = await Task.WhenAny(readObjectTask, _disposedTask.Task).ConfigureAwait(false);

                            if (done == _disposedTask.Task)
                            {
                                return;
                            }
                            var connectionStatus = await readObjectTask.ConfigureAwait(false);

                            if (_proccessingCts.IsCancellationRequested)
                            {
                                return;
                            }

                            AssertConnectionState(connectionStatus);

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            Task.Run(() => successfullyConnected.TrySetResult(null));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

                            readObjectTask = ReadNextObject(jsonReader, waitForAck: false);

                            if (_proccessingCts.IsCancellationRequested)
                            {
                                return;
                            }

                            var incomingBatch = new List <RavenJObject>();
                            _lastReceivedEtag = 0;
                            bool waitingForAck = false;
                            while (_proccessingCts.IsCancellationRequested == false || waitingForAck)
                            {
                                BeforeBatch();
                                bool endOfBatch = false;
                                while ((endOfBatch == false && _proccessingCts.IsCancellationRequested == false) || waitingForAck)
                                {
                                    done = await Task.WhenAny(readObjectTask, _disposedTask.Task).ConfigureAwait(false);

                                    SubscriptionConnectionServerMessage receivedMessage;
                                    if (done == _disposedTask.Task)
                                    {
                                        if (waitingForAck == false)
                                        {
                                            break;
                                        }
                                        waitingForAck   = false; // we will only wait once
                                        receivedMessage = await readObjectTask.ConfigureAwait(false);
                                    }
                                    else
                                    {
                                        receivedMessage = await readObjectTask.ConfigureAwait(false);

                                        if (receivedMessage == null)
                                        {
                                            break; // cancelled
                                        }
                                        readObjectTask = ReadNextObject(jsonReader, waitForAck: true);
                                    }

                                    switch (receivedMessage.Type)
                                    {
                                    case SubscriptionConnectionServerMessage.MessageType.Data:
                                        incomingBatch.Add(receivedMessage.Data);
                                        break;

                                    case SubscriptionConnectionServerMessage.MessageType.EndOfBatch:
                                        endOfBatch = true;
                                        break;

                                    case SubscriptionConnectionServerMessage.MessageType.Confirm:
                                        AfterAcknowledgment();
                                        AfterBatch(incomingBatch.Count);
                                        incomingBatch.Clear();
                                        waitingForAck = false;
                                        break;

                                    case SubscriptionConnectionServerMessage.MessageType.Error:
                                        switch (receivedMessage.Status)
                                        {
                                        case SubscriptionConnectionServerMessage.ConnectionStatus.Closed:
                                            throw new SubscriptionClosedException(receivedMessage.Exception ?? string.Empty);

                                        default:
                                        {
                                            throw new Exception(
                                                      $"Connection terminated by server. Exception: {receivedMessage.Exception ?? "None"}");
                                        }
                                        }

                                    default:
                                        throw new ArgumentException(
                                                  $"Unrecognized message '{receivedMessage?.Type}' type received from server");
                                    }
                                }

                                if (_proccessingCts.IsCancellationRequested)
                                {
                                    break;
                                }

                                foreach (var curDoc in incomingBatch)
                                {
                                    NotifySubscribers(curDoc, out _lastReceivedEtag);
                                }

                                SendAck(_lastReceivedEtag, tcpStream);
                                waitingForAck = true;
                            }
                        }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                if (_proccessingCts.Token.IsCancellationRequested == false)
                {
                    InformSubscribersOnError(ex);
                }
                throw;
            }
        }