示例#1
0
        private async Task FlushBufferToNetwork()
        {
            ArraySegment <byte> bytes;

            _buffer.TryGetBuffer(out bytes);
            await TcpConnection.Stream.WriteAsync(bytes.Array, bytes.Offset, bytes.Count);

            await TcpConnection.Stream.FlushAsync();

            TcpConnection.RegisterBytesSent(bytes.Count);
            _buffer.SetLength(0);
        }
        private async Task WriteJsonAsync(DynamicJsonValue value)
        {
            int writtenBytes;

            using (TcpConnection.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, TcpConnection.Stream))
                {
                    context.Write(writer, value);
                    writtenBytes = writer.Position;
                }

            await TcpConnection.Stream.FlushAsync();

            TcpConnection.RegisterBytesSent(writtenBytes);
        }
        private void SendErrorToClient(Exception e)
        {
            if (_logger.IsInfoEnabled)
            {
                _logger.Info("Failure during bulk insert", e);
            }

            _messagesToClient.CompleteAdding();
            try
            {
                _replyToCustomer.Wait();
            }
            catch (Exception)
            {
                // we don't care about any errors here, we just need to make sure that the thread
                // isn't sending stuff to the client while we are sending the error
            }
            try
            {
                var error = TcpConnection.Context.ReadObject(new DynamicJsonValue
                {
                    ["Type"]      = "Error",
                    ["Exception"] = e.ToString()
                }, "error/message");

                using (var countingStream = new CountingStream(TcpConnection.Stream))
                {
                    TcpConnection.Context.Write(countingStream, error);
                    TcpConnection.RegisterBytesSent(countingStream.NumberOfWrittenBytes);
                }
            }
            catch (Exception errorSending)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Could not write bulk insert error to client", errorSending);
                }
            }
        }