Exemplo n.º 1
0
        public static RpcMessage.Parameter BoolToMessage(bool parameter)
        {
            var message = new RpcMessage.Parameter();

            message.BoolParam = parameter;
            return(message);
        }
Exemplo n.º 2
0
        public static bool ProtoFromMessage(RpcMessage.Parameter message, Type type, ref object value,
                                            ref string error)
        {
            //check the server returned a protobuf message
            if (message == null)
            {
                error = String.Format(
                    "Server had unexpected return type, was expecting a ProtoBuf message of type {0}.", type);
                return(false);
            }

            if (message.IsNull)
            {
                value = null;
                return(true);
            }

            //check the server returned a protobuf message
            if (message.ProtoParam == null)
            {
                error = String.Format(
                    "Server had unexpected return type, was expecting a ProtoBuf message of type {0}.", type);
                return(false);
            }

            //can't check the protobuf message was the expected type, as protobuf-net is much too lenient, it doesn't
            //check required fields or care about invalid fields either
            var memStream = new MemoryStream(message.ProtoParam);

            value = Serializer.NonGeneric.Deserialize(type, memStream);
            return(true);
        }
Exemplo n.º 3
0
        public static RpcMessage.Parameter UintToMessage(uint parameter)
        {
            var message = new RpcMessage.Parameter();

            message.UintParam = parameter;
            return(message);
        }
Exemplo n.º 4
0
        public static RpcMessage.Parameter Uint64ToMessage(ulong parameter)
        {
            var message = new RpcMessage.Parameter();

            message.Uint64Param = parameter;
            return(message);
        }
Exemplo n.º 5
0
        public static RpcMessage.Parameter DoubleToMessage(double parameter)
        {
            var message = new RpcMessage.Parameter();

            message.DoubleParam = parameter;
            return(message);
        }
Exemplo n.º 6
0
        public static RpcMessage.Parameter FloatToMessage(float parameter)
        {
            var message = new RpcMessage.Parameter();

            message.FloatParam = parameter;
            return(message);
        }
Exemplo n.º 7
0
 public static bool Uint64FromMessage(RpcMessage.Parameter message, ref ulong value, ref string error)
 {
     if (!message.Uint64ParamSpecified)
     {
         error = "Parameter did not have expected ulong type.";
         return(false);
     }
     value = message.Uint64Param;
     return(true);
 }
Exemplo n.º 8
0
 public static bool BoolFromMessage(RpcMessage.Parameter message, ref bool value, ref string error)
 {
     if (!message.BoolParamSpecified)
     {
         error = "Parameter did not have expected bool type.";
         return(false);
     }
     value = message.BoolParam;
     return(true);
 }
Exemplo n.º 9
0
 public static bool FloatFromMessage(RpcMessage.Parameter message, ref float value, ref string error)
 {
     if (!message.FloatParamSpecified)
     {
         error = "Parameter did not have expected float type.";
         return(false);
     }
     value = message.FloatParam;
     return(true);
 }
Exemplo n.º 10
0
 public static bool DoubleFromMessage(RpcMessage.Parameter message, ref double value, ref string error)
 {
     if (!message.DoubleParamSpecified)
     {
         error = "Parameter did not have expected double type.";
         return(false);
     }
     value = message.DoubleParam;
     return(true);
 }
Exemplo n.º 11
0
        public static double DoubleFromMessage(RpcMessage.Parameter parameter)
        {
            double value = 0.0;
            string error = null;

            if (!ParameterConverter.DoubleFromMessage(parameter, ref value, ref error))
            {
                throw new ArgumentException(
                          String.Format("Cannot convert server return value into expected double return type ({0})", error));
            }
            return(value);
        }
Exemplo n.º 12
0
        public static ulong Uint64FromMessage(RpcMessage.Parameter parameter)
        {
            ulong  value = 0;
            string error = null;

            if (!ParameterConverter.Uint64FromMessage(parameter, ref value, ref error))
            {
                throw new ArgumentException(
                          String.Format("Cannot convert server return value into expected uint64 return type ({0})", error));
            }
            return(value);
        }
Exemplo n.º 13
0
 public void ReceiveResult(RpcMessage.Result resultMessage)
 {
     mResult             = resultMessage.CallResult;
     mIsCompleted        = true;
     mIsFailed           = resultMessage.IsFailed;
     mServerErrorMessage = resultMessage.ErrorMessage;
     if (mAsyncCallback != null)
     {
         mAsyncCallback(this);
     }
     mWaitHandle.Set();
 }
Exemplo n.º 14
0
        public static bool BoolFromMessage(RpcMessage.Parameter parameter)
        {
            bool   value = false;
            string error = null;

            if (!ParameterConverter.BoolFromMessage(parameter, ref value, ref error))
            {
                throw new ArgumentException(
                          String.Format("Cannot convert server return value into expected bool return type ({0})", error));
            }
            return(value);
        }
Exemplo n.º 15
0
        public static object ProtoFromMessage(RpcMessage.Parameter parameter, Type type)
        {
            object value = null;
            string error = null;

            if (!ParameterConverter.ProtoFromMessage(parameter, type, ref value, ref error))
            {
                throw new ArgumentException(
                          String.Format("Cannot convert server return value into expected protobuf return type '{0}' ({1})",
                                        type, error));
            }
            return(value);
        }
Exemplo n.º 16
0
 private static RpcMessage.Parameter[] CreateParameters(object[] args, string service, string method)
 {
     RpcMessage.Parameter[] parameters = new RpcMessage.Parameter[args.Length];
     for (int i = 0; i < args.Length; ++i)
     {
         string errorMsg;
         if (!ParameterConverter.ToMessage(args[i], out parameters[i], out errorMsg))
         {
             throw new InvalidRpcCallException(service, method,
                                               String.Format("Couldn't use parameter in rpc call ({0}).", errorMsg));
         }
     }
     return(parameters);
 }
Exemplo n.º 17
0
        public static RpcMessage.Parameter StringToMessage(string parameter)
        {
            var message = new RpcMessage.Parameter();

            if (parameter == null)
            {
                message.IsNull = true;
            }
            else
            {
                message.StringParam = parameter;
            }
            return(message);
        }
Exemplo n.º 18
0
 public static bool StringFromMessage(RpcMessage.Parameter message, ref string value, ref string error)
 {
     if (message.IsNull)
     {
         value = null;
         return(true);
     }
     if (!message.StringParamSpecified)
     {
         error = "Parameter did not have expected string type.";
         return(false);
     }
     value = message.StringParam;
     return(true);
 }
Exemplo n.º 19
0
        public static RpcMessage.Parameter ProtoToMessage(object parameter)
        {
            var message = new RpcMessage.Parameter();

            if (parameter == null)
            {
                message.IsNull = true;
            }
            else
            {
                var memStream = new MemoryStream();
                Serializer.NonGeneric.Serialize(memStream, parameter);
                message.ProtoParam = memStream.ToArray();
            }
            return(message);
        }
Exemplo n.º 20
0
        public static bool BoolFromMessage(RpcMessage.Parameter parameter, RpcMessage.Result resultMessage,
                                           ref bool value)
        {
            string error = null;

            if (!ParameterConverter.BoolFromMessage(parameter, ref value, ref error))
            {
                if (resultMessage != null)
                {
                    resultMessage.IsFailed     = true;
                    resultMessage.ErrorMessage =
                        String.Format("Cannot convert call parameter value into expected bool type ({0})", error);
                }
                return(false);
            }
            return(true);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Converts an object to a parameter message.
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="message"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static bool ToMessage(object parameter, out RpcMessage.Parameter message, out string errorMsg)
        {
            message = new RpcMessage.Parameter();
            if (parameter == null)
            {
                message.IsNull = true;
            }
            else
            {
                if (parameter is string)
                {
                    message.StringParam = (string)parameter;
                }
                else if (parameter is bool)
                {
                    message.BoolParam = (bool)parameter;
                }
                else if (parameter is int)
                {
                    message.IntParam = (int)parameter;
                }
                else if (parameter is uint)
                {
                    message.UintParam = (uint)parameter;
                }
                else if (parameter is float)
                {
                    message.FloatParam = (float)parameter;
                }
                else if (parameter is double)
                {
                    message.DoubleParam = (double)parameter;
                }
                else if (parameter is sbyte)
                {
                    message.IntParam = (sbyte)parameter;
                }
                else if (parameter is byte)
                {
                    message.UintParam = (byte)parameter;
                }
                else if (parameter is char)
                {
                    message.UintParam = (char)parameter;
                }
                else if (parameter is short)
                {
                    message.IntParam = (short)parameter;
                }
                else if (parameter is ushort)
                {
                    message.UintParam = (ushort)parameter;
                }
                else if (parameter is long)
                {
                    message.Int64Param = (long)parameter;
                }
                else if (parameter is ulong)
                {
                    message.Uint64Param = (ulong)parameter;
                }
                else
                {
                    //Must be a protobuf message
                    Type     parameterType = parameter.GetType();
                    object[] attrs         = parameterType.GetCustomAttributes(typeof(ProtoContractAttribute), true);
                    if (attrs.Length < 1)
                    {
                        errorMsg = String.Format("Parameter of type '{0}' is not supported. Only integral, string, and protobuf types are supported",
                                                 parameterType);
                        return(false);
                    }
                    var memStream = new MemoryStream();
                    Serializer.NonGeneric.Serialize(memStream, parameter);
                    message.ProtoParam = memStream.ToArray();
                }
            }

            errorMsg = null;
            return(true);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Converts a parameter message into an object.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="parameterType"></param>
        /// <param name="parameter"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static bool FromMessage(RpcMessage.Parameter message, Type parameterType, out object parameter,
                                       out string errorMsg)
        {
            if (parameterType.IsPrimitive)
            {
                if (parameterType == typeof(bool) && message.BoolParamSpecified)
                {
                    parameter = message.BoolParam;
                }
                else if (parameterType == typeof(byte) && message.UintParamSpecified)
                {
                    parameter = (byte)message.UintParam;
                }
                else if (parameterType == typeof(sbyte) && message.IntParamSpecified)
                {
                    parameter = (sbyte)message.IntParam;
                }
                else if (parameterType == typeof(char) && message.UintParamSpecified)
                {
                    parameter = (char)message.UintParam;
                }
                else if (parameterType == typeof(short) && message.IntParamSpecified)
                {
                    parameter = (short)message.IntParam;
                }
                else if (parameterType == typeof(ushort) && message.UintParamSpecified)
                {
                    parameter = (ushort)message.UintParam;
                }
                else if (parameterType == typeof(int) && message.IntParamSpecified)
                {
                    parameter = message.IntParam;
                }
                else if (parameterType == typeof(uint) && message.UintParamSpecified)
                {
                    parameter = message.UintParam;
                }
                else if (parameterType == typeof(long) && message.Int64ParamSpecified)
                {
                    parameter = message.Int64Param;
                }
                else if (parameterType == typeof(ulong) && message.Uint64ParamSpecified)
                {
                    parameter = message.Uint64Param;
                }
                else if (parameterType == typeof(float) && message.FloatParamSpecified)
                {
                    parameter = message.FloatParam;
                }
                else if (parameterType == typeof(double) && message.DoubleParamSpecified)
                {
                    parameter = message.DoubleParam;
                }
                else
                {
                    errorMsg = String.Format("Expected return type '{0}' was not found in the parameter message, or type is not supported. Only integral, string, and protobuf types are supported",
                                             parameterType);
                    parameter = null;
                    return(false);
                }
            }
            else if (parameterType == typeof(string))
            {
                if (message.IsNull)
                {
                    parameter = null;
                }
                else
                {
                    if (!message.StringParamSpecified)
                    {
                        errorMsg  = String.Format("Expected string return type was not found in the parameter message");
                        parameter = null;
                        return(false);
                    }
                    parameter = message.StringParam;
                }
            }
            else
            {
                //not a primitive or string, so it must be a protobuf message
                object[] attrs = parameterType.GetCustomAttributes(typeof(ProtoContractAttribute), true);
                if (attrs.Length < 1)
                {
                    errorMsg = String.Format("Expected return type '{0}' is not supported. Only integral, string, and protobuf types are supported",
                                             parameterType);
                    parameter = null;
                    return(false);
                }

                if (message.IsNull)
                {
                    parameter = null;
                }
                else
                {
                    //check the server returned a protobuf message
                    if (message.ProtoParam != null)
                    {
                        //can't check the protobuf message was the expected type, as protobuf-net is much too lenient, it doesn't
                        //check required fields or care about invalid fields either
                        var memStream = new MemoryStream(message.ProtoParam);
                        parameter = Serializer.NonGeneric.Deserialize(parameterType, memStream);
                    }
                    else
                    {
                        errorMsg = String.Format("Server had unexpected return type, was expecting a ProtoBuf message of type {0}.",
                                                 parameterType);
                        parameter = null;
                        return(false);
                    }
                }
            }

            errorMsg = null;
            return(true);
        }