Exemplo n.º 1
0
        /// <summary>
        ///		Processes asynchronous operation completion logic.
        /// </summary>
        /// <param name="context">The response context which holds response data.</param>
        /// <param name="exception">The exception occured.</param>
        /// <param name="completedSynchronously">When operation is completed same thread as initiater then <c>true</c>; otherwise, <c>false</c>.</param>
        public void OnCompleted(ClientResponseContext context, Exception exception, bool completedSynchronously)
        {
            if (exception != null)
            {
                base.OnError(exception, completedSynchronously);
            }
            else
            {
                var error = ErrorInterpreter.UnpackError(context);
                if (!error.IsSuccess)
                {
                    base.OnError(error.ToException(), completedSynchronously);
                }
                else
                {
                    Interlocked.CompareExchange(ref this._result, new ResultHolder(Unpacking.UnpackObject(context.ResultBuffer)), null);
                    base.Complete(completedSynchronously);
                }
            }

            var callback = this.AsyncCallback;

            if (callback != null)
            {
                callback(this);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the fields cursor.
        /// </summary>
        private ClientFieldsQueryCursor GetFieldsCursor(ClientResponseContext ctx)
        {
            var cursorId    = ctx.Stream.ReadLong();
            var columnNames = ClientFieldsQueryCursor.ReadColumns(ctx.Reader);

            return(new ClientFieldsQueryCursor(ctx.Socket, cursorId, _keepBinary, ctx.Stream,
                                               ClientOp.QuerySqlFieldsCursorGetPage, columnNames));
        }
        private static ClientResponseContext CreateContext()
        {
            var context = new ClientResponseContext();

            context.ErrorBuffer  = new ByteArraySegmentStream(new ArraySegment <byte>[] { new ArraySegment <byte>(new byte[] { 0xC0 }) });
            context.ResultBuffer = new ByteArraySegmentStream(new ArraySegment <byte>[] { new ArraySegment <byte>(new byte[] { 0xC0 }) });
            return(context);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the fields cursor.
        /// </summary>
        private ClientQueryCursorBase <T> GetFieldsCursorNoColumnNames <T>(ClientResponseContext ctx,
                                                                           Func <IBinaryRawReader, int, T> readerFunc)
        {
            var cursorId    = ctx.Stream.ReadLong();
            var columnCount = ctx.Stream.ReadInt();

            return(new ClientQueryCursorBase <T>(ctx.Socket, cursorId, _keepBinary, ctx.Stream,
                                                 ClientOp.QuerySqlFieldsCursorGetPage, r => readerFunc(r, columnCount)));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Unmarshals the value, wrapping in a cache result.
        /// </summary>
        private CacheResult <T> UnmarshalCacheResult <T>(ClientResponseContext ctx)
        {
            var stream = ctx.Stream;
            var hdr    = stream.ReadByte();

            if (hdr == BinaryUtils.HdrNull)
            {
                return(new CacheResult <T>());
            }

            stream.Seek(-1, SeekOrigin.Current);

            return(new CacheResult <T>(_marsh.Unmarshal <T>(stream, _keepBinary)));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Unmarshals the value, throwing an exception for nulls.
        /// </summary>
        private T UnmarshalNotNull <T>(ClientResponseContext ctx)
        {
            var stream = ctx.Stream;
            var hdr    = stream.ReadByte();

            if (hdr == BinaryUtils.HdrNull)
            {
                throw GetKeyNotFoundException();
            }

            stream.Seek(-1, SeekOrigin.Current);

            return(_marsh.Unmarshal <T>(stream, _keepBinary));
        }
Exemplo n.º 7
0
        private static object ReadJavaTaskResponse <TRes>(ClientResponseContext ctx, TaskCompletionSource <TRes> tcs,
                                                          bool keepBinary)
        {
            var taskId = ctx.Stream.ReadLong();

            ctx.Socket.AddNotificationHandler(taskId, (stream, ex) =>
            {
                if (ex != null)
                {
                    tcs.TrySetException(ex);
                    return;
                }

                ctx.Socket.RemoveNotificationHandler(taskId);

                var reader = ctx.Marshaller.StartUnmarshal(stream,
                                                           keepBinary ? BinaryMode.ForceBinary : BinaryMode.Deserialize);

                try
                {
                    var flags  = (ClientFlags)reader.ReadShort();
                    var opCode = (ClientOp)reader.ReadShort();

                    if (opCode != ClientOp.ComputeTaskFinished)
                    {
                        tcs.TrySetException(new IgniteClientException(
                                                string.Format("Invalid server notification code. Expected {0}, but got {1}",
                                                              ClientOp.ComputeTaskFinished, opCode)));
                    }
                    else if ((flags & ClientFlags.Error) == ClientFlags.Error)
                    {
                        var status = (ClientStatusCode)reader.ReadInt();
                        var msg    = reader.ReadString();

                        tcs.TrySetException(new IgniteClientException(msg, null, status));
                    }
                    else
                    {
                        tcs.TrySetResult(reader.ReadObject <TRes>());
                    }
                }
                catch (Exception e)
                {
                    tcs.TrySetException(e);
                }
            });

            return(null);
        }
        private static ClientResponseContext CreateContext(RpcErrorMessage message)
        {
            var context = new ClientResponseContext();

            using (var buffer = new MemoryStream())
                using (var packer = Packer.Create(buffer, false))
                {
                    packer.Pack(message.Error.Identifier);
                    context.ErrorBuffer = new ByteArraySegmentStream(new[] { new ArraySegment <byte>(buffer.ToArray()) });
                    buffer.SetLength(0);
                    packer.Pack(message.Detail);
                    context.ResultBuffer = new ByteArraySegmentStream(new[] { new ArraySegment <byte>(buffer.ToArray()) });
                }

            return(context);
        }
Exemplo n.º 9
0
        private static object ReadJavaTaskResponse <TRes>(ClientResponseContext ctx, TaskCompletionSource <TRes> tcs,
                                                          CancellationToken cancellationToken, bool keepBinary)
        {
            var taskId = ctx.Stream.ReadLong();

            cancellationToken.Register(() =>
            {
                ctx.Socket.DoOutInOpAsync <object>(ClientOp.ResourceClose,
                                                   c => c.Stream.WriteLong(taskId),
                                                   _ => null,
                                                   (status, msg) =>
                {
                    if (status == ClientStatusCode.ResourceDoesNotExist)
                    {
                        // Task finished before we could cancel it - ignore.
                        return(null);
                    }

                    throw new IgniteClientException(msg, null, status);
                });
            });

            ctx.Socket.AddNotificationHandler(taskId, (stream, ex) =>
            {
                if (ex != null)
                {
                    tcs.TrySetException(ex);
                    return;
                }

                ctx.Socket.RemoveNotificationHandler(taskId);

                var reader = ctx.Marshaller.StartUnmarshal(stream,
                                                           keepBinary ? BinaryMode.ForceBinary : BinaryMode.Deserialize);

                try
                {
                    var flags  = (ClientFlags)reader.ReadShort();
                    var opCode = (ClientOp)reader.ReadShort();

                    if (opCode != ClientOp.ComputeTaskFinished)
                    {
                        tcs.TrySetException(new IgniteClientException(
                                                string.Format("Invalid server notification code. Expected {0}, but got {1}",
                                                              ClientOp.ComputeTaskFinished, opCode)));
                    }
                    else if ((flags & ClientFlags.Error) == ClientFlags.Error)
                    {
                        var status = (ClientStatusCode)reader.ReadInt();
                        var msg    = reader.ReadString();

                        tcs.TrySetException(new IgniteClientException(msg, null, status));
                    }
                    else
                    {
                        tcs.TrySetResult(reader.ReadObject <TRes>());
                    }
                }
                catch (Exception e)
                {
                    tcs.TrySetException(e);
                }
            });

            return(null);
        }
Exemplo n.º 10
0
        /// <summary>
        ///		Unpacks <see cref="RpcErrorMessage"/> from stream in the specified context.
        /// </summary>
        /// <param name="context"><see cref="ClientResponseContext"/> which stores serialized error.</param>
        /// <returns>An unpacked <see cref="RpcErrorMessage"/>.</returns>
        internal static RpcErrorMessage UnpackError(ClientResponseContext context)
        {
            Contract.Assert(context != null);
            Contract.Assert(context.ErrorBuffer != null);
            Contract.Assert(context.ErrorBuffer.Length > 0);
            Contract.Assert(context.ResultBuffer != null);
            Contract.Assert(context.ResultBuffer.Length > 0);

            MessagePackObject error;

            try
            {
                error = Unpacking.UnpackObject(context.ErrorBuffer);
            }
            catch (UnpackException)
            {
                error = new MessagePackObject(context.ErrorBuffer.GetBuffer().SelectMany(segment => segment.AsEnumerable()).ToArray());
            }

            if (error.IsNil)
            {
                return(RpcErrorMessage.Success);
            }

            bool     isUnknown = false;
            RpcError errorIdentifier;

            if (error.IsTypeOf <string>().GetValueOrDefault())
            {
                var asString = error.AsString();
                errorIdentifier = RpcError.FromIdentifier(asString, null);
                // Check if the error is truely Unexpected error.
                isUnknown = errorIdentifier.ErrorCode == RpcError.Unexpected.ErrorCode && asString != RpcError.Unexpected.Identifier;
            }
            else if (error.IsTypeOf <int>().GetValueOrDefault())
            {
                errorIdentifier = RpcError.FromIdentifier(null, error.AsInt32());
            }
            else
            {
                errorIdentifier = RpcError.Unexpected;
                isUnknown       = true;
            }

            MessagePackObject detail;

            if (context.ResultBuffer.Length == 0)
            {
                detail = MessagePackObject.Nil;
            }
            else
            {
                try
                {
                    detail = Unpacking.UnpackObject(context.ResultBuffer);
                }
                catch (UnpackException)
                {
                    detail = new MessagePackObject(context.ResultBuffer.GetBuffer().SelectMany(segment => segment.AsEnumerable()).ToArray());
                }
            }

            if (isUnknown)
            {
                // Unknown error, the error should contain original Error field as message.
                if (detail.IsNil)
                {
                    return(new RpcErrorMessage(errorIdentifier, error.AsString(), null));
                }
                else
                {
                    var details = new MessagePackObjectDictionary(2);
                    details[RpcException.MessageKeyUtf8]          = error;
                    details[RpcException.DebugInformationKeyUtf8] = detail;
                    return(new RpcErrorMessage(errorIdentifier, new MessagePackObject(details, true)));
                }
            }
            else
            {
                return(new RpcErrorMessage(errorIdentifier, detail));
            }
        }