Ejemplo n.º 1
0
 public UnmanagedStreamBuffer(JsonOperationContext context, Stream stream)
 {
     _stream       = stream;
     _sizeInBytes  = 0;
     Used          = 0;
     _returnBuffer = context.GetManagedBuffer(out _buffer);
 }
Ejemplo n.º 2
0
        private async Task <BlittableJsonReaderObject> Receive(RavenClientWebSocket webSocket,
                                                               JsonOperationContext context)
        {
            BlittableJsonDocumentBuilder builder = null;

            try
            {
                if (webSocket.State != WebSocketState.Open)
                {
                    throw new InvalidOperationException(
                              $"Trying to 'ReceiveAsync' WebSocket while not in Open state. State is {webSocket.State}");
                }

                var state = new JsonParserState();
                JsonOperationContext.ManagedPinnedBuffer buffer;
                using (context.GetManagedBuffer(out buffer))
                    using (var parser = new UnmanagedJsonParser(context, state, "")) //TODO: FIXME
                    {
                        builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None,
                                                                   nameof(TrafficRec) + "." + nameof(Receive), parser, state);
                        builder.ReadObjectDocument();
                        while (builder.Read() == false)
                        {
                            var result = await webSocket.ReceiveAsync(buffer.Buffer, CancellationToken.None);

                            if (result.MessageType == WebSocketMessageType.Close)
                            {
                                if (Logger.IsInfoEnabled)
                                {
                                    Logger.Info("Client got close message from server and is closing connection");
                                }

                                builder.Dispose();
                                // actual socket close from dispose
                                return(null);
                            }

                            if (result.EndOfMessage == false)
                            {
                                throw new EndOfStreamException("Stream ended without reaching end of json content.");
                            }

                            parser.SetBuffer(buffer, result.Count);
                        }
                        builder.FinalizeDocument();

                        return(builder.CreateReader());
                    }
            }
            catch (WebSocketException ex)
            {
                builder?.Dispose();
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info("Failed to receive a message, client was probably disconnected", ex);
                }
                throw;
            }
        }
Ejemplo n.º 3
0
        public AbstractBlittableJsonTextWriter(JsonOperationContext context, Stream stream)
        {
            _context = context;
            _stream  = stream;

            _returnBuffer = context.GetManagedBuffer(out _pinnedBuffer);
            _buffer       = _pinnedBuffer.Pointer;

            _parserAuxiliarMemory = context.GetMemory(32);
        }
Ejemplo n.º 4
0
        public async Task <BlittableJsonReaderObject> TryReadFromWebSocket(
            JsonOperationContext context,
            RavenClientWebSocket webSocket,
            string debugTag,
            CancellationToken cancellationToken)
        {
            var jsonParserState = new JsonParserState();

            JsonOperationContext.ManagedPinnedBuffer bytes;
            using (context.GetManagedBuffer(out bytes))
                using (var parser = new UnmanagedJsonParser(context, jsonParserState, debugTag))
                {
                    var writer = new BlittableJsonDocumentBuilder(context,
                                                                  BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, jsonParserState);

                    writer.ReadObjectDocument();

                    var result = await webSocket.ReceiveAsync(bytes.Buffer, cancellationToken).ConfigureAwait(false);

                    parser.SetBuffer(bytes, result.Count);
                    while (writer.Read() == false)
                    {
                        // we got incomplete json response.
                        // This might happen if we close the connection but still server sends something
                        if (result.CloseStatus != null)
                        {
                            return(null);
                        }

                        result = await webSocket.ReceiveAsync(bytes.Buffer, cancellationToken).ConfigureAwait(false);

                        parser.SetBuffer(bytes, result.Count);
                    }
                    writer.FinalizeDocument();
                    return(writer.CreateReader());
                }
        }
Ejemplo n.º 5
0
        private async Task HandleConnection(WebSocket webSocket, JsonOperationContext context)
        {
            // this flag can be used to detect if server was restarted between changes connections on client side
            var sendStartTime      = GetBoolValueQueryString("sendServerStartTime", false).GetValueOrDefault(false);
            var throttleConnection = GetBoolValueQueryString("throttleConnection", false).GetValueOrDefault(false);

            var connection = new NotificationsClientConnection(webSocket, Database);

            Database.Notifications.Connect(connection);
            var sendTask = connection.StartSendingNotifications(sendStartTime, throttleConnection);
            var debugTag = "changes/" + connection.Id;

            JsonOperationContext.ManagedPinnedBuffer segment1, segment2;
            using (context.GetManagedBuffer(out segment1))
                using (context.GetManagedBuffer(out segment2))
                {
                    try
                    {
                        var segments        = new[] { segment1, segment2 };
                        int index           = 0;
                        var receiveAsync    = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);
                        var jsonParserState = new JsonParserState();
                        using (var parser = new UnmanagedJsonParser(context, jsonParserState, debugTag))
                        {
                            var result = await receiveAsync;
                            parser.SetBuffer(segments[index], result.Count);
                            index++;
                            receiveAsync = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);

                            while (true)
                            {
                                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, jsonParserState))
                                {
                                    parser.NewDocument();
                                    builder.ReadObjectDocument();

                                    while (builder.Read() == false)
                                    {
                                        result = await receiveAsync;

                                        parser.SetBuffer(segments[index], result.Count);
                                        if (++index >= segments.Length)
                                        {
                                            index = 0;
                                        }
                                        receiveAsync = webSocket.ReceiveAsync(segments[index].Buffer, Database.DatabaseShutdown);
                                    }

                                    builder.FinalizeDocument();

                                    using (var reader = builder.CreateReader())
                                    {
                                        string command, commandParameter;
                                        if (reader.TryGet("Command", out command) == false)
                                        {
                                            throw new ArgumentNullException(nameof(command), "Command argument is mandatory");
                                        }

                                        reader.TryGet("Param", out commandParameter);
                                        connection.HandleCommand(command, commandParameter);

                                        int commandId;
                                        if (reader.TryGet("CommandId", out commandId))
                                        {
                                            connection.Confirm(commandId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        /* Client was disconnected, write to log */
                        if (Logger.IsInfoEnabled)
                        {
                            Logger.Info("Client was disconnected", ex);
                        }
                    }
                    finally
                    {
                        Database.Notifications.Disconnect(connection.Id);
                    }
                }
            await sendTask;
        }
Ejemplo n.º 6
0
 public PeepingTomStream(Stream stream, JsonOperationContext context)
 {
     _stream         = stream;
     _returnedBuffer = context.GetManagedBuffer(out _bufferWindow);
 }
Ejemplo n.º 7
0
        private void WriteToServer(string url, Stream serverStream)
        {
            const string debugTag        = "bulk/insert/document";
            var          jsonParserState = new JsonParserState();
            //var streamNetworkBuffer = new BufferedStream(serverStream, 32 * 1024);
            var streamNetworkBuffer = serverStream;
            var writeToStreamBuffer = new byte[32 * 1024];
            var header = Encoding.UTF8.GetBytes(RavenJObject.FromObject(new TcpConnectionHeaderMessage
            {
                DatabaseName = MultiDatabase.GetDatabaseName(url),
                Operation    = TcpConnectionHeaderMessage.OperationTypes.BulkInsert
            }).ToString());

            streamNetworkBuffer.Write(header, 0, header.Length);
            JsonOperationContext.ManagedPinnedBuffer bytes;
            using (_jsonOperationContext.GetManagedBuffer(out bytes))
            {
                while (_documents.IsCompleted == false)
                {
                    _cts.Token.ThrowIfCancellationRequested();

                    MemoryStream jsonBuffer;

                    try
                    {
                        jsonBuffer = _documents.Take();
                    }
                    catch (InvalidOperationException)
                    {
                        break;
                    }

                    var needToThrottle = _throttlingEvent.Wait(0) == false;

                    _jsonOperationContext.ResetAndRenew();
                    using (var jsonParser = new UnmanagedJsonParser(_jsonOperationContext, jsonParserState, debugTag))
                        using (var builder = new BlittableJsonDocumentBuilder(_jsonOperationContext,
                                                                              BlittableJsonDocumentBuilder.UsageMode.ToDisk, debugTag,
                                                                              jsonParser, jsonParserState))
                        {
                            _jsonOperationContext.CachedProperties.NewDocument();
                            builder.ReadObjectDocument();
                            while (true)
                            {
                                var read = jsonBuffer.Read(bytes.Buffer.Array, bytes.Buffer.Offset, bytes.Length);
                                if (read == 0)
                                {
                                    throw new EndOfStreamException("Stream ended without reaching end of json content");
                                }
                                jsonParser.SetBuffer(bytes, read);
                                if (builder.Read())
                                {
                                    break;
                                }
                            }
                            _buffers.Add(jsonBuffer);
                            builder.FinalizeDocument();
                            WriteVariableSizeInt(streamNetworkBuffer, builder.SizeInBytes);
                            WriteToStream(streamNetworkBuffer, builder, writeToStreamBuffer);
                        }

                    if (needToThrottle)
                    {
                        streamNetworkBuffer.Flush();
                        _throttlingEvent.Wait(500);
                    }
                }
                streamNetworkBuffer.WriteByte(0); //done
                streamNetworkBuffer.Flush();
            }
        }