/**
         * <summary>
         * Wraps task result bean into a protocol message.</summary>
         *
         * <param name="bean">Task result that need to be wrapped.</param>
         * <returns>Wrapped message.</returns>
         */
        private static ProtoTaskBean WrapTaskResult(GridClientTaskResultBean bean)
        {
            ProtoTaskBean.Builder builder = ProtoTaskBean.CreateBuilder()
                                            .SetTaskId(bean.TaskId)
                                            .SetFinished(bean.IsFinished);

            if (bean.Error != null)
            {
                builder.SetError(bean.Error);
            }

            if (bean.Result != null)
            {
                builder.SetResultBean(WrapObject(bean.Result));
            }

            return(builder.Build());
        }
        /**
         * <summary>
         * Unwraps protocol message to a task result bean.</summary>
         *
         * <param name="bean">Protocol message to unwrap.</param>
         * <returns>Unwrapped message.</returns>
         */
        private static GridClientTaskResultBean WrapTaskResult(ProtoTaskBean bean)
        {
            GridClientTaskResultBean res = new GridClientTaskResultBean();

            res.TaskId     = bean.TaskId;
            res.IsFinished = bean.Finished;

            if (bean.HasError)
            {
                res.Error = bean.Error;
            }

            if (bean.HasResultBean)
            {
                res.Result = WrapObject(bean.ResultBean);
            }

            return(res);
        }
        /**
         * <summary>
         * Converts protocol object into object.</summary>
         *
         * <param name="val">Protocol message object to convert into value.</param>
         * <returns>Recovered object.</returns>
         */
        public static Object WrapObject(ObjectWrapper val)
        {
            byte[] bin = val.Binary.ToByteArray();

            // Primitives.

            switch (val.Type)
            {
            case ObjectWrapperType.NONE:
                return(null);

            case ObjectWrapperType.BOOL:
                Dbg.Assert(bin.Length == 1, "bin.Length == 1");

                return(bin[0] != 0);

            case ObjectWrapperType.BYTE:
                Dbg.Assert(bin.Length == 1, "bin.Length == 1");

                return(bin[0]);

            case ObjectWrapperType.SHORT:
                Dbg.Assert(bin.Length == 2, "bin.Length == 2");

                return(U.BytesToInt16(bin, 0));

            case ObjectWrapperType.INT32:
                Dbg.Assert(bin.Length == 4, "bin.Length == 4");

                return(U.BytesToInt32(bin, 0));

            case ObjectWrapperType.INT64:
                Dbg.Assert(bin.Length == 8, "bin.Length == 8");

                return(U.BytesToInt64(bin, 0));

            case ObjectWrapperType.FLOAT:
                Dbg.Assert(bin.Length == 4, "bin.Length == 4");

                return(U.BytesToSingle(bin, 0));

            case ObjectWrapperType.DOUBLE:
                Dbg.Assert(bin.Length == 8, "bin.Length == 8");

                return(U.BytesToDouble(bin, 0));

            case ObjectWrapperType.BYTES:
                return(bin);

            case ObjectWrapperType.UUID:
                return(WrapGuid(val.Binary));

            case ObjectWrapperType.STRING:
                return(val.Binary.ToStringUtf8());

            case ObjectWrapperType.COLLECTION:
                return(WrapCollection(Collection.ParseFrom(bin)));

            case ObjectWrapperType.MAP:
                return(WrapMap(Map.ParseFrom(bin)));

            case ObjectWrapperType.AUTH_REQUEST:
                return(WrapAuthRequest(ProtoRequest.ParseFrom(bin)));

            case ObjectWrapperType.CACHE_REQUEST:
                return(WrapCacheRequest(ProtoRequest.ParseFrom(bin)));

            case ObjectWrapperType.TASK_REQUEST:
                return(WrapTaskRequest(ProtoRequest.ParseFrom(bin)));

            case ObjectWrapperType.LOG_REQUEST:
                return(WrapLogRequest(ProtoRequest.ParseFrom(bin)));

            case ObjectWrapperType.TOPOLOGY_REQUEST:
                return(WrapTopologyRequest(ProtoRequest.ParseFrom(bin)));

            case ObjectWrapperType.RESPONSE:
                return(WrapResponse(ProtoResponse.ParseFrom(bin)));

            case ObjectWrapperType.NODE_BEAN:
                return(WrapNode(ProtoNodeBean.ParseFrom(bin)));

            case ObjectWrapperType.TASK_BEAN:
                return(WrapTaskResult(ProtoTaskBean.ParseFrom(bin)));

            default:
                throw new ArgumentException("Failed to deserialize object (object deserialization" +
                                            " of given type is not supported): " + val.Type);
            }
        }